I'm quite new to NginX, so my apologies if anything I ask here sounds naive. Lets say I have a DNS www.example.com.
Whenever a request comes to www.example.com/
(only /
), I want NginX to redirect it to www.example.com/home
, which in turn will forward the request to a React app running on port 5000.
Whenever a request comes to www.example.come/{anything}
, I want my Springboot application, running on 3001, to handle that.
So I have the following configuration
example.conf
server {
server_name example.com www.example.com;
listen 443 ssl; # managed by Certbot
location = / {
return 301 https://$host/home;
}
location / {
proxy_pass http://localhost:3001;
proxy_set_header X-Real-IP $remote_addr;
}
location = /home {
proxy_pass http://localhost:5000;
}
}
And I have the following react-app.conf
server {
listen 5000;
server_name localhost;
root /home/ubuntu/example/frontend/build;
index index.html;
location / {
}
}
This path /home/ubuntu/example/frontend/build
is where my react app's build folder is sitting.
Now whenever I try to access www.example.com/xxx
or www.example.com/xyz
, my Springboot app is getting called, but whenever I try only www.example.com
, it first redirects to www.example.com/home
and then shows the generic 404 NginX error page.
I checked the error logs. It prints the follwing:
2020/02/11 19:21:31 [error] 12936#12936: *15 open() "/home/ubuntu/example/frontend/build/home" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /home HTTP/1.0", host: "localhost:5000"
What I don't understand here is why it's trying to open /home/ubuntu/example/frontend/build/home
. Where is that trailing /home coming from and how to take care of it?
I checked this and this, but they didn't help much
Edit 1
I made the changes suggested in the comment section and my example.conf now looks like the following:
server {
server_name example.com www.example.com;
listen 443 ssl; # managed by Certbot
location = / {
return 301 https://$host/home;
}
location / {
proxy_pass http://localhost:3001;
proxy_set_header X-Real-IP $remote_addr;
}
location = /home {
proxy_pass http://localhost:5000/;
}
}
However, now I don't get any error in NginX error logs, but I do see my Springboot app getting invoked. What's happening is, example.com
is going to example.com/home
, which is then going to http://localhost:3001
where my Springboot app is currently running.
But this isn't supposed to happen since on /home
, it should have been taken to the NginX server running on port 5000. Is there any changes I need to make in my react-app.conf file?