使用proxy_pass配置Nginx

cl25kdpy  于 2022-11-22  发布在  Nginx
关注(0)|答案(2)|浏览(200)

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

hivapdat

hivapdat1#

给予这个...

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

zbwhf8kr2#

Nginx更喜欢基于前缀的位置匹配(不涉及正则表达式),这就是为什么在您的代码块中,/stash重定向将指向/。
Nginx用来选择使用哪个位置的算法在这里详细描述:https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#matching-location-blocks

相关问题