我在使用 nginx 的虚拟主机时遇到问题,无论我做什么,我都会访问 www/html 而不是 www/example1 文件夹。任何人都可以找出问题所在吗?我错过了什么吗?
pi@homeserver:/etc/nginx/sites-enabled $ ls -l
total 4
-rw-r--r-- 1 root root 467 Sep 4 19:41 default
lrwxrwxrwx 1 root root 38 Sep 4 19:43 example1 -> /etc/nginx/sites-available/example1
默认文件是
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~\.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
try_files $uri =404;
include fastcgi_params;
}
}
example1 文件是
server {
listen example1.com:80;
root /var/www/example1.com;
index index.php index.html index.htm;
server_name example1.com www.example1.com;
location / {
try_files $uri $uri/ =404;
}
location ~\.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
try_files $uri =404;
include fastcgi_params;
}
}
我在日志中收到此错误
2016/09/04 21:41:08 [emerg] 1788#0: invalid host in "http://www.example1.com:80" of the "listen" directive in /etc/nginx/sites-enabled/example1.com:2
答案1
从错误消息来看,似乎nginx
很难将语句解析listen example1.com:80;
为 IP 地址。
listen
如果您有一台多宿主服务器并希望将服务限制在一个接口上,则为指令提供 IP 地址非常有用。
在大多数情况下,该声明listen 80;
就足够了。我建议您对两个服务器块使用以下内容:
server {
listen 80;
...
}
看这个文件了解更多。