[英]How to set and configure node.js with express to nginx reverse proxy on centos 6.4 virtualbox client?
我正在使用VirtualBox 4.3.4在Windows 8.1系统上创建开发VM,并将CentOS 6.4安装为VMServer。 我安装了NginX和Node.js没问题。 在对管理知识不多的情况下,我删除了/etc/sysconfig/iptables
的内容,仅允许将所有连接用于开发目的(稍后将进行更改)。
NginX在我的Windows主机上运行正常,并且将浏览器指向http://192.168.1.20
表示“欢迎使用EPEL上的nginx!” 页面正在工作。
验证我的node.js应用程序是否正常运行,我通过运行在VMServer上创建了一个节点应用程序
[~]# express node_nginx_practice
[~]# cd node_nginx_practice && npm install
[~]# node app
然后,在我的Windows主机上,将URL指向http://192.168.1.20:3000
,“ Express default”页面运行正常。
问题是如何设置和配置节点应用程序以在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
设置nginx配置, /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 Blanchard”)
编辑默认的NginX配置文件:
[~]# vim /etc/nginx/conf.d/default.conf
并通过在所有行之前添加“#”来注释内部的所有内容。
重新启动NginX
[~]# /etc/init.d/nginx restart
现在,URL指向快速页面。
编辑更新:更好的解决方案。
打开/etc/nginx/conf.d/default.conf,找到该位置块并通过添加“#”对其进行注释,然后更改以指向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
}
配置中的指令(如下所示)将加载默认服务器块。 这就是为什么在新安装的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已经在本地主机的端口80上侦听,因此您的请求可能永远不会到达新定义的反向代理
尝试通过打开/etc/nginx/conf.d/default.conf
禁用默认的nginx服务器,在/etc/nginx/conf.d/default.conf
注释掉服务器块,然后重新启动nginx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.