Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在centos 6.4 virtualbox客户端上使用express to nginx反向代理设置和配置node.js?_Node.js_Express_Centos_Virtualbox - Fatal编程技术网

如何在centos 6.4 virtualbox客户端上使用express to nginx反向代理设置和配置node.js?

如何在centos 6.4 virtualbox客户端上使用express to nginx反向代理设置和配置node.js?,node.js,express,centos,virtualbox,Node.js,Express,Centos,Virtualbox,我正在使用VirtualBox 4.3.4在Windows 8.1系统上创建开发虚拟机,并将CentOS 6.4安装为我的虚拟机服务器。我安装了NginX和Node.js,没有问题。由于对管理知之甚少,我删除了/etc/sysconfig/iptables的内容,目前只允许所有用于开发目的的连接(稍后将更改) NginX在我的Windows主机上运行良好,并将浏览器指向http://192.168.1.20显示“欢迎使用EPEL上的nginx!”页面正在工作 为了验证node.js应用程序是否正

我正在使用VirtualBox 4.3.4在Windows 8.1系统上创建开发虚拟机,并将CentOS 6.4安装为我的虚拟机服务器。我安装了NginX和Node.js,没有问题。由于对管理知之甚少,我删除了
/etc/sysconfig/iptables
的内容,目前只允许所有用于开发目的的连接(稍后将更改)

NginX在我的Windows主机上运行良好,并将浏览器指向
http://192.168.1.20
显示“欢迎使用EPEL上的nginx!”页面正在工作

为了验证node.js应用程序是否正常工作,我通过运行

[~]# express node_nginx_practice
[~]# cd node_nginx_practice && npm install
[~]# node app
然后,在我的Windows主机上,将URL指向
http://192.168.1.20:3000
“快速默认”页面运行正常

问题是如何设置和配置节点应用程序以在nginx反向代理服务器上提供服务?

示例:
http://192.168.1.20 //将指向Express默认页面

我尝试在
/etc/nginx/nginx.conf
中设置我的nginx配置,方法是在web上遵循一些教程,但没有运气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布兰查德”)

编辑默认的NginX配置文件:

[~]# vim /etc/nginx/conf.d/default.conf
并通过在所有行前加“#”来注释其中的所有内容

重新启动NginX

[~]# /etc/init.d/nginx restart
现在url指向express页面

编辑更新:更好的解决方案。

打开/etc/nginx/conf.d/default.conf找到位置块并通过添加“#”对其进行注释,然后更改指定指向node.js app的正确位置块。请参见下面的代码示例

... 

#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服务器,在那里注释掉服务器块,然后重新启动nginx

您能详细说明一下“运气不好”吗?你看到了什么样的错误?我的意思是url仍然指向NginX欢迎页面。Wew!真管用!从昨天起我就一直在做这个。谢谢