user1543863
user1543863

Reputation:

Nginx - Reverse proxy to multiple backends

please before to mark this as duplicated read my own case, it's a bit different from others.

I'm developing multiple node.js endpoints that I'd like to have under the same domain.

These services do respond to something like:

And so on.

The first part /user/:user_id/ is the same to all services, it's just a REST way to pass the user id inside the path instead of using the Authentication header.

Is there a way I can reverse-proxy NGINX to these webservices since they are using the same base path?

Another question: if NGINX is not used for caching content, may it downgrade node.js performances (for example if its performances are worst than node.js) if it's reverse proxying?

Upvotes: 1

Views: 4131

Answers (2)

VBart
VBart

Reputation: 15110

With nginx you can do whatever routing you want, look here as a start point: http://nginx.org/en/docs/http/request_processing.html and http://nginx.org/r/location


Another question: if NGINX is not used for caching content, may it downgrade node.js performances (for example if its performances are worst than node.js) if it's reverse proxying?

node.js itself or nginx frontend for serving static files?

Also see: http://www.aosabook.org/en/nginx.html

Upvotes: 3

Vadim Baryshev
Vadim Baryshev

Reputation: 26189

If i clearly understand your question you need something like this:

server {
    listen              80;
    server_name default;
    root                /var/www/;

    access_log  /var/log/nginx/access.log combined;
    error_log   /var/log/nginx/error.log error;

    location / {
        if (-f $request_filename) {
                access_log      off;
                expires         30d;
                break;
        }
        error_log       off;
        error_page 404  = @node;
    }

    location @node {
        if ($uri ~ "/user/(.*)/authorization") { proxy_pass     http://127.0.0.1:8080; } #authorization service
        if ($uri ~ "/user/(.*)/log")           { proxy_pass     http://127.0.0.1:8081; } #log service

        proxy_next_upstream     error timeout http_500 http_502 http_503 http_504;
        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_ignore_headers    X-Accel-Expires Expires Cache-Control;
    }
}

Upvotes: 0

Related Questions