1

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:

  • /user/:user_id/authorization - base path for the authorization service
  • /user/:user_id/log - base path for the log service

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?

2 Answers2

3

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

Community
  • 1
  • 1
VBart
  • 14,714
  • 4
  • 45
  • 49
  • Thanks for these resources, I'm reading them all. I've also seen your comment on Vadim answer and I checked out "IfIsEvil", they say it's something that works or not, is not a random behaviour, so if I configure it and it worked on tests it will always work... Otherwise they purpose different approaches if any. This is my question: do I have any other way to do that? I'm reading "request_processing", should I use differend "location" sections? Would it do the job? Many thanks. –  Nov 16 '12 at 13:45
  • Yes, using locations is preferred way in nginx. It makes config clearer. Moreover, the "location" is optimized as effective search tree and works faster. Different tools for different tasks. I know that apache guys like to programm in its configs, but nginx is different. – VBart Nov 16 '12 at 14:17
  • Also note, that you can have embed location blocks, like: `location /user/ { location ~ ^/user/(?:.*)/authorization$ { .. } }` – VBart Nov 16 '12 at 14:30
0

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;
    }
}
Vadim Baryshev
  • 25,689
  • 4
  • 56
  • 48