1. nginx.conf 配置信息
由于nginx.conf配置信息較多,本篇只關注跟axios和靜態資源請求設置,順便也將常見的一些配置項備注一下。具體設置如下:
#?設定http服務器,利用它的反向代理功能提供負載均衡支持 http?{ ?#連接超時時間 ?keepalive_timeout?120; ? ?#gzip壓縮開關及相關配置 ?gzip?on; ?gzip_min_length?1k; ?gzip_buffers??4?32k; ?gzip_http_version?1.1; ?gzip_comp_level?2; ?gzip_types??text/plain?application/x-javascript?text/css?application/xml; ?gzip_vary?on; ?gzip_disable?"msie?[1-6]."; ?#設定實際的服務器列表? ?upstream?zp_server{ ?server?127.0.0.1:8089; ?} ? ?#http服務器? ?server?{ ?#監聽80端口 ?listen?80 ? ?#定義服務名稱 ?server_name?localthost; ? ?#首頁 ?index?index.html ? ?#指向項目根目錄 ?root?d:projectsrcmainwebapp; ? ?#編碼格式 ?charset?utf-8; ? ?#代理的路徑(和upstream綁定),location?后面設置映射的路徑 ?location?/?{ ??#代理配置參數 ??proxy_connect_timeout?180; ??proxy_send_timeout?180; ??proxy_read_timeout?180; ??proxy_set_header?host?$host; ??proxy_set_header?x-forwarder-for?$remote_addr; ??proxy_pass?http://zp_server/; ?? ??#跨域相關設置 ??add_header?'access-control-allow-origin'?'*'?always; ??add_header?'access-control-allow-credentials'?'true'; ??add_header?'access-control-allow-headers'?'origin,?x-requested-with,?content-type,?accept'?always; ??} ?? ??#配置靜態資源?解決js?css文件無法加載無法訪問的問題,注意末尾不能有?/ ??location?~?.*.(js|css|jpg|png)$?{ ??proxy_pass?http://zp_server; ??}? ?} }
2. proxy_pass的斜杠問題
nginx的將proxy_pass分為兩種類型:
-
一種是只包含ip和端口號的(連端口之后的/也沒有,這里要特別注意),比如proxy_pass http://localhost:8080,這種方式稱為不帶uri方式;
-
另一種是在端口號之后有其他路徑的,包含了只有單個/的,如proxy_pass http://localhost:8080/,以及其他路徑,比如proxy_pass http://localhost:8080/abc。
2.1 對于不帶uri方式
對于不帶uri方式,nginx將會保留location中路徑部分,比如:
location?/api1/?{ ?proxy_pass?http://localhost:8080; }
在訪問http://localhost/api1/xxx時,會代理到http://localhost:8080/api1/xxx
2.2 對于帶uri方式
對于帶uri方式,nginx將使用諸如alias的替換方式對url進行替換,并且這種替換只是字面上的替換,比如:
location?/api2/?{ ?proxy_pass?http://localhost:8080/; }
當訪問http://localhost/api2/xxx時,http://localhost/api2/(注意最后的/)被替換成了http://localhost:8080/,然后再加上剩下的xxx,于是變成了http://localhost:8080/xxx。
2.3 總結一下
server?{ ?listen????80; ?server_name?localhost; ?location?/api1/?{ ??proxy_pass?http://localhost:8080; ?} ?#?http://localhost/api1/xxx?->?http://localhost:8080/api1/xxx ?location?/api2/?{ ??proxy_pass?http://localhost:8080/; ?} ?#?http://localhost/api2/xxx?->?http://localhost:8080/xxx ?location?/api3?{ ??proxy_pass?http://localhost:8080; ?} ?#?http://localhost/api3/xxx?->?http://localhost:8080/api3/xxx ?location?/api4?{ ??proxy_pass?http://localhost:8080/; ?} ?#?http://localhost/api4/xxx?->?http://localhost:8080//xxx,請注意這里的雙斜線,好好分析一下。 ?location?/api5/?{ ??proxy_pass?http://localhost:8080/haha; ?} ?#?http://localhost/api5/xxx?->?http://localhost:8080/hahaxxx,請注意這里的haha和xxx之間沒有斜杠,分析一下原因。 ?location?/api6/?{ ??proxy_pass?http://localhost:8080/haha/; ?} ?#?http://localhost/api6/xxx?->?http://localhost:8080/haha/xxx ?location?/api7?{ ??proxy_pass?http://localhost:8080/haha; ?} ?#?http://localhost/api7/xxx?->?http://localhost:8080/haha/xxx ?location?/api8?{ ??proxy_pass?http://localhost:8080/haha/; ?} ?#?http://localhost/api8/xxx?->?http://localhost:8080/haha//xxx,請注意這里的雙斜杠。 }
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END