2

URL nginx return に既知のファイル拡張子を入れるたびに404 Not Found.

domain.com/myroute.fooanddomain.com/foo/myroute.fooは問題ありませんが、domain.com/myroute.phpand domain.com/foo/myroute.php(または .css、.js など) は を返します404 Not Found

私のnginxサーバー構成:

server {
        listen          80;
        server_name     domain.com;
        root            /var/www/path/public;

        charset utf-8;
        gzip on;

        #access_log  logs/host.access.log  main;

        location / {
                index index.html index.php index.htm;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        location ~ \.php$ {
                try_files                       $uri = 404;
                fastcgi_pass    unix:/var/run/php5-fpm.sock;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME  $request_filename;
                include         fastcgi_params;
        }

        location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
                add_header        Cache-Control public;
                add_header        Cache-Control must-revalidate;
                expires           7d;
                access_log off;
        }
}

既知のファイル拡張子 ( /myroute.php) を持つ URL が、index.php他の URL のように自分のファイルに移動しないのはなぜですか?

4

1 に答える 1

5

myroute.phpサーバーに存在しません。

Nginxlocationディレクティブはこの順序でチェックされます

  1. クエリに正確に一致する「=」プレフィックスを持つディレクティブ (リテラル文字列)。見つかった場合、検索は停止します。
  2. 従来の文字列を使用した残りのすべてのディレクティブ。この一致に「^~」プレフィックスが使用されている場合、検索は停止します。
  3. 構成ファイルで定義されている順序での正規表現。
  4. #3 が一致した場合は、その結果が使用されます。それ以外の場合は、#2 からの一致が使用されます

これは、myroute.phpリクエストが~ \.php$location ブロックによって処理されることを意味し、その結果、try_files ディレクティブに従って 404 が返されます。

これを解決するには、場所ディレクティブをより具体的にするか (例: ~ index\.php$)、またはlocation /. 書き換えを使用すると、問題を解決することもできます。

編集:

nginx が他のロケーション ブロックよりもロケーション ブロックを選択する順序を理解することが重要です。nginx wikiで詳細を参照してください

あなたの質問に関して、私が考える最も簡単な解決策は、try_filesを使用することです

    try_files $uri $uri/ /index.php?q=$uri&$args;

あなたlocation ~ \.php$ {location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {ブロックの両方で

  • 注:ブロックtry_files $uri =404から古いものを削除することを忘れないでください.php$

最終的な conf ファイルは次のようになります。

server {
    listen          80;
    server_name     domain.com;
    root            /var/www/path/public;

    charset utf-8;
    gzip on;

    #access_log  logs/host.access.log  main;

    location / {
            index index.html index.php index.htm;
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
            try_files $uri $uri/ /index.php?q=$uri&$args;
            fastcgi_pass    unix:/var/run/php5-fpm.sock;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME  $request_filename;
            include         fastcgi_params;
    }

    location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
            try_files  $uri $uri/ /index.php?q=$uri&$args;
            add_header        Cache-Control public;
            add_header        Cache-Control must-revalidate;
            expires           7d;
            access_log off;
    }
 }
于 2013-11-11T21:54:46.863 に答える