Nodejs应用程序在nginx和相同的域下

时间:2014-08-25 22:47:49

标签: node.js express nginx

我试图在同一个域下提供两个nodejs应用程序,我像这样配置nginx

server {   
    listen 80;     

    location /client {
        proxy_pass         http://127.0.0.1:3000;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    location / {
        proxy_pass         http://127.0.0.1:3001;
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

}

我可以连接到第二个应用程序,即端口3001上的应用程序,但我无法连接到端口3000(App1)上的那个,我得到502 Bad Gateway。

App1正在快递4.x上运行,我还需要配置其他东西吗?

1 个答案:

答案 0 :(得分:1)

server {
listen 80;
root /var/www;
server_name your-domain.com;

location /app1 {
    proxy_pass http://localhost:{YOUR_PORT};
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location /app2 {
    proxy_pass http://localhost:{YOUR_PORT};
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
}

尝试类似于上面的配置