How do I do multiple nginx reverse proxies?

How do I do multiple nginx reverse proxies?

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?

답변1

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

관련 정보