Score:0

How do I do multiple nginx reverse proxies?

ca flag

I want to connect example.com/api/buy and example.com/api/sell to a specific site through reverse proxy.

So I am trying to connect through localhost/api/buy and localhost/api/sell with nginx.

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.

You can set it up using a different port.

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

diya avatar
la flag
Look in your logs as to why you get the 404 (on both nginx and the backend )
Richard Smith avatar
jp flag
You are stripping off `/api/buy` from the URI before passing it upstream - is that correct. If the upstream server expects the original URI, remove the trailing `/` from your `proxy_pass` statements. See [proxy_pass documentation](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass).
diya avatar
la flag
I *think* that when using the default TCP port for HTTP ; `80` (and `443` for HTTPS) formatting the URI with a port number is considered incorrect ( see https://www.rfc-editor.org/rfc/rfc3986#page-22 ), in other words use: `proxy_pass http://example.com/` rather than `proxy_pass http://example.com:80/`
john_smith avatar
ca flag
@diya/ There is nothing wrong with uri. Adding server works fine. The log shows an error saying that there are no files in C:/.../api/buy/ and C:/.../api/sell/. (The system cannot find the file specified)
john_smith avatar
ca flag
@Richard Smith / I tried deleting the / at the end of the proxy pass uri, but the same 404 error occurred.
Score:0
ca flag

When location is specified using a regular expression, and also inside named locations.

In these cases, proxy_pass should be specified without a URI.

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

I sit in a Tesla and translated this thread with Ai:

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.