Debian 8 nginx configuration



在我的Nginx网络服务器上,我有几个这样的虚拟主机: - api.example.com - www.example.com - cv.example.com

但是当我访问 www.example.com/example 并且这不是一条有效的路径时,它给了我 404 页的 api.example.com。但是为什么?

这是我目前的 nginx 配置 www.example.com:

server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/www.example.com-access.log timed;
error_log /var/log/nginx/www.example.com-error.log;
root /var/www/wwwexamplecom/html/_site;
server_name example.com www.example.com;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param  SCRIPT_FILENAME /var/www/example/html/public/index.php;
include fastcgi_params;
}
location ~ /.well-known {
allow all;
}
}

这是我 api.example.com 的配置:

server {
listen 443 ssl;
listen [::]:443 ssl;
access_log /var/log/nginx/api.example.com-access.log timed;
error_log /var/log/nginx/api.example.com-error.log;
root /var/www/apiexamplecom/html/public;
server_name api.example.com;
include snippets/ssl-api.example.com.conf;
include snippets/ssl-params.conf;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param  SCRIPT_FILENAME /var/www/apiexamplecom/html/public/index.php;
include fastcgi_params;
}
location ~ /.well-known {
allow all;
}
}

我认为自己在/位置部分,但我并不是真正如何解决这个问题。这在其他虚拟主机上也发生。

你必须明确地让nginx知道,如果它在位置块中找不到任何内容,你想抛出一个404错误。否则,它将尝试匹配您的"默认"服务器块(在这种情况下,这似乎是 api.example.com,因为您尚未指定默认值(。它也在该服务器中找不到任何内容,然后尝试 404。

要明确告诉nginx在您的位置块中找不到任何内容时抛出404,请在try_files行的末尾添加=404。这意味着如果它找不到任何$uri$uri/或任何 php 文件的文件,那么抛出 404 而不尝试其他任何东西。

try_files $uri $uri/ /index.php?q=$uri&$args =404;

最新更新