为多个域设置 Nginx

Setting up Nginx for multiple domains

我查看了之前发布的一些与我的问题类似的问题,但其中 none 与我的问题有关。

在我的 Nginx 配置中,我进行了以下设置:

server {
    listen 80;
    server_name (www.)?domain1.com;
    root /etc/nginx/www/domain1.com;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

server {
    listen 80;
    server_name (www.)?domain2.com;
    root /etc/nginx/www/domain2.com;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

但出于某种原因,这两个域似乎都指向 /etc/nginx/www/domain2.com。我可能做错了什么?

您在 server_name 指令中有错误。

正则表达式服务器名称必须以 ~.

为前缀

不过用起来会好很多

server_name domain1.com www.domain1.com;

http://nginx.org/r/server_name