为具有自己域的多个 node.js 应用程序配置 nginx

为具有自己域的多个 node.js 应用程序配置 nginx

我在 Debian squeeze 上用 nginx 启动并运行了一个 node webapp。现在我想添加另一个有自己域名的 webapp,但当我这样做时,只有第一个应用程序可用,即使我转到第二个域名,我也只会被重定向到第一个 webapp。希望您看到我在这里做错了什么:

示例1.conf:

upstream example1.com {
    server 127.0.0.1:3000;
}

server {
listen   80;
server_name  www.example1.com;
rewrite ^/(.*) http://example1.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example1.com;
    access_log /var/log/nginx/example1.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    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-NginX-Proxy true;

      proxy_pass http://example1.com;
      proxy_redirect off;
    }
 }

示例2.conf:

upstream example2.com {
    server 127.0.0.1:1111;
}

server {
listen   80;
server_name  www.example2.com;
rewrite ^/(.*) http://example2.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example2.com;
    access_log /var/log/nginx/example2.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    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-NginX-Proxy true;

      proxy_pass http://example2.com;
      proxy_redirect off;
    }
 }

curl 只是这样做:

zazzl:Desktop udo$ curl -I http://example2.com/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.2
Date: Sat, 04 Aug 2012 13:46:30 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://example1.com/

谢谢 :)

答案1

抱歉,各位。我这里刚刚出现了某种 DNS 问题,删除本地 DNS 缓存后一切恢复正常。无论如何,感谢您的帮助。

相关内容