-2

I am trying to configure nginx for my application setup. I have a React app, and it talks to a Node backend, running at localhost:3000.

In nginx.conf:

server { listen 80; server_name localhost; location / { root D:\....\build; index index.html; try_files $uri $uri/ /index.html; # Proxy to the Node backend proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } 

But this doesn't serve React app, just shows a page with message 'Node Authentication'.

What am I missing in my config?

    1 Answer 1

    1

    A single location can have only one content handler - either the static one, for static content serving, or provided by some nginx module (such as ngx_http_proxy_module by using the proxy_pass directive). You need to split your config in two locations, distinguishing your requests by the request URI prefix. A typical configuration should look somewhat like the following:

    location / { # implicit static content handler root D:\....\build; index index.html; try_files $uri $uri/ /index.html; } location /api/ { # explicit ngx_http_proxy_module content handler proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } 

    The /api/ URI prefix is given as an example only; the actual prefix depends on your backend application. You do need to distinguish requests to the frontend and backend somehow, and a URI prefix is the most common way to do it.

    4
    • Is /api needed even if my node backend doesn't have it in context path?
      – Mandroid
      Commented6 hours ago
    • @Mandroid Of course not. You need to use the prefix used by your backend app. More generally, you do need an ability to distinguish forntend/backend requests somehow. The simplest way is to use some URI prefix; the others, while probably could be solved too, may lead to significant complication of the configuration.Commented6 hours ago
    • Problem is my node backend doesn't have /api..now if I split as you suggested, nginx complains of collision.
      – Mandroid
      Commented5 hours ago
    • @Mandroid I have nothing to add to what has already been said: you do need an ability to distinguish frontend and backend requests somehow. If you can't, you should consider some kind of app redesign. You can't have two prefix locations using the same URI prefix.Commented5 hours ago

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.