2017-07-22 63 views
0

目前,我有一個碼頭容器中的nginx/php-fpm設置,以及在容器中作爲卷裝入的wordpress文件夾。我希望xyz.com/blog和xyz.com/blog/的功能完全相同。 我有以下nginx的配置:nginx重寫規則爲尾隨斜線加載fpm/wordpress

server { 
     listen  80 default_server; 
     server_name 0.0.0.0; 
     root   /mnt/blog; 

    # Load configuration files for the default server block. 
    include /etc/nginx/default.d/*.conf; 
    location/{ 
     if (!-e $request_filename){ 
      rewrite ^(.*)$ /index.php last; #converted from .htaccess 
     } 
    } 
    location ~ .php$ { 

     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 
} 

同時擊中xyz.com/blog/ - WordPress網站被加載預期,但是當我刪除了結尾的斜線(/) - xyz.com/blog,我得到301 Moved Permanently錯誤。我如何確保xyz.com/blogxyz.com/blog/行爲正確並加載wordpress網站?

回答

0

你想/blog調用/index.php沒有首先重定向到/blog/

根據您現有的解決方案,您可以將if (!-e ...更改爲if (!-f ...。詳情請見this document

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

詳見this document

然而,相同的功能可以實現。

+0

感謝您的回覆。我嘗試了以下內容: location =/blog { if(!-f $ request_filename){ rewrite ^(。*)$ $ 1/index.php break; } } 現在的問題是,index.php現在加載爲一個html文件,而不是由fastcgi/fpm處理。 – Cheezo

+0

我也試過了:'location =/blog {{{0}} {0} try_files $ uri $ uri//index.php; }' 但我得到301錯誤 - 永久移動。 – Cheezo