Reputation: 45
i created a Site using React js and i made some changes to Nginx to rewrite route to file so i wont get 404 error , recently i tried to use express on , if i config nginx for single page app buy using : location / { try_files $uri $uri/ /index.html; } i ll get notFound page from my app when i'm trying to reach /api , if i don't config it i can access to my express server but Site shows notFound by nginx when i try to access url except homepage Directly ,
so im looking for some exception config for nginx , i want to rewrite all route to index html except any url start with /api/*
Please save my Life :) i can not find solution on internet Kind Regards
Upvotes: 0
Views: 1850
Reputation: 1180
While it is not clear what kind of architecture you have, I will make an assumption that React app is running on a certain port, e.g. 3000 and backend server /api
on port 5000.
Here is a very basic example of a nginx configuration that routes all /api
requests to the server (port 5000) and the rest - to the React app.
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
location / {
proxy_pass http://client;
// try_files $uri $uri/ /index.html; // to redirect to the index page
}
location /api {
proxy_set_header X-Forwarded-For $remote_addr;
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
Upvotes: 5