使用proxy_pass配置Nginx

17

我正在尝试配置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 重定向到 /。我做错了什么吗?

2个回答

29

试试这个...

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。 - bear
嘿 - 好的,看看这个人的配置文件。他有两个服务器条目,可能会对你的情况有所帮助。https://dev59.com/bHM_5IYBdhLWcg3w6HvT - Andrew Kloos
11
这个答案如果加上一些背景说明和解释会更好。仅凭代码块很少能够清晰表达意思。 - Madbreaks
这个答案重定向到 / 而不是 /stash 的原因是位置 / 块在位置 ~ ^/stash 块之上。 - Root0x

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接