【问题标题】:Debian 8 nginx configurationDebian 8 nginx 配置
【发布时间】:2018-01-07 10:47:04
【问题描述】:

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

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

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

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;
        }
}

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

【问题讨论】:

    标签: nginx configuration debian webserver debian-based


    【解决方案1】:

    如果在 location 块中找不到任何内容,您必须明确让 nginx 知道您要抛出 404 错误。否则它将尝试匹配您的“默认”服务器块(在这种情况下,它似乎是 api.example.com 之一,因为您没有指定默认值)。它也无法在该服务器中找到任何内容,然后尝试 404。

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

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

    【讨论】:

    • 将其添加到我的 wwwexamplecom nginx 配置后,它并没有解决我的问题。
    • 尝试将=404 添加到try_files 部分的末尾,例如:try_files $uri $uri/ ..... =404
    • 好的,现在可以了,但我不知道为什么我必须在 try_files 末尾添加 =404
    • 改变了答案,让我知道这是否能更好地解释事情
    猜你喜欢
    • 2021-05-11
    • 2018-10-03
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多