I have a next js app which I deployed on port 3000 and set the proxy 3000 on nginx.
Now I have my custom Node js Backend server which I want to run on the same server on different port 5000.
I setup the nginx like this
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:3000;
}
location ^~ /api {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:5000;
}
I have no issue with accessing the app on example.com.
I setup the location /api for connecting the server.js / api fetching.
when I fetching like this
axios.get(example.com/api)
its returning error -- Cannot GET /api/
but when I set the nginx location proxy like this
proxy_pass http://localhost:5000/;
with a forwar slash, its working and axios is fetching the data successfully.
But whenever I am trying to fetching another route from the server.js file like
axios.get(example.com/api/credential)
Its showing error again like this
Cannot GET //credential
Returning with double slash // credential
the only '/' route working from the server.js by fetching example.com/api
without the forward slash on proxy, its returning error. can not GET /api or can not GET api/credential
please help.
Thanks in advance.
Update
I fixed with the Ivan's answer. Now I have new issue with that config.
I am using socket.io beside the axios. So whenever I am trying to connect through socket.io
my client socket code is ..
import io from "socket.io-client";
const socket = io("https://example.com/api");
and server socket config is ..
const io = require("socket.io")(server, {
cors: {
origin: "*",
},
});
Now when the page loads, the console is logging..
GET https://example.com/socket.io?EIO=4&transport=polling&t=O4UTf4l 404 (Not Found)
please help.