我刚刚开始使用 Nginx,正在尝试使用虚拟主机,但无法弄清楚我的配置出了什么问题......
以下是 sites-enabled 文件夹的内容:
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
的内容默认服务器块:
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;
}
}
这示例 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;
}
}
这示例2服务器块:
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;
}
}
由于某种原因,nginx 仅为所有域提供来自 example1.com 的文件。它甚至无视默认服务器配置,直接转到 example1。
抱歉,我是 nginx 新手,大部分时间都在使用 Apache。我猜这应该比较简单,只是我遗漏了一些东西... 我花了好几个小时才弄清楚,任何帮助我都会很感激。
答案1
Server1 与请求最匹配:IP:端口,而其他两个仅匹配端口。因此,nginx 为所有到达此 IP:端口对的请求选择 server1