2017-04-13 21 views
1

Nginxの最新の安定版をGNU/Linux OSに実行しています。次の仮想ホストを持っています。setup custom localized 404 error pages avoiding ifに行っていますが、いつもリダイレクトループで終わります。URLパラメータを使用してロケールに基づいてnginxのカスタム404エラーページを設定してください

ここまでは、次のロケールesencaしか考えていませんが、これはURLパラメータとして常にドメインの隣にあります。以下のようなexpected URLs are何か:

http://www.example.com/ca 
http://www.example.com/ca/home 
http://www.example.com/en 
http://www.example.com/en/contact 

これは私のnginxのサーバーのブロックです:

server { 
     listen   80; 
     listen   [::]:80; 
     server_name  example.com; 
     root   /var/www/html/example_com; 
     # By default, show Castilian index 
     index   es/index.html; 

     access_log  /var/log/nginx/example_com.access.log main; 
     error_log  /var/log/nginx/example_com.error.log error; 

     #Remove HTML Extension 
     rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent; 

     # Remove trailing slash 
     rewrite ^/(.*)/$ /$1 permanent; 

     # Make sure Nginx knows what files to look for, and for that we use the try_files directive. 
     # Look for a file with the current $uri and an .html extension, and if no file exists, 
     # check for a directory with that name and serve the index. Otherwise, render a 404 error. 
     try_files $uri/index.html $uri.html $uri/ $uri =404; 

     location = /form/contact.php { 
       deny all; 
       return 404; 
     } 

     # Custom error pages 
     set $error404 /en/404.html; 

     location /ca { 
      set $error404 /ca/404.html; 
     } 

     error_page 404 =404 $error404; 


     location = /form/contact { 
       try_files $uri.php =404; 

       include /etc/nginx/fastcgi_params; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       send_timeout 1800; 
       fastcgi_read_timeout 1800; 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
     } 

     location ~ /\.ht { 
      deny all; 
     } 

     location ~ /(config) { 
       deny all; 
       return 404; 
     } 
    } 

答えて

1

あなたはそのような場所のブロックを使用することはできません、あなたは他の場所のブロックをすべて解除されます。詳細は、how nginx processes a requestを参照してください。

簡単な方法があります。名前付きの場所を使用して404応答を処理し、元のURIを必要なエラーページに書き換えます。例えば

error_page 404 @error404; 

location @error404 { 
    rewrite ^/(en|es|ca) /$1/404.html last; 
    rewrite^/en/404.html last; 
} 

は、より多くのためにthis documentを参照してください。

+0

お手数をおかけして、効率的なソリューションを提供していただきありがとうございます。また、リンクをメモしましたが、私はすでに多くを読んでいましたが、初めての解決策は見つけにくいです:) – user846226

+0

次のURL [ ]ので、次の場所ブロックをnginx設定ファイルに追加しました。これまでのところ、間違いなく、言語ロケールが繰り返される次のようなURLを許可する以外は正常に動作します[5] [6]。あなたはそれを修正するために私を助けてくれますか?私はここに私が出てきたロケーションブロックを貼り付けています。 [1] http://www.example.com/en/oil [2] http://www.example.com/en/oil-paint [3] http://www.example.com/es/oleo [4] http://www.example.com/es/pintura-al-oleo [5] http://www.example.com/es/es/es/es/es/pintura-al-oleo [6] ] http://www.example.com/en/en/oil – user846226

+0

これは私が使用しているロケーションブロックです。goo.gl/rd221F – user846226

関連する問題