2017-02-04 17 views
1

サイト内の複数の専用ルートを、同じ名前ではない特定のディレクトリにネストしたいとします。私はそれがtry_filesのために使用するパスの書き換えを行う方法を理解できません。Nginxのネストされた場所のルートリライト

server {   
    listen 80 default_server; 
    listen [::]:80 default_server; 

    root /var/www/default; 

    index index.html; 

    server_name _; 

    if ($bad_referer) { 
     return 444; 
    } 

    location/{ 
     try_files $uri $uri/ =404; 
    } 

    location /postfixadmin/ { 
     access_log /var/log/nginx/postfixadmin/access.log; 
     error_log /var/log/nginx/postfixadmin/error.log; 

     root /var/www/postfixadmin/; 
     index index.php index.html index.htm; 
     try_files $uri $uri/ /index.php; 


     location ~ \.php$ { 
      include fastcgi.conf; 
      fastcgi_index index.php; 
      fastcgi_intercept_errors on; 
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     } 

     location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ { 
      expires 168h; 
      add_header Pragma public; 
      add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
     } 
    } 

    location /email/ { 

     #access_log /var/log/nginx/roundcube/access.log; 
     #error_log /var/log/nginx/roundcube/error.log; 

     root /var/www/roundcube/; 
     index index.php index.html index.htm; 
     try_files $uri $uri/ /index.php; 

     location ~ \.php$ { 
      include fastcgi.conf; 
      fastcgi_index index.php; 
      fastcgi_intercept_errors on; 
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     } 

     location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ { 
      expires 168h; 
      add_header Pragma public; 
      add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
     } 
    } 
} 

私はwww.site.com/emailに移動すると、私は404を受け、私は存在しない、それは/var/www/roundcube/email/index.phpを探しているので、それがあると仮定しています。 try_filesの前にファイルパスを書き換えるために何をする必要がありますか?

+0

あなたはどこを見れするのnginxを伝えるために、ルートまたはエイリアスディレクティブを使用することができます。ここでは

は、作業のソリューションです。 –

答えて

1

私は解決策を考え出しました。それはかなり単純です。 aliasを使用すると、rootでは、locationに一致する部分の後の文字列のセクションのみが使用されるため、正しいディレクトリを探していました。もう1つの問題は、PHPが正しいスクリプト名を渡されていないため、間違った場所で探していたことです。解決策はfastcgi_param SCRIPT_FILENAME $request_filename;に合格することでした。私はtry_filesセクションも取り除くことができましたが、私は100%確かな理由はありません。

server {   
    listen 80 default_server; 
    listen [::]:80 default_server; 

    root /var/www/default; 

    index index.html; 

    server_name _; 

    if ($bad_referer) { 
     return 444; 
    } 

    try_files $uri $uri/ =404; 

    location /postfixadmin { 
     alias /var/www/postfixadmin/; 
     index index.php index.html index.htm; 

     location ~ /postfixadmin/.+\.php$ { 
      include fastcgi.conf; 
      fastcgi_index index.php; 
      fastcgi_intercept_errors on; 
      fastcgi_param SCRIPT_FILENAME $request_filename; 
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     } 

     location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ { 
      expires 168h; 
      add_header Pragma public; 
      add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
     } 
    } 

    location /email { 
     alias /var/www/roundcube/; 
     index index.php index.html index.htm; 

     location ~ /email/.+\.php$ { 
      include fastcgi.conf; 
      fastcgi_index index.php; 
      fastcgi_intercept_errors on; 
      fastcgi_param SCRIPT_FILENAME $request_filename; 
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     } 

     location ~* \.(css|js|gif|jpe?g|png|woff|woff2|ttf|eot|svg|ico)$ { 
      expires 168h; 
      add_header Pragma public; 
      add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
     } 
    } 
} 
関連する問題