如何在centos 6.4 virtualbox客户端上用express来设置和configurationnode.js到nginx反向代理?

我正在使用VirtualBox 4.3.4在我的Windows 8.1系统上创build一个开发虚拟机,并安装了CentOS 6.4作为我的VMServer。 我安装NginX和Node.js没有问题。 在pipe理知识less的情况下,我删除了/etc/sysconfig/iptables的内容,允许所有连接仅用于开发目的(稍后将进行更改)。

Nginx工作正常,在我的Windows主机上,并指向浏览器http://192.168.1.20显示“欢迎来到EPEL的nginx!” 页面正在工作。

validation我的node.js应用程序工作正常,我通过运行在我的VMServer上创build了一个节点应用程序

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

然后,在我的Windows主机上,将URL指向http://192.168.1.20:3000 default”页面运行时没有任何问题。

问题是我如何设置和configuration节点应用服务于nginx反向代理?

例如: http://192.168.1.20 //will point to Express default page : http://192.168.1.20 //will point to Express default page

我尝试在/etc/nginx/nginx.conf按照一些教程设置/etc/nginx/nginx.conf的nginxconfiguration, /etc/nginx/nginx.conf没有运气,url仍指向Nginx“欢迎使用EPEL上的nginx!” 页。

这是我的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; } } } 

我知道我错过了一些东西。 有人可以指出我正确的方向吗?

编辑答案:

这就是我一步一步解决问题的方法! (感谢“C布兰卡德”)

编辑默认的NginXconfiguration文件:

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

并通过在所有行中加上“#”来注释所有内容。

重新启动NginX

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

现在url指向快速页面。

编辑更新:更好的解决scheme。

打开/etc/nginx/conf.d/default.conffind位置块,并通过追加“#”来注释它,并更改指定正确的位置块指向node.js应用程序。 看下面的代码示例。

 ... #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; } ... 

现在我删除在/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 

}

您的configuration中的指令(如下所示)将加载默认的服务器块。 当你在新安装的nginx实例上请求localhost:80时,这就是为什么你看到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; 

你会发现nginx已经在localhost的端口80上侦听,所以你的请求可能永远不会到达你新定义的反向代理

尝试通过打开/etc/nginx/conf.d/default.conf禁用默认的nginx服务器,在那里注释掉服务器块,然后重新启动nginx