Nginx 中是否有办法从一个域和端口中删除位置中指定的路由并将其转发到反向代理?

Nginx 中是否有办法从一个域和端口中删除位置中指定的路由并将其转发到反向代理?

我想要连接示例.com:80示例.com:82通过反向代理到特定站点。

本地主机/api/购买/->http://example.com:80/

本地主机/api/销售/->http://example.com:82/

我按如下方式设置了配置文件。

server {
    listen       80;
    server_name  localhost;



    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }


    location = /api/buy {
    return 302 /api/buy/;
    }

        location /api/buy/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_pass http://example.com:80/;
            proxy_redirect off;
            }

    location = /api/sell {
    return 302 /api/sell/;
    }

        location /api/sell/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header HOST $host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_pass http://example.com:82/;
            proxy_redirect off;
            }

}

但是如果我连接 /api/buy 和 /api/sell,我会收到 404 未找到错误。

以下是error.log文件的内容。

connect() failed (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /api/ HTTP/1.1", upstream: "http://example.com/api/buy/", host: "localhost"

当使用不同的端口时,我确认设置运行良好。

我想将其设置为相同的端口,但是有什么办法吗?

答案1

最好的办法是改变代理密码http://example.com:80/代理密码http://example.com:80/api/buy/

location = /api/buy {
return 302 /api/buy/;
}

    location /api/buy/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://example.com:80/api/buy/;
        proxy_redirect off;
        }

相关内容