如何强制将所有404(或每个页面,无论是否无效)重定向到主页?

hob*_*es3 30 redirect nginx http-status-code-404

目前,每个无效页面都是500(内部服务器错误),因为我可能搞砸了我的服务器块配置.

我决定暂时关闭我的网站,并创建了一个简单的单页感谢主页.但旧链接和外部网站仍在尝试访问网站的其他部分,这些部分已不复存在.

如何强制将所有非主页(任何无效的URL)重定向到主页?

我尝试使用以下块,但它不起作用:

location / {
    try_files $uri $uri/ $document_uri/index.html;
}
Run Code Online (Sandbox Code Playgroud)

我当前的配置是(我现在甚至不提供PHP文件,即主页是简单的html):

server {
    server_name www.example.com example.com;
    access_log /srv/www/example.com/logs/access.log;
    error_log /srv/www/example.com/logs/error.log;
    root /srv/www/example.com/public_html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ $document_uri/index.html;
    }

    # Disable favicon.ico logging
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    # Allow robots and disable logging
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Enable permalink structures
    if (!-e $request_filename) {
        rewrite . /index.php last;
    }

    # Handle php requests
    location ~ \.php$ {
        try_files $uri = 404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_send_timeout 900;
        fastcgi_read_timeout 900;
        fastcgi_connect_timeout 900;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # Disable static content logging and set cache time to max
    location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
        access_log off;
        log_not_found off;
        expires max;
    }

    # Deny access to htaccess and htpasswd files
    location ~ /\.ht {
        deny  all;
    }

    # Deny access to hidden files (beginning with a period)
    location ~ /\. {
        access_log off; log_not_found off; deny all;
    }
}
Run Code Online (Sandbox Code Playgroud)

Moh*_*ady 45

像这样将错误页面设置为主页

error_page 404 /index.html;
Run Code Online (Sandbox Code Playgroud)

有一个小问题,主页的状态代码将是"404 not found",如果你想加载一个"200 ok"状态代码的主页,你应该这样做

error_page 404 =200 /index.html;
Run Code Online (Sandbox Code Playgroud)

这会将"404 not found"错误代码转换为"200 ok"代码,并加载主页

@jvperrin提到的第二种方法也很好,

try_files $uri $uri/ /index.html;
Run Code Online (Sandbox Code Playgroud)

但你需要记住一件事,因为它是location /任何与另一个位置不匹配但未找到的资产也会加载index.html,例如缺少图像,css,js文件,但在你的情况下我可以看到你有另一个匹配资产扩展的位置,所以你不应该面对这个问题.

  • 我想不出你需要"覆盖"真正的http状态代码的情况.如果找不到页面,则应返回404状态代码. (2认同)

小智 14

要获得真正的重定向,您可以这样做:

在serverblock中定义要重定向的错误页面,如下所示:

# define error page
error_page 404 = @myownredirect;
error_page 500 = @myownredirect;
Run Code Online (Sandbox Code Playgroud)

然后定义该位置:

# error page location redirect 302
location @myownredirect {
  return 302 /;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,错误404和500生成HTTP 302(临时重定向)到/(当然可以是任何URL)

如果您使用fast-cgi for php或其他这些块必须添加以下内容以将错误"upstrem"发送到server-block:

fastcgi_intercept_errors on;
Run Code Online (Sandbox Code Playgroud)


Vis*_*hnu 6

nginx 托管站点的此解决方案:

编辑您的虚拟主机文件

sudo nano /etc/nginx/sites-available/vishnuku.com 
Run Code Online (Sandbox Code Playgroud)

在服务器块中添加此代码段

# define error page
error_page 404 = @notfound;

# error page location redirect 301
location @notfound {
    return 301 /;
}
Run Code Online (Sandbox Code Playgroud)

在您的 php 块中,将 fastcgi_intercept_errors 设置为 on

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    # intercept errors for 404 redirect
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Run Code Online (Sandbox Code Playgroud)

最终代码将如下所示

server {
    listen 80;
    server_name vishnuku.com;

    root /var/www/nginx/vishnuku.com;
    index index.php index.html;

    access_log /var/log/nginx/vishnuku.com.log;
    error_log /var/log/nginx/vishnuku.com.error.log;

    location / {
        try_files $uri $uri/ /index.php?$args /;
    }

    # define error page
    error_page 404 = @notfound;

    # error page location redirect 301
    location @notfound {
        return 301 /;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /nginx.conf {
        deny all;
    }
}
Run Code Online (Sandbox Code Playgroud)


jvp*_*rin 5

尝试在index定义之后添加以下行:

error_page 404 /index.html;
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请尝试将您的try_files呼叫更改为以下内容:

try_files $uri $uri/ /index.html;
Run Code Online (Sandbox Code Playgroud)

希望其中一件适合您,我也没有测试过。