Nginx 有沒有一種方法可以從一個網域和連接埠中刪除 location 中指定的路由並將其轉送到反向代理?

Nginx 有沒有一種方法可以從一個網域和連接埠中刪除 location 中指定的路由並將其轉送到反向代理?

我想連接example.com:80example.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 not found 錯誤。

以下是 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;
        }

相關內容