簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM