【问题标题】:Configure Nginx with proxy_pass使用 proxy_pass 配置 Nginx
【发布时间】:2012-10-02 14:27:20
【问题描述】:

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

【问题讨论】:

    标签: nginx reverse-proxy proxypass


    【解决方案1】:

    试试这个...

    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
    • 嘿 - 好的,看看这个人的 conf 文件。他有两个服务器条目可以帮助您解决问题。 stackoverflow.com/questions/1174554/…
    • 这个答案可以通过上下文/解释来改进。单独的代码块很少是足够的。
    • 这个答案重定向到/ insteal of /stash的原因是因为位置/块在位置上方~^/stash块
    【解决方案2】:

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

    Nginx 用来选择使用哪个位置的算法在这里详细描述:https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms#matching-location-blocks

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多