0
votes

I have a nodejs server that's served with nginx as reverse proxy. That part is ok, and static files locations are set up correctly. But I want the root address to serve a static html file, and I don't know how to configure nginx so that the root url is not redirectected to the node app. Here's my server block:

upstream promotionEngine {
 server 127.0.0.1:3001;
}

server {
    listen       3000;
    server_name  localhost;
    root C:/swaven/dev/b2b.pe/promotionEngine/templates/;
    index index.html;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://promotionEngine;
        proxy_redirect off;
    }

    location /public/ {
      alias C:/swaven/dev/b2b.pe/promotionEngine/public/;
    }

    location /assets/ {
      alias C:/swaven/dev/b2b.pe/promotionEngine/assets/;
    }
}

htttp://localhost:3000/ping and http://localhost:3000/public/js/riot.js are correctly served.
But http://localhost:3000 keeps being sent to the node server, where I would like it to return a static index.html. If I remove the / location bloc, the html file is correctly served. How would I configure the location to work as reverse proxy for all urls except the root one ?

3

3 Answers

2
votes

UPDATED: (based on comments and discussion)

You'll need 2 exact location blocks. One to intercept the / location and another to serve just /index.html.

An exact location block is described on nginx docs:

Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.

Simply using the index directive does not work. Because nginx creates an internal redirect to allow other blocks to match index.html. Which gets picked up by your proxy block.

upstream promotionEngine {
 server 127.0.0.1:3001;
}

server {
    listen       3000;
    server_name  localhost;

    # Do an exact match on / and rewrite to /index.html
    location = / {
      rewrite ^$ index.html;
    }

    # Do an exact match on index.html to serve just that file
    location = /index.html {
      root C:/swaven/dev/b2b.pe/promotionEngine/templates/;
    }

    # Everything else will be served here
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://promotionEngine;
      proxy_redirect off;
    }

    location /public/ {
      alias C:/swaven/dev/b2b.pe/promotionEngine/public/;
    }

    location /assets/ {
      alias C:/swaven/dev/b2b.pe/promotionEngine/assets/;
    }
}
1
votes

You can use =/ this type of location have higher priority due to lookup:

location =/ {
  root ...
}

This request will not even try to reach other locations.

-1
votes
Something like this, adjust for your own use case.

http {
    map $request_uri $requri {
        default          1;
        /                0;
    }
...........
    server {
        listen       80;
        server_name  www.mydomain.eu;
        root   '/webroot/www.mydomain.eu’;
        if ($requri) { return 301 https://www.mydomain.eu$request_uri; }
        location / {
            ..........
        }
    }