Score:0

Is there a way in Nginx to remove a route specified in location from one domain and port and forward it to reverses proxy?

ca flag

I want to connect example.com:80 and example.com:82 to a specific site through reverse proxy.

localhost/api/buy/ -> http://example.com:80/

localhost/api/sell/ -> http://example.com:82/

I set up the config file as below.

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

}

But If I connect /api/buy and /api/sell, I get a 404 not found error.

Below is the contents of the error.log file.

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"

When using a different port, I confirmed that the setting works well.

I want to set it to the same port, but is there any way?

Score:0
ca flag

best way is changed proxy_pass http://example.com:80/ to proxy_pass 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;
        }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.