使用NGINX的一个VPS实例中的多个网站

时间:2015-10-14 11:35:26

标签: node.js nginx reverse-proxy

我仍在努力为一个虚拟机中的多个网站配置nginx。 所以,如果我这样做:

server {
    listen 80;

    server_name example1.com;

    location / {
        proxy_pass http://localhost:8181;
        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;
    }
}

点击example1.com后,我的nodejs页面被正确加载。但是,如果我尝试添加第二个服务器块:

server {
    listen 80;

    server_name example2.com;

    location / {
        proxy_pass http://localhost:8080;
        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;
    }
}

没有什么工作,也是例子1。所以我想通过example2.com默认的nginx位置加载...类似的东西:

server {
    listen 80;

    server_name example1.com;

    location / {
          root /usr/share/nginx/html;
          index index.html index.htm;
    }
}

server {
    listen 80;

    server_name example2.com;

    location / {
        proxy_pass http://localhost:8181;
        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;
    }
}

但它总是被重定向到nginx根位置。

我该怎么做?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

Node.js的?

您可以参考我的配置:

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://127.0.0.1:2368;
}

或另一个

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

第一个配置指向https重定向循环。

你说默认网站,你可以配置类似的东西:

listen 80 default;
server_name xxxxx.com;