Nginx proxy config problems for Node.js app

2019-09-01 15:25发布

问题:

I'm almost certain this is just my ignorance and expecting this to be an easy fix, but basically mydomain.com:3000 serves the correct content, but mydomain.com does not. Here is what my nginx.conf.default file has:

upstream mydomain {
    server 127.0.0.1:3000;
    keepalive 8;
}

server {
    listen       0.0.0.0:80;
    server_name  mydomain.com www.mydomain.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://mydomain/;
        proxy_redirect off;
    }
}

I've been following this article, but can't figure out where my error is: http://dailyjs.com/2010/03/15/hosting-nodejs-apps/

(Note: I believe my installation of nginx differs from this as there is no sites-available folder. Instead, I'm editing the nginx.conf.default file because I saw the lines for the localhost server above the ones I've got here.)

Basically, what I want is to be able to visit mydomain.com to load the content, instead of mydomain.com:3000.

回答1:

This is all a matter of distros and packages not nginx versions, I've explained this in a different answer before, the main and the only file that matters is nginx.conf, if you check that file you would know what folders matters and what don't, for example debian distros add this line in their config

include /etc/nginx/sites-enabled/*;

So that auto includes all the files inside the sites-enabled folder,
Redhat distros use this line only

include /etc/nginx/conf.d/*.conf;

which is btw still also included in the debian distributed config,
This means that only the files inside /etc/nginx/conf.d are considered, and also they have to end with .conf

Just check your nginx.conf and make sure that your config is in an included folder, as for the config it self it seems that it should be working fine.



回答2:

With the newer installation of NginX, there is a subfolder called vhosts. Virtual Host config data needs to go in this folder. If you add a domain to cPanel using the UI, it creates a vhost file automatically.