37

サーバーブロックの設定を間違えた可能性があるため、現在、すべての無効なページは 500 (Internal Server Error) です。

少し前に自分のウェブサイトを閉鎖することに決め、シンプルな 1 ページのサンキュー ホームページを作成しました。ただし、古いリンクや外部サイトは、サイトの他の部分にアクセスしようとしていますが、それらはもう存在しません。

ホームページ以外 (無効な URL) をすべて強制的にホームページにリダイレクトするにはどうすればよいですか?

次のブロックで試しましたが、うまくいきませんでした。

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

私の現在の構成は次のとおりです(現在、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;
    }
}
4

8 に答える 8

65

このようにエラーページをホームページに設定します

error_page 404 /index.html;

小さな問題があります。ホームページのステータス コードは「404 not found」になります。「200 ok」ステータス コードでホームページをロードしたい場合は、次のようにする必要があります。

error_page 404 =200 /index.html;

これにより、「404 not found」エラー コードが「200 ok」コードに変換され、ホームページが読み込まれます。

@jvperrin が言及した 2 番目の方法も良いですが、

try_files $uri $uri/ /index.html;

location /ただし、別の場所と一致せず、見つからないアセットはindex.html、欠落している画像、css、jsファイルなどもロードするため、1つのことを覚えておく必要がありますが、あなたの場合はすでに見ることができますアセットの拡張子に一致する別の場所があるため、この問題に直面することはありません。

于 2013-10-21T08:25:54.380 に答える
6

nginx がホストするサイトのこのソリューション:

仮想ホスティング ファイルを編集する

sudo nano /etc/nginx/sites-available/vishnuku.com 

このスニペットをサーバー ブロックに追加します

# define error page
error_page 404 = @notfound;

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

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

最終的なコードは次のようになります

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;
    }
}
于 2019-05-13T09:54:01.200 に答える
5

index定義の後に次の行を追加してみてください。

error_page 404 /index.html;

それでもうまくいかない場合は、try_files代わりに呼び出しを次のように変更してみてください。

try_files $uri $uri/ /index.html;

うまくいけば、これらのいずれかが機能することを願っています。まだテストしていません。

于 2013-10-21T06:16:30.013 に答える