简体   繁体   中英

How to configure nginx server to forward request to a node server with empty path?

i have an Nginx server with the following configuration:

server
{
    listen 80;
    server_name example.ca www.example.ca;
    location / {

proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

and on the node server am listening to port 3000 using node express as follow;

app.get('*', function (req, res) {
  console.log(' request received ', req.headers.host);
});

and i have a domain www.example.com which is pointing to the Nginx server IP, the test am doing is:

http://www.example.com/test result ===> request received www.example.com

http://www.example.com/test/abc result ===> request received www.example.com

http://www.example.com result ===> ( i get no result !! its like the app.get did not triggered )

so can any one help me in this issue ? when i browse the domain without any path parameter am not getting any result, so its like the Nginx didn't forward this request to the node server !!

First of all when you browse abc.com using browser or curl it will always send the path as / . There is no way to send a blank path

Next your nginx config is of a socket.io implementation. It should be below

server
{
    listen 80;
    server_name example.ca www.example.ca;
    location / {
       proxy_pass http://127.0.0.1:3000;
       proxy_http_version 1.1;
       proxy_set_header Host $host;
    }
}

Next you are not ending your requests they will be just timing out. Change your code to below

app.get('*', function (req, res) {
  console.log(' request received ', req.headers.host);
  res.status(200).send("all ok");
});

Also when you have issues you can test it using curl like below

curl -v http://www.example.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM