2

I've setup nginx for serving my node web app (api + web) but as I see the server is only responding to "/" (web root) calls. When I test it I see the main web page (located at /index.html) but with no images or css styles and also the api which is in route /api/v1/.... (/api/v1/users, /api/v1/cars and so on) can't be reached because nginx is responding "not found".

Current nginx configuration is:

server {
    listen 80;
    server_name localhost www.mydomain.com mydomain.com

    access_log off;
    error_log off;

    location = / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

How can I configure nginx in order to serve all routes?

1 Answer 1

3

To match all routes, drop the = sign. Directives with the = prefix will match queries exactly. More information can be found here.

location / {
  proxy_pass http://127.0.0.1:3000;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Real-IP $remote_addr;
}

Here's the samples from the documentation:

location  = / {
  # matches the query / only.
  [ configuration A ] 
}
location  / {
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be
  # matched first.
  [ configuration B ] 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Saved my day! Spent hours figuring out this issue....Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.