无法让 Nginx 上的虚拟主机(服务器块)工作

1 linux configuration nginx

我刚开始使用 Nginx 正在尝试使用虚拟主机,但无法理解我的配置有什么问题......

以下是启用站点的文件夹的歌词:

root@cavalier:/etc/nginx/sites-enabled# ls -hal
total 12K
drwxrwxr-x 2 root vnc  4.0K Jul 28 18:06 .
drwxr-xr-x 5 root root 4.0K Jul  1 16:58 ..
-rw-r--r-- 1 root root  360 Jul 28 18:07 default
lrwxrwxrwx 1 root root   31 Jul 28 18:01 example1.com -> ../sites-available/example1.com
lrwxrwxrwx 1 root root   33 Jul 28 17:57 example2.com -> ../sites-available/example2.com
Run Code Online (Sandbox Code Playgroud)

默认服务器块的内容:

root@cavalier:/etc/nginx/sites-enabled# cat default
server {
listen 80 default_server;

root /www/example.com;
index index.html index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)

实施例1的服务器块:

root@cavalier:/etc/nginx/sites-enabled# cat example1.com
server {
listen example1.com:80;
server_name www.example1.com example1.com;

root /www/example1.com;
index index.html index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)

例题服务器块:

root@cavalier:/etc/nginx/sites-enabled# cat example2.com
server {
server_name example2.com;

root /www/example2.com;
index index.php;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,nginx 只为所有域提供来自 example1.com 的文件。它甚至无视默认服务器配置并直接进入example1。

对不起,我是 nginx 的新手,并且主要使用 Apache。我猜这应该相对简单,我只是遗漏了一些东西......不过我已经花了几个小时试图弄清楚,任何帮助将不胜感激。

Ale*_*Ten 6

Server1 与请求最匹配:IP:port,而另外两个只匹配端口。所以 nginx 为所有到达这个 IP:port 对的请求选择 server1