1

I have following nginx setup where I used proxy configuration to redirect my landing page (built with reactjs) to my domain, say (www.example.com), here is my nginx configuration

server {
listen 80;
listen [::]:80;
server_name www.example.com;

location / {
    proxy_pass http://127.0.0.1:8000;
}

........
........

}

no problem with the above configuration if I request on www.example.com, it give me the redirected web page(my landing page) hosted on http://127.0.0.1:8000. But if I try to navigate to a sub page like http://127.0.0.1:8000/orders, what should I do then? I tried the following

location /orders {
    proxy_pass http://127.0.0.1:8000/orders;
}

but it's not working if I request on www.example.com/orders, its always redirect me into the www.example.com (landing page), i.e http://127.0.0.1:8000.

1 Answer 1

2

This is my way of handling routes in reactjs and nginx I send every path to index.html and as a result reactjs is responsible for routes, and for that purpose I use reactrouter

location / {
    try_files $uri /index.html;
}
6
  • You are suggesting that they remove the reverse proxy altogether and deploy the react files directly under a directory served by nginx? Oct 26, 2021 at 11:08
  • yes I do, is that wrong? Oct 26, 2021 at 11:25
  • No, it is a good idea. But maybe you could expand your answer to say that explicitly. Maybe also explain why that is better than the reverse proxy. Oct 26, 2021 at 12:10
  • Actually, I don't understand why he needs to use different ports for the domain and react app. Here is a good tutorial on deploying a reactjs application with nginx. link Oct 26, 2021 at 13:29
  • Rather than adding details in comments, please edit your answer to add requested information. You can then comment saying that you have done so. Comments here may get hidden by "show more comments" or could be deleted without notice. Editing updates into your answer ensures that they don't get lost and that everybody who wants to use your answer sees them. Oct 26, 2021 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.