简体   繁体   中英

How to set and configure node.js with express to nginx reverse proxy on centos 6.4 virtualbox client?

I am creating a development VM on my Windows 8.1 system using VirtualBox 4.3.4 and installed CentOS 6.4 as my VMServer. I installed NginX and Node.js with no problems. With little knowledge of administration I deleted the contents of /etc/sysconfig/iptables allowing all connections for development purpose only as of the moment (which will be changed later).

NginX is working fine, on my Windows host, and pointing the browser to http://192.168.1.20 shows that the "Welcome to nginx on EPEL!" page is working.

Verifying that my node.js app is working correctly, I created a node app on my VMServer by running

[~]# express node_nginx_practice
[~]# cd node_nginx_practice && npm install
[~]# node app

Then, on my Windows host, point the URL to http://192.168.1.20:3000 , the "Express default" page is running with no problem.

The question is how could I set and configure node app to serve on nginx reverse-proxy?

example: http://192.168.1.20 //will point to Express default page

I tried setting my nginx config in /etc/nginx/nginx.conf by following some tutorials on the web with no luck url still points to the NginX "Welcome to nginx on EPEL!" page.

Here's my nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.1:3000;
            proxy_redirect off;
        }
    }
}

I know I am missing something. Could someone point me to right direction?

EDIT ANSWER:

Here's what i did to solve the issue in step by step! (Thanks to "C Blanchard")

edit the default NginX config file:

[~]# vim /etc/nginx/conf.d/default.conf

and comment everything that is inside by prepending "#" to all of the lines.

restart NginX

[~]# /etc/init.d/nginx restart

Now the url points to the express page.

EDIT UPDATE: Better solution.

Open /etc/nginx/conf.d/default.conf find the location block and comment it by appending "#", and change specify the right location block pointing to node.js app. See code example below.

... 

#access_log  logs/host.access.log  main;

# Comment this block
#location / {
#    root   /usr/share/nginx/html;
#    index  index.html index.htm;
#}

# This is the changed location block
location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://127.0.0.1:3000;
    proxy_redirect off;
}

error_page  404              /404.html;
location = /404.html {
    root   /usr/share/nginx/html;
}

...

Now I remove the server block that I specified in /etc/nginx/nginx.conf

# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;

# Remove from here
server {
    listen 80;
    server_name localhost;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
    }
}
# Remove till here

}

The directive in your configuration (shown below) loads the default server block. This is the reason why you see the nginx welcome page when you request localhost:80 on a newly installed instance of nginx

# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/*.conf;

You'll find that nginx is already listening on port 80 for localhost so your requests may never reach your newly defined reverse-proxy

Try disabling your default nginx server by opening /etc/nginx/conf.d/default.conf , commenting out the server block there and then restarting nginx

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