zoukankan      html  css  js  c++  java
  • Nginx代理proxy pass配置去除前缀

    原文链接:https://www.cnblogs.com/woshimrf/p/nginx-proxy-rewrite-url.html

    目录
    一个种方案是proxy_pass后面加根路径/.
    另一种方案是使用rewrite
    使用Nginx做代理的时候,可以简单的直接把请求原封不动的转发给下一个服务。

    比如,访问abc.com/appv2/a/b.html, 要求转发到localhost:8088/appv2/a/b.html

    简单配置如下:

    upstream one {
      server localhost:8088 weight=5;
    }
    
    server {
        listen              80;
        server_name         abc.com;
        access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;
    
        location / {
            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_set_header X-NginX-Proxy true;
    
            proxy_pass http://one;
        }
    
    }
    

    即,设置proxy_pass即可。请求只会替换域名。

    但很多时候,我们需要根据url的前缀转发到不同的服务。

    比如

    abc.com/user/profile.html转发到 用户服务localhost:8089/profile.html

    abc.com/order/details.html转发到 订单服务 localhost:8090/details.html

    即,url的前缀对下游的服务是不需要的,除非下游服务添加context-path, 但很多时候我们并不喜欢加这个。如果Nginx转发的时候,把这个前缀去掉就好了。

    一个种方案是proxy_pass后面加根路径/.

    server {
        listen              80;
        server_name         abc.com;
        access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;
    
        location ^~/user/ {
            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_set_header X-NginX-Proxy true;
    
            proxy_pass http://user/;
        }
    
        location ^~/order/ {
            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_set_header X-NginX-Proxy true;
    
            proxy_pass http://order/;
        }
    
    }
    

    ^~/user/表示匹配前缀是user的请求,proxy_pass的结尾有/, 则会把/user/*后面的路径直接拼接到后面,即移除user.

    另一种方案是使用rewrite

    upstream user {
      server localhost:8089 weight=5;
    }
    upstream order {
      server localhost:8090 weight=5;
    }
    
    
    server {
        listen              80;
        server_name         abc.com;
        access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;
    
        location ^~/user/ {
            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_set_header X-NginX-Proxy true;
    
            rewrite ^/user/(.*)$ /$1 break;
            proxy_pass http://user;
        }
    
        location ^~/order/ {
            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_set_header X-NginX-Proxy true;
    
            rewrite ^/order/(.*)$ /$1 break;
            proxy_pass http://order;
        }
    
    }
    

    注意到proxy_pass结尾没有/rewrite重写了url

    关于rewrite

    syntax: rewrite regex replacement [flag]
    Default: —
    Context: server, location, if
    
  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/blackcat/p/13852948.html
Copyright © 2011-2022 走看看