ng的用途就不用說了,反向代理么,都知道,不過以前一直不太理解怎沒配,現在終於理解點了
1.下載ng,如圖:
2.先解壓,解壓后的路徑不建議有空格和中文,其次配置環境變量,加到系統path
3.啟動ng win+R---->cmd---->start nginx
4.修改配置文件
#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; sendfile on; keepalive_timeout 65; gzip on;
#tomcat集群,這里只有一台機器,不管什么均衡策略都用不到 upstream tomcat_cxts { ip_hash; #均衡策略 server localhost:8081 max_fails=3 fail_timeout=3s; } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } #靜態圖片轉發配置 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { root D:/webapps/static_resource/imgs; if (-f $request_filename) { expires 1d; break; } } #根據項目名稱進行轉發,如果是cxts,轉發到上面定義的名字叫tomcat_cxts的upstream組 location /cxts { proxy_redirect off; proxy_set_header Upgrade $http_upgrade; #websocket配置參數 proxy_set_header Connection "upgrade"; #websocket配置參數 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat_cxts/cxts; #英文字義:代理傳遞,即拿到數據傳給哪個,這里的tomcat_cxts就是上面配置的upstream proxy_read_timeout 2; #websocket不需要此參數,加上去websocket會自動斷開 } access_log logs/cxts.log; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
轉發策略還可以寫if條件,ng還有很多內置變量,監聽地址還支持正則表達式,結合起來我覺得想怎么轉都行了
5.監測配置是否能通過檢查 nginx -t
6.nginx -s reload 重新加載配置