Nginx将几个域代理到不同的端口

时间:2013-03-28 09:27:56

标签: nginx debian virtualhost

我有一台nginx监听端口80的服务器,并且有几个域名的配置,应该将其连接到不同的端口。

以下是我的配置文件:

域1

#This is for redirecting everyone from www.domain.com to domain.com
server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.domain1.com;

    return 301 $scheme://domain1.com$request_uri;
}

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        server_name domain1.com;

        location / {
                proxy_pass http://localhost:8081/;
                proxy_set_header  X-Real-IP  $remote_addr;
        }
}

DOMAIN2

#This is for redirecting everyone from www.domain.com to domain.com
server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.domain2.com;

    return 301 $scheme://domain2.com$request_uri;
}

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        server_name domain2.com;

        location / {
                proxy_pass http://localhost:8090/;
                proxy_set_header  X-Real-IP  $remote_addr;
        }
}

DOMAIN3

#This is for redirecting everyone from www.domain.com to domain.com
server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.domain3.com;

    return 301 $scheme://domain3.com$request_uri;
}

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        server_name domain3.com;

        location / {
                proxy_pass http://localhost:8091/;
                proxy_set_header  X-Real-IP  $remote_addr;
        }
}

所有这些配置文件(像往常一样)位于/etc/nginx/sites-available/,符号链接到/etc/nginx/sites-enabled/

当我添加第一个文件时,一切都作为魅力,当我添加第二个文件时它是相同的,添加第三个文件后nginx在我尝试service nginx reloadservice nginx restart时停止向我发送任何消息或service nginx start

它响应service nginx stopservice nginx status。显然这些功能起作用,但这些先决条件中的其他功能没有响应,也没有做任何事情。下面的输出只是从我的控制台复制粘贴。

# service nginx status
[ ok ] nginx is running.
# service nginx reload
# service nginx restart
# service nginx stop
[ ok ] Stopping nginx: nginx.
# service nginx start
# service nginx status
[FAIL] nginx is not running ... failed!
#

如果我删除符号链接到第三个文件,一切都会再次完美。

所以这是问题 - 我的设置有问题吗?还是我的逻辑?还是用nginx?

环境是:

- Debian Squeeze x64
- Nginx version 1.2.6

1 个答案:

答案 0 :(得分:1)

您遇到的症状表明您的第3个文件的nginx配置不正确,这就是重新加载失败的原因

nginx -t的输出应告诉您配置中的问题所在。