HoneyNutIchiros
HoneyNutIchiros

Reputation: 551

How to have Nginx to proxy pass only on "/" location and serve index.html on the rest

I have a web app that uses Django for the backend and some frontend and ReactJS for stricly the frontend. I am setting up my Nginx configuration and I am trying to get Nginx to proxy_pass only on the "/" location and then on the rest of the locations I want it to serve the index.html file from React.

Here is my current Nginx configuration under sites-available. So the only url that does not have the prefix "/home/" or "/app/" is the homepage. I want Django to serve the homepage and then ReactJS to serve the rest.

server {
listen 80;
root /home/route/to/my/ReactJS/build;
server_name www.mydomain.com;

location = /favicon.ico { log_not_found off; }

location / {
    include proxy_params;
    proxy_pass http://0.0.0.0:8000;
}

location /home/ {
    try_files $uri $uri/ /index.html;
}

location /app/ {
    try_files $uri $uri/ /index.html;
}
}

Let me know if you need any more details or if I could clear things up.

Thanks!

Upvotes: 6

Views: 3174

Answers (1)

iquito
iquito

Reputation: 2545

Just change it to the following: (replacing the last three location blocks)

location = / {
    include proxy_params;
    proxy_pass http://0.0.0.0:8000;
}

location / {
    try_files $uri $uri/ /index.html;
}

The location = / only matches the exact domain, everything else will be matched by location /.

Upvotes: 2

Related Questions