美文网首页
关于Nginx反向代理配置去除前缀

关于Nginx反向代理配置去除前缀

作者: Katakuly | 来源:发表于2019-05-19 16:00 被阅读0次

    在使用Nginx做代理的时候,如果要根据不同的url后缀来访问不同的服务,即需要去除前缀的时候,可以通过如下两种方法配置。

    1.在配置项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/;
        }
    }
    

    2.通过重写,rewrite重写了url,参考代码:

    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;
        }
    }
    

    相关文章

      网友评论

          本文标题:关于Nginx反向代理配置去除前缀

          本文链接:https://www.haomeiwen.com/subject/yilqzqtx.html