60

ドメイン名に index.html を設定する方法 (例: https://www.example.com/ ) - ユーザーをルート ディレクトリの index.html に誘導します。

私は次のようなさまざまなことを試しました:

server {
    # some configs

    location = / {
            index index.html;
            fastcgi_index index.html;
    }
or 
    location / {
            index index.html;
            fastcgi_index index.html;
    }

}

何も助けてくれませんでした。

location キーワードを使用した構成が他にもいくつかありますが、コメントしたこともあります。

server {句内のその他の「場所」構成:

location ~ .*(css|htc|js|bmp|jp?g|gif|ico|cur|png|swf|htm?|html)$ {
        access_log off;
        root $www_root;
}

location ~ \.php$
{
        include                         /etc/nginx/fastcgi_params;
        index                           index.html;
        fastcgi_index                   index.html;
        fastcgi_param SCRIPT_FILENAME   $www_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING      $query_string;
        fastcgi_param PATH_INFO         $fastcgi_path_info;
        fastcgi_pass                    127.0.0.1:9000;
        # Директива определяет что ответы FastCGI-сервера с кодом больше или равные 400
        # перенаправлять на обработку nginx'у с помощью директивы error_page
        fastcgi_intercept_errors        on;
        break;
}

location ~ /\.ht {
    deny all;
}

それらはすべてコメントされ、コメント解除されましたが、何も役に立ちませんでした。

PS Edition は /etc/nginx/sites-enabled/domainname.com ファイルで作成されました。

4

5 に答える 5

59

あなたのロケーションブロックでは、次のことができます:

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

これにより、最初に指定された正確な名前のファイルを探すよう ngingx に指示され、そのようなファイルが見つからない場合は、uri/index.html が試行されます。したがって、https://www.example.com/へのリクエストが来ると、最初に完全に一致するファイルが検索され、見つからない場合は index.html がチェックされます。

于 2012-08-14T17:49:55.603 に答える
44

location / {最も一般的な場所です ( 付きlocation {)。AFAIU、何にでも一致します。location / { index index.html; }サイトのすべてのサブディレクトリに重複するコンテンツが多数あるため、これが役立つとは思えません。

とのアプローチ

try_files $uri $uri/index.html index.html;

上記のコメントで述べたように、index.htmlサイトに存在しないはずのページを返すため、悪いです(可能なものはすべて$uriその中に入ります)。また、上記の回答で述べたように、の最後の引数に内部リダイレクトがありtry_filesます。

あなたのアプローチ

location = / {
   index index.html;

index内部リダイレクトも行うため、これも悪いことです。それが必要な場合は、特定の でそれを処理できるはずですlocation。作成例

location = /index.html {

ここで提案されたように。しかし、その後、望ましくない可能性がある作業リンクhttp://example.org/index.htmlが作成されます。私が使用する別のバリ​​アントは次のとおりです。

root /www/my-root;

# http://example.org
# = means exact location
location = / {
    try_files /index.html =404;
}

# disable http://example.org/index as a duplicate content
location = /index      { return 404; }

# This is a general location. 
# (e.g. http://example.org/contacts <- contacts.html)
location / {
    # use fastcgi or whatever you need here
    # return 404 if doesn't exist
    try_files $uri.html =404;
}

PS nginx のデバッグは非常に簡単です (バイナリで許可されている場合)。server {ブロックに追加するだけです:

error_log /var/log/nginx/debug.log debug;

すべての内部リダイレクトなどが表示されます。

于 2015-10-30T16:18:21.353 に答える
21

答えは、ルート ディレクトリを場所ディレクティブに配置することです。

root   /srv/www/ducklington.org/public_html;
于 2012-08-14T14:42:08.907 に答える
1

私にとって、try_files(現在最も投票されている)回答https://stackoverflow.com/a/11957896/608359のディレクティブは、書き換えサイクルにつながりました。

*173 rewrite or internal redirection cycle while internally redirecting

index ディレクティブの方がうまくいきました。名前の前にスラッシュを使用したことに注意してください。これは、必要な場合とそうでない場合があります。

server {
  listen 443 ssl;
  server_name example.com;

  root /home/dclo/example;
  index /index.html;
  error_page 404 /index.html;

  # ... ssl configuration
}

この場合、404 を返す場合を含め、すべてのパスが /index.html につながるようにしました。

于 2019-02-10T12:31:40.257 に答える