系统相关
首页 > 系统相关> > php – Nginx不显示404页面,而是在root中提供索引文件

php – Nginx不显示404页面,而是在root中提供索引文件

作者:互联网

我的Nginx服务器没有显示我的404页面.相反,每当尝试访问不存在的页面或目录时,它只是在我的web文件夹的根目录中提供我的索引(.php)(没有相应的样式表).

这是我在/ etc / nginx / sites-available下的’默认’文件:

server {
listen 80;
listen [::]:80 ipv6only=on;
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;

add_header Strict-Transport-Security max-age=15768000;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4';
ssl_prefer_server_ciphers on;

root /usr/share/nginx/html;
index index.html index.htm;

location / {
    index index.php;
}

if (!-e $request_filename) {
    rewrite ^.*$/index.php last;
}

try_files $uri $uri/ =404;

error_page 403 404 405 /error/404.html;
error_page 500 501 502 503 504 /error/50x.html;

location ^~ /error/ {
    internal;
    root /usr/share/nginx/html;
}

location ~ \.php${
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}

毫无疑问,这对我来说是新手并且过多地摆弄它,所以这里也可能存在其他问题(关于如何 – 关于如何 – 一旦其余部分得到修复 – 我可以强制HTTPS连接会膨胀!).感谢帮助和建设性的意见,谢谢!

解决方法:

如果文件不存在,你将重写为index.php,所以它永远不会进入你的try_files或errorpage ……

if (!-e $request_filename) {
    rewrite ^.*$/index.php last;
}

^应该被删除,除非你有特定的目的

标签:nginx,php,ubuntu-server
来源: https://codeday.me/bug/20190624/1279040.html