今天,測試 一個后台接口,寫了個html,在其中使用jquery發送請求給后台,chrome91竟然報錯說跨域給error了!那豈不是以后本機頁面不能和后端愉快的玩耍了?
於是,配置起了nginx+反向代理的方式,旨在迂回對抗chrome的新安全機制。
目標地址:http://127.0.0.1:8006/publish/template/modifyInterPublish
配置如下:
1 server { 2 listen 80; 3 server_name localhost; 4
5 location / { 6 root html; 7 index index.html index.htm; 8 if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|jar|war)$) { 9 add_header Content-Disposition 'attachment'; 10 } 11 } 12
13 location /proxy { 14 proxy_pass http://127.0.0.1:8006;
15 index index.html; 16 } 17
18 }
結果:怎么也訪問不到!
原來只差一個“/”!
即:
1 location /proxy { 2 proxy_pass http://127.0.0.1:8006;
3 index index.html; 4 }
改為:
1 location /proxy { 2 proxy_pass http://127.0.0.1:8006/;
3 index index.html; 4 }
原來proxy_pass匹配路徑后加杠代理后將去掉已匹配的路徑,不加杠則保留已匹配的路徑。
請求網址:http://127.0.0.1:8006/publish/template/modifyInterPublish
使用 proxy_pass http://127.0.0.1:8006; 代理后的網址為:http://127.0.0.1:8006/proxy/publish/template/modifyInterPublish
使用 proxy_pass http://127.0.0.1:8006/; 代理后的網址為:http://127.0.0.1:8006/publish/template/modifyInterPublish
而location匹配的路徑后有沒有杠無所謂,即
location /proxy 和 location /proxy/ 匹配的結果沒有區別。
最后再上正確的配置:
1 server { 2 listen 80; 3 server_name localhost; 4
5 location / { 6 root html; 7 index index.html index.htm; 8 if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|jar|war)$) { 9 add_header Content-Disposition 'attachment'; 10 } 11 } 12
13 location /proxy { 14 proxy_pass http://127.0.0.1:8006/;
15 index index.html; 16 } 17
18 }