2013-07-23 244 views
11

我在nginx服務器上有一個站點主機。以前使用try_files可以正常工作,以便在nginx站點配置中刪除index.phpNginx重寫子文件夾

但現在我要添加一個博客,其中的網址將是www.foo.com/blog,我可以訪問博客並使用index.php?p =確定。

但一旦我用漂亮的永久鏈接與Nginx的助手,www.foo.com/blog/2013/07/bar,就成了404

server { 
    # don't forget to tell on which port this server listens 
    listen 80; 

    # listen on the www host 
    server_name foo.com; 

    # and redirect to the non-www host (declared below) 
    return 301 $scheme://www.ultra-case.com$request_uri; 
} 

server { 
    # listen 80 default_server deferred; # for Linux 
    # listen 80 default_server accept_filter=httpready; # for FreeBSD 
    listen 80; 

    # The host name to respond to 
    server_name www.foo.com; 

    # Path for static files 
    root /web/foo.com 

    #index file 
    index index.php; 

    #Specify a charset 
    charset utf-8; 

    # Custom 404 page 
    error_page 404 /404.html; 

    # Uri Rewrite 

    location /blog { 
    index index.php; 
    try_files $uri $uri/ /blog/index.php?$args; 
    } 

    location/{ 
    autoindex on; 
    # This is cool because no php is touched for static content. 
    # include tihe "?$args" part so non-default permalinks doesn't break when using query string 
    try_files $uri $uri/ /index.php?$args; 
    } 
    location ~ \.php$ { 
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 
    include fastcgi.conf; 
    fastcgi_intercept_errors on; 
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php; 
    } 

    # Include the component config parts for h5bp 
    include conf/h5bp.conf; 
} 
+0

'根/ WEB/foo.com'分號失蹤 –

+0

@MohammadAbuShady UM ...對於我的錯誤感到抱歉,我想隱藏真實的服務器主機名和文件位置,這只是錯誤。 – Cauliturtle

回答

32

接受的答案通過index.php路由一切。這將打破包括某些腳本。 wp-admin腳本就是其中之一。

您可以使用:

location /blog/ { 
    index index.php; 
    try_files $uri $uri/ /blog/index.php?$args; 
} 
+0

非常感謝!我已經嘗試了其他解決方案,這個幫助我。 – Tico

+0

一年前,我發現這個問題仍然是積極的,我會投票這個答案將是最正確的一個 – Cauliturtle

+0

你的代碼對我完美的作品.. –

1

試試這個,我改變了我的答案,嘗試模仿你正在使用您的重寫相同的行爲。

location ~ /blog(.*) { 
    index index.php; 
    try_files $uri /blog/index.php?$1&$args; 
} 
+0

感謝您的回覆。但我遵循你的指示,它變成了'HTTP 500'。 – Cauliturtle

+0

@Cauliturtle嘗試這個不同的答案。 –

+0

然後,當我嘗試訪問/ blog/post-name時,Chrome會嘗試將該頁面作爲文件下載。 – Ryan

14

呃...謝謝你的評論和回答。但最後我用這種方法得到它的工作

location /blog { 
    index index.php; 
    rewrite ^/blog/(.*)+$ /blog/index.php?$1; # it finally works 
    # return 200 $request_uri; # it is for inspect what $request_uri is 
    # try_files $uri $uri/ /blog/index.php$request_uri$is_args$args; # it gets 500 server error 
} 

請指出目前的設置是否有任何問題。謝謝!

+0

我得到「這個網頁有一個重定向循環」。 – Ryan

+0

@Ryan從此服務器{}塊中刪除任何其他配置,因爲在此之後它仍會讀取它們 - 導致無限循環。我通過爲博客設置位置/博客{}和位置/ {}來解決主要索引問題。 –

+0

這沒有爲我正確傳遞變量。 'location/blog/{ try_files $ uri $ uri//blog/index.php?args; }'結束了工作 – Josh

5

我建議以下,趕下的子文件夾的任何固定鏈接/博客

location /blog { 
    index index.php; 
    try_files $uri $uri/ /blog/index.php?$args; 
} 
0

想爲PHP,重寫是沒有用這樣的需要:

location /app/ { 
    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME /path/to/your/app/index.php; 
    fastcgi_pass php; 
} 

隨着fastcgi通

upstream php { 
    server unix:/var/run/php5-fpm.sock; 
} 
0

試試這個

location /api { 
    # example: http://demo.com/api/channels/dmzb 
    root /data/webserver/demo.com/api/web; 
    rewrite ^/api/(.*) /$1 break; 
    try_files $uri $uri/ /api/index.php?$args; 

    location ~ ^/api/index\.php { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     include fastcgi.conf; 

     # fix request_uri 
     set $changed_request_uri $request_uri; 
     if ($changed_request_uri ~ ^/api(.*)) { 
      set $changed_request_uri $1; 
     } 
     fastcgi_param REQUEST_URI $changed_request_uri; 

     # fix script_filename 
     fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*); 
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
    } 
} 
0

在根漂亮的URL和一個子文件夾級別的通用解決方案:

set $virtualdir ""; 
set $realdir ""; 

if ($request_uri ~ ^/([^/]*)/.*$) { 
     set $virtualdir /$1; 
} 

if (-d "$document_root$virtualdir") { 
     set $realdir "${virtualdir}"; 
} 

location/{ 
     try_files $uri $uri/ $realdir/index.php?$args; 
}