2017-03-21 4 views
1

私はnginxの設定ファイルに次のブロックを持っている:予期しないnginxの行動

location ~ /$ { 
    index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml; 
} 

location /cat { 
    try_files /index.php?url=$uri =404; 
} 

私はこれが何を期待するもの:http://www.example.com/cat<whatever>に送信されたすべての要求は、に設定GET変数のURLでindex.phpに送信されますuriを要求する。代わりに書き換えログに基づいて何が起こるかは、location /catブロックがまったくヒットしないということです。私はhttp://www.example.com/cart/testing/を要求したときにここでは書き換えログから関連の抜粋です:

2017/03/21 00:32:49 [error] 25711#0: *20566477 "/var/www/vhosts/example.com/httpdocs/cat/testing/index.html" is not found (2: No such file or directory), client: <ip redacted>, server: example.com, request: "GET /cat/testing/ HTTP/1.1", host: "www.example.com" 

要求に関連する他のエントリはありません。

nginxは正規表現を含む任意のロケーションブロック(すなわちlocation ~ /$)の前に、nginxがプレフィックスロケーションブロック(つまり、location /cat)を処理することを理解しました。だから私はこの振る舞いに困惑している。

答えて

1

の理解は間違っています。詳細はthis documentを参照してください。

また、お客様のtry_files指示は間違っています。 /index.php?url=$uriまたは=404は、ステートメントの最後の要素にする必要があります。詳細については、this documentを参照してください。あなたはおそらくしたい

:他の場所のブロックの目的の

index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml; 

location /cat { 
    try_files $uri $uri/ /index.php?url=$uri; 
} 

わからない、indexディレクティブとして、既に末尾/で任意のURIを処理しようとしています。詳細はthis documentを参照してください。