0

I've tried the following:

    # this *should* serve the root from a static index.html file
    location = / {
            try_files $uri $uri.html $uri/ /index.html;
    }

    # resources are served directly by nginx
    location /img {
    }
    location /css {
    }

    # all other URLs are served dynamically by nodejs
    location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;
    }

The location /img, location /css and location / clauses work.

I'm having trouble with location = /. I added $uri $uri.html $uri/ because tryfiles won't accept just /index.html as a single argument.

Requests to / are passed upstream, rather than being answered by nginx serving the (static) index.html file.

How can I redirect the root directory to the proxy server (NodeJS), while serving a literal '/' from a static file?

fadedbee
  • 2,068
  • 5
  • 24
  • 36

1 Answers1

1

I fixed it by another method:

    # any url that is not served by a file is passed to nodejs
    location / {
            try_files $uri $uri.html $uri/ @backend;
    }

    # all other URLs are served dynamically by nodejs
    location @backend {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Scheme $scheme;
    }
fadedbee
  • 2,068
  • 5
  • 24
  • 36