2013-03-31 156 views
0

我花了很多年的時間在谷歌上搜索任何信息,並且問了一些人(在任何人建議我去之前)。Nginx重寫404

我的nginx.conf中不能正常工作的位在下方。 什麼工作:重寫爲BlogHome,Home和About。

什麼不工作 - 重寫爲C_ReadBlogURL和C_ReadAllPosts。由於某些原因,這兩個都是404,儘管路徑是正確的。我不明白爲什麼 - 而且我一直在困惑這一整天。我認爲這可能與他們作爲php文件有關,但我不知道。

任何幫助,將不勝感激:)

server { 
listen 80; 


server_name blog.example.com; 

root /usr/share/nginx/www/example; 
index /views/Read/BlogHome.php; 


location/{ 
    rewrite ^/?$ /views/Read/BlogHome.php last; break; 
    rewrite ^/(.+)/?$ /controllers/read/C_ReadBlogURL.php?url=$1 last; break; 
} 
location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
} 
} 

server { 
listen 80; 
server_name example.com; 

root /usr/share/nginx/www/example; 
index /controllers/read/C_ReadLatestPost.php; 

location ~ ^(/posts\.php) { 
    rewrite ^(/posts\.php) /controllers/read/C_ReadAllPosts.php?type=$arg_type last; break; 
} 

location ~ ^/?$ { 
    rewrite ^/?$ /controllers/read/C_ReadLatestPost.php last; break; 


} 

location ~ ^(/about)/?$ { 
    rewrite ^(/about)/?$ /views/Read/About.php last; break; 
} 

location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
} 

} 

回答

0

刪除所有 「破發」;在你的每個重寫規則中。它不屬於這裏。第一次「休息」後的任何重寫規則指令將被忽略。不要以爲這就是你想要的。

參考:http://wiki.nginx.org/HttpRewriteModule#break

[UPDATE基於來自下面的評論nginx的配置文件。

請注意,「break directive與」rewrite ... break「不同;我的主要變化是將2個規則移動到php位置塊中,並用'break'替換'last',使其不會觸發另一輪到位置搜索。

你的第二個重寫規則是錯誤的(在正則表達式中使用「[]」與「()」不同)我的理解是你想匹配所有剩下的PHP腳本。

我還從「位置/」塊中刪除了另一個「break」的外觀,你可能只想放一個'break'指令的IF塊中。除此之外,我沒有看到該指令的任何實際應用。

[UPDATE2同樣是有意義的一切移動到「位置/」塊爲好。

server { 
    listen 80; 
    server_name blog.example.com; 
    root /usr/share/nginx/www/example; 
    index /views/Read/BlogHome.php; 

    location/{ 
     rewrite ^(/posts\.php) /controllers/read/C_ReadAllPosts.php?type=$arg_type break; 
     rewrite ^/?$ /views/Read/BlogHome.php break; 
     rewrite^/controllers/read/C_ReadBlogURL.php?url=$uri break; 

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

您好 - 感謝您的回答。 重寫^(/ posts.php)/controllers/read/C_ReadAllPosts.php?type=$arg_type last; break;重寫^ /(。+)/?$ /controllers/read/C_ReadBlogURL.php?url=$1 last;打破; 如果我把任何一個放在前面 - 那麼最上面的那個會工作,而下面的不會。如果我抽出時間,那麼它默認爲最後一個。我不知道發生了什麼事情:( – user1438377

+0

我甚至讓下面的規則更詳細:[^ \。php] - 它仍然沒有幫助。我不明白爲什麼兩個規則不能一起工作。雖然 – user1438377

+0

您需要刪除那些「break」,當然,請注意,您的第二個正則表達式匹配任何非空的uri,但第一個正則表達式只匹配給定的字符串「/posts.php」,所以我認爲您的請求?可能沒有「/posts.php」,在它的URI您使用的同時,跳出後,以測試規則,要求 –

0

的()是不是在第一個規則必要的,但是,這只是一個細節

我認爲你的問題是「破;」,只有「最後,」是正確的選擇。這個案例。

我試過了這個第二它的工作原理(test1.html和test2.html有不同的內容和我知道,當每個規則正在進入):

rewrite ^/posts.php /test1.html?type=$arg_type last; 
rewrite ^/(.+)/?$ /test2.html?url=$1 last; 

所以,你這應該工作:

rewrite ^/posts.php /controllers/read/C_ReadAllPosts.php?type=$arg_type last; 
rewrite ^/(.+)/?$ /controllers/read/C_ReadBlogURL.php?url=$1 last; 

或根據您的最後更新:

rewrite ^/posts\.php /controllers/read/C_ReadAllPosts.php?type=$arg_type last; 
rewrite ^/?$ /views/Read/BlogHome.php last; 
rewrite ^/(.+)/?$ /controllers/read/C_ReadBlogURL.php?url=$uri last;