2013-07-10 89 views
30

我是Nginx的新手,我试图让子域名工作。nginx - 两个子域配置

我想要做的就是把我的域(姑且称之为example.com),并添加:

  • sub1.example.com
  • sub2.example.com,并且也有
  • www.example.com可用。

我知道如何使用Apache来做到这一点,但Nginx是一个真正的头部划痕。

我运行Debian 6

我目前/etc/nginx/sites-enabled/example.com:

server { 
    server_name www.example.com example.com; 
    access_log /srv/www/www.example.com/logs/access.log; 
    error_log /srv/www/www.example.com/logs/error.log; 
    root /srv/www/www.example.com/public_html; 

    location/{ 
     index index.html index.htm; 
    } 

    location ~ \.php$ { 
     include /etc/nginx/fastcgi_params; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name; 
    } 
} 

它正在努力成为example.com和www.example。 COM。

我试图在同一文件中添加第二个服务器模块,如:

server { 
     server_name www.example.com example.com; 
     access_log /srv/www/www.example.com/logs/access.log; 
     error_log /srv/www/www.example.com/logs/error.log; 
     root /srv/www/www.example.com/public_html; 

     server { 
      server_name sub1.example.com; 
      access_log /srv/www/example.com/logs/sub1-access.log; 
      error_log /srv/www/example.com/logs/sub1-error.log; 
      root /srv/www/example.com/sub1; 
    } 
     location/{ 
      index index.html index.htm; 
     } 

     location ~ \.php$ { 
      include /etc/nginx/fastcgi_params; 
      fastcgi_pass 127.0.0.1:9000; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name; 
     } 
} 

没有运气。有任何想法吗?我非常感谢任何反馈。

+0

我应该提到:sub1.example.com的最终目标是转到example.com/sub1和sub2.example.com转到example.com/sub2。我希望这是有道理的。 – boredemt

回答

43

的错误是把服务器模块的服务器模块内,你应该关闭主服务器块,然后打开该子域

server { 
    server_name example.com; 
    # the rest of the config 
} 
server { 
    server_name sub1.example.com; 
    # sub1 config 
} 
server { 
    server_name sub2.example.com; 
    # sub2 config 
} 
4

你只需要添加下面一行代替一个新的您的server_name

server_name xyz.com *.xyz.com; 

并重新启动Nginx。而已。

+9

如果您希望子域指向服务器上的其他资源,而不是位置www.example.com的资源,该怎么办? – courtyen