nginx 中 404、50x 错误处理 HTML 页面未被检测到

nginx 中 404、50x 错误处理 HTML 页面未被检测到

nginx 版本:nginx/1.18.0

这是我的“nginx”conf默认的“vhost”:

cat /etc/nginx/sites-enabled/000-default
server {
    listen 80;
    listen [::]:80;

    server_name example.org;
    return 301 https://$server_name$request_uri;
}

# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    index index.html;

    server_name example.org www.example.org;
    root /home/www/example.org;

    include /etc/nginx/ssl.conf;

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }

    location / {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }

    error_page   500 502 503 504  /50x.html;
    error_page   404  /404.html;

    location = /50x {
         root   /home/www/example.org;
         # rewrite foobar.html to foobar, foobar.php to foobar
         if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
         try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    location = /404 {
         root   /home/www/example.org;
         # rewrite foobar.html to foobar, foobar.php to foobar
         if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
         try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    access_log  /var/log/nginx/example.org.access.log;
}

我的根目录中有“404.html”和“50x.html”文件。

如果我直接打电话给他们,这是成功的,但如果我尝试获取一个未知的页面,如https://example.org/non-existant,我得到的是File not found字符串而不是我的 HTML 页面。

有什么帮助吗?

短暂性脑缺血发作

答案1

您的配置文件有错误,请更新 404 和 50x 错误页面的位置块

只需替换此块:

location = /50x {
    root   /home/www/example.org;
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
}
location = /404 {
    root   /home/www/example.org;
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
}

location = /50x.html {
    root /home/www/example.org;
    internal;
}
location = /404.html {
    root /home/www/example.org;
    internal;
}

然后您可以重新加载 nginx,sudo nginx -s reload它应该可以正常工作。

答案2

我需要做什么才能使其工作:存在冲突,我需要明确设置已知路径,我想将 index.html index.php 重写为索引(等等):

cat /etc/nginx/sites-enabled/000-default
server {
    listen 80;
    listen [::]:80;

    server_name example.org;
    return 301 https://$server_name$request_uri;
}

# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    index index.html;

    server_name example.org www.example.org;
    root /home/www/example.org;

    include /etc/nginx/ssl.conf;

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }

    location = /known {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    location = /also-known {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    location = /also-also-known {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }

    error_page   500 502 503 504  /50x.html;
    error_page   404  /404.html;

    location = /50x {
         root   /home/www/example.org;
         internal;
    }
    location = /404 {
         root   /home/www/example.org;
         internal;
    }

    access_log  /var/log/nginx/example.org.access.log;
}

相关内容