3

I have an Nginx server which works as a proxy server.

I also have 3 different NodeJS Express servers running on ports 8080, 9090 and 8888 (all working correctly).

Servers 8080 and 9090 execute the same APP. Server on 8888 currently should return 'POSTING' when the POST request has been forwarded.

Both GET and POST routes are set up correctly for server 8888 and I get a correct response if I call them directly with CURL requests:

import Express from 'express';
import BodyParser from 'body-parser';

// Initialise server
const server = Express();

// for parsing application/json
server.use(BodyParser.json());

// Setting port
server.set('port', 8888);

server.post('/', function(request, response) {

    console.log('POSTING');
    response.send('POSTING');
});

server.get('/', function(request, response) {

    console.log('GETTING');
    response.send('GETTING');
});

console.log(`Starting server on port ${server.get('port')}`)
server.listen(server.get('port'));

My Nginx config is the following:

upstream amit4got {

    server 127.0.0.1:8080;
    server 127.0.0.1:9090;
}

server {

    listen       7070;
    server_name  127.0.0.1;

    access_log  /usr/local/etc/nginx/logs/default.access.log;
    error_log  /usr/local/etc/nginx/logs/default.error.log;


    location /amit4got/ {

        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        proxy_pass              http://amit4got;
        proxy_read_timeout      90;
    }


    location /amit4got/flows {

        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto $scheme;

        proxy_read_timeout      90;

        if ($request_method = POST) {

            proxy_pass          http://127.0.0.1:8888;
        }

        if ($request_method = GET) {

            proxy_pass          http://amit4got;
        }

     }
}

Nginx serves on port 7070, and for the endpoint /amit4got loads the content from either 8080 or 9090.

The problem is that when I try to POST to /amit4got/flows, there is no POST request to http://127.0.0.1:8888. I just get a response of 404 not found.

If I change the proxy_pass to a rewrite, then I get a correct response from the 8888 server.

if ($request_method = POST) {

    rewrite ^ http://127.0.0.1:8888;
}

I need to send through the POST params, so the rewrite does not work for me.

How can I make the proxy_pass to server 8888 work?

Thanks, Amit

Amit4got
  • 31
  • 2
  • From what I can see, the server 8888 has to have the same endpoint as the one which was specified in the proxy_pass ... `server.post('/amit4got/flows', function(request, response) { console.log('POSTING'); response.send('POSTING'); });` – Amit4got Mar 28 '16 at 15:03

1 Answers1

0

As mentioned in the comment:

proxy_pass http://127.0.0.1:8888; will end up posting to http://127.0.0.1:8888/amit4got/flows and not to http://127.0.0.1:8888/.

Amit4got
  • 31
  • 2
  • [Nginx Docs Proxy_Pass](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass): _Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a domain name or IP address, and an optional port: proxy_pass http://localhost:8000/uri/;_ – Amit4got Mar 30 '16 at 09:47
  • check your ifs, see https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ – djdomi Jun 14 '21 at 04:13