nginx中的sub_filter

需求/问题

最近在做一个需求, 大概的部署模型是这样的:

由于有严格的端口限制(对外暴露80端口) 所以我们在右边的服务器才有一个nginx来根据api path做反向代理.

因为想把我们的代码跟CI jenkins集成, 所以想找个办法来看看怎么将jenkins也通过代理服务器的80端口访问?

步骤

step1:

我们将/j/的路径访问到jenkins服务器地址.  比如服务器为: test.com, 那么我们通过test.com/j/来访问jenkins:

location /j/ {
      proxy_pass http://127.0.0.1:8002/; #这里的
      rewrite ^/j/(.*)$ /$1 break;   # 并且重写去掉/j 然后发给jenkins
    }         

问题:  发现自动重定向了到test.com/login:

Step2:

添加sub_filter来重写content:

location /j/ {
      proxy_pass http://127.0.0.1:8002/; #这里的端口记得改成项目对应的哦
      proxy_set_header Accept-Encoding "";
       rewrite ^/j/(.*)$ /$1 break;
      sub_filter '/login' '/j/login';
      sub_filter '/static/' '/j/static/';
      sub_filter_types *;
      sub_filter_once off;
      sub_filter '/adjuncts' '/j/adjuncts';
    }       

 这里面看到我加了很多的声明. 为了方便我来解释下:

sub_filter '/login' '/j/login';   --- 这个我们发现返回值里面有重定向 所以我们需要把jenkins返回的内容进行替换重写. sub_filter       sub_filter '/adjuncts' '/j/adjuncts';  -- 这个也是类似.  但是的多用firefox/chrome的开发者工具才看得出来 有哪些返回失败.

注意只有在新版本nginx中才支持多sub_filter.

sub_filter_types *;  -- 对所有请求响应类型都做sub_filter指定的替换.

sub_filter_once off; -- sub_filter会执行多次而不是一次. 效果类似于java中的string.replaceAll而不是replace.

proxy_set_header Accept-Encoding "";  -- 设置这个得原因是告诉后端不要进行gzip压缩.  如果是gzip压缩流, 那么我们就没法进行替换了.

# 贴一个完整的nginx.conf

        server_name test.com localhost;
        index index.html;

        root  /usr/share/nginx/ui;

        # 避免访问出现 404 错误

        location /api/ {
          proxy_pass http://test.com:8000; #这里的端口记得改成项目对应的哦
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Port $server_port;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }
        location /j/ {
          gzip off;
          proxy_pass http://127.0.0.1:8002/; #这里的端口记得改成项目对应的哦
          proxy_set_header Accept-Encoding "";
          rewrite ^/j/(.*)$ /$1 break;
          sub_filter '/login' '/j/login';
          sub_filter '/static/' '/j/static/';
          sub_filter_types *;
          sub_filter_once off;
          sub_filter '/adjuncts' '/j/adjuncts';
        }
         location /chatbot/ {
          proxy_pass http://test.com:8001; #这里的端口记得改成项目对应的哦
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Port $server_port;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }

        location / {
          try_files $uri $uri/ @router;
          index  index.html;
        }

        location @router {
          rewrite ^.*$ /index.html last;
        }
    }
}

 

 

 

 

 

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值