How do I force redirect all 404's (or every pa

2019-01-21 19:46发布

Currently every invalid page is 500 (Internal Server Error) because I probably messed up with my server block configuration.

I decided to shut down my website a while ago and created a simple one-page, thank-you homepage. However old links and external sites are still trying to access other parts of the site, which no longer exists.

How do I force redirect all non-homepage (any invalid URL) to the homepage?

I tried with the following block, but it didn't work:

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

My current configuration is (I don't even serve PHP files right now, ie homepage is simple 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;
    }
}

4条回答
贪生不怕死
2楼-- · 2019-01-21 20:13

To get a true redirect you could do this:

in serverblock define the error-page you want to redirect like this:

# define error page
error_page 404 = @myownredirect;
error_page 500 = @myownredirect;

Then you define that location:

# error page location redirect 302
location @myownredirect {
  return 302 /;
}

In this case errors 404 and 500 generates HTTP 302 (temporary redirect) to / (could of course be any URL)

If you use fast-cgi for php or other these blocks must have the following added to send the errors "upstrem" to server-block:

fastcgi_intercept_errors on;
查看更多
Fickle 薄情
3楼-- · 2019-01-21 20:17

Setting the error page to the home page like this

error_page 404 /index.html;

has a small problem, the status code of the home page will be "404 not found", if you want to load the home page with a "200 ok" status code you should do it like this

error_page 404 =200 /index.html;

This will convert the "404 not found" error code to a "200 ok" code, and load the home page

The second method which @jvperrin mentioned is good too,

try_files $uri $uri/ /index.html;

but you need to keep 1 thing in mind, since it's the location / any asset that doesn't match another location and is not found will also load the index.html, for example missing images, css, js files, but in your case I can see you already have another location that's matching the assets' extensions, so you shouldn't face this problem.

查看更多
迷人小祖宗
4楼-- · 2019-01-21 20:17

Must use

"fastcgi_intercept_errors on;"

along with the custom redirect location like error_page 404 =200 /index.html or

as above

location @myownredirect {
  return 302 /;
}
查看更多
仙女界的扛把子
5楼-- · 2019-01-21 20:22

Try adding the following line after your index definition:

error_page 404 /index.html;

If that doesn't work, try changing your try_files call to the following instead:

try_files $uri $uri/ /index.html;

Hopefully one of those works for you, I haven't tested either yet.

查看更多
登录 后发表回答