• nginx根据路径配置反向代理


      今天,测试 一个后台接口,写了个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 }
  • 相关阅读:
    pm3
    算法交易系列研究之一
    CDS究竟是个什么鬼?它直接导致了次贷危机?
    源特定组播(SSM:Source Specific Multicast)
    投资交易系统常用表
    交易系统解析(六)前台报盘应用设计要点
    人物
    句子
    康德拉季耶夫周期
    非标
  • 原文地址:https://www.cnblogs.com/guanghe/p/14954236.html
Copyright © 2020-2023  润新知