最近在實習公司的開發一個項目,項目是前后端徹底分離的項目,前端項目和后端項目各監聽着特定的端口號,顯然不是80的通用端口,為了不在地址欄上輸入IP+端口號的形式,我們可以使用Nginx作為反向代理服務器,達到人性化訪問的目的。
一,設置hosts
當然首先別忘了設置hosts,把你想要的域名設置進去,設置方法如下:
cd /etc sudo vi hosts //hosts文件 //添加示例如下: 127.0.0.1 www.example.com
這個時候你訪問www.example.com就是訪問的127.0.0.1:8080這個url
好了,設置好了hosts,我們需要配置Nginx反向代理服務器的配置。
二,配置Nginx反向代理服務器
首先我們進入Nginx的目錄。
cd /usr/local/etc/nginx
我們會發現這個目錄下有個nginx.conf的配置文件
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include servers/*;//主要看這里,這里的配置說明了除了可以在這里配置Nginx的配置外,還可以在當前目錄下的servers子目錄中配置。 }
那么我們去servers目錄下配置我們的屬性。
upstream example_server_name { server localhost:8081; //這里配置我們的前端地址,其實這里可以配置多個地址那么Nginx做的不光是反向代理的功能了,還能實現負載均衡的功能。 }
這樣設置好了我們的前端地址,那么我們需要把訪問hosts設置的地址轉向訪問我們上面設置的前端地址。
server { listen 80; //這里國際默認通用80端口 server_name www.example.com *.example.com;//hosts中設置的訪問地址 # individual nginx logs for this web vhost access_log /tmp/rrs-access.log; error_log /tmp/rrs-error.log ; location = /favicon.ico { return 404; } #when not specify request uri, redirect to /index; location = / { rewrite ^ /index ; } #static files location ~/(.*)$ { root x/xx/xx; //靜態資源訪問 expires 30d; access_log off; }
location / {
proxy_pass http://example_web_server; //配置轉向的upstream,就是最上面設置的upstream。
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Mobile-Client $mobile_request;
}
}
這個時候Nginx反向代理服務器的配置就已經配置完成了,我們可以訪問www.example.com驗證一下Nginx代理是否成功。
三,Nginx命令
sudo nginx //啟動Nginx
sudo nginx -s reload //重啟nginx
sudo nginx -s quit //退出nginx
我也是剛接觸到Nginx很多東西也不是很熟悉,慢慢學習以后不斷補充......
