16

我正在尝试将 Nginx 配置为代理子域上的内容:dev.int.com

我希望将 dev.int.com 代理到 IP:8080,并将 dev.int.com/stash 代理到 IP:7990

这是我当前的配置文件

server {
listen   80;
server_name  dev.int.com;
access_log off;
location / {
    proxy_pass http://IP:8080;
    proxy_set_header    Host            $host;
    proxy_set_header    X-Real-IP       $remote_addr;
    proxy_set_header    X-Forwarded-for $remote_addr;
    port_in_redirect off;
    proxy_redirect   http://IP:8080/jira  /;
    proxy_connect_timeout 300;
    location ~ ^/stash {
        proxy_pass http://IP:7990;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-for $remote_addr;
        port_in_redirect off;
        proxy_redirect   http://IP:7990/  /stash;
        proxy_connect_timeout 300;
    }
}

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   /usr/local/nginx/html;
    }
}

但是,/stash 重定向将转到 /。我究竟做错了什么?

4

2 回答 2

28

试试这个...

server {
    listen   80;
    server_name  dev.int.com;
    access_log off;
    location / {
        proxy_pass http://IP:8080;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-for $remote_addr;
        port_in_redirect off;
        proxy_redirect   http://IP:8080/jira  /;
        proxy_connect_timeout 300;
    }

    location ~ ^/stash {
        proxy_pass http://IP:7990;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-for $remote_addr;
        port_in_redirect off;
        proxy_redirect   http://IP:7990/  /stash;
        proxy_connect_timeout 300;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/nginx/html;
    }
}
于 2012-10-11T20:56:36.320 回答
0

Nginx 更喜欢基于前缀的位置匹配(不涉及正则表达式),这就是为什么在您的代码块中,/stash 重定向将转到 /。

Nginx 用于选择要使用的位置的算法在此处详细描述:https ://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#matching-location-块

于 2019-10-28T19:42:36.800 回答