2013-03-09 53 views
3

我通常使用apache,並且想要嘗試NGINX。NGINX配置。 PHP框架與PATHINFO 404

我已經在我的ubuntu dev機器上安裝了它,並且有幾個不同的框架和站點在開發中(codeigniter,symfony,laravel等)設置和開發。

我得到的問題是,只有.php結束的路徑工作。如果我嘗試index.php/welcome/index它只是404s而不是加載index.php。

我與cgi.fix_pathinfo集嘗試1和0

這是我目前(許多試過)站點配置。

server { 
listen 80; ## listen for ipv4; this line is default and implied 
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6 

root /my/path; 
index index.php index.html; 

# Make site accessible from http://localhost/ 
server_name localhost; 

#error_page 404 /404.html; 

# redirect server error pages to the static page /50x.html 
# 
#error_page 500 502 503 504 /50x.html; 
#location = /50x.html { 
# root /usr/share/nginx/www; 
#} 

location ~ \.php$ { 
    try_files $uri =404; 

    # Fix for server variables that behave differently under nginx/php-fpm than typically expected 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    # Include the standard fastcgi_params file included with nginx 
    include fastcgi_params; 
    fastcgi_param PATH_INFO  $fastcgi_path_info; 
    fastcgi_index index.php; 
    # Override the SCRIPT_FILENAME variable set by fastcgi_params 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
} 


location/{ 
    # First attempt to serve request as file, then 
    # as directory, then fall back to displaying a 404. 
    try_files $uri $uri/ =404; 
    # Uncomment to enable naxsi on this location 
    # include /etc/nginx/naxsi.rules 
} 

# deny access to .htaccess files, if Apache's document root 
# concurs with nginx's one 
# 
location ~ /\.ht { 
    deny all; 
} 
} 
+1

我知道,symfony還有鏈接到一個例子nginx的設置,我認爲大多數的你提到的其他框架中做的一樣好......沒你諮詢文件?此外,您的主要問題在於您正在尋找**位置**處的.php文件,以在您的位置 – prodigitalson 2013-03-09 22:49:33

+1

中明確顯示:http://wiki.nginx.org/Codeigniter。你需要「try_files $ uri $ uri//index.php;」 – galchen 2013-03-09 22:52:16

+0

@prodigitalson謝謝,它是導致問題的php $。現在刪除它,它工作正常。 galchen這隻會用於codeigniter和其他使用index.php(symfony不),也轉發所有的根,這是不利於發展。 – Atrox1449 2013-03-09 22:58:07

回答

2

我寧願使用下面的nginx的配置結構。它的清潔:

location/{ 
    try_files $uri $uri/ @phpsite; 
} 

location @phpsite { 
    include fastcgi_params; 
    ... other fast_cgi directives 
} 

有點複雜的設置可以在受歡迎的硅石項目中找到:http://silex.sensiolabs.org/doc/web_servers.html#nginx

我看到在原來的配置文件中2個問題:

location ~ \.php$ { 
    try_files $uri =404; 
    ... 
} 
  1. 在正則表達式「$」是指在字符串的結尾匹配。所以它失敗了,如prodigitalson的評論中所述。
  2. 上面的fast_cgi位置塊內的try_files指令不應該存在,因爲該位置塊應該由php單獨處理。清除這條線更乾淨。
+0

我刪除了$,它工作。問題是爲什麼當$在那裏時它不工作?畢竟,文件應該以「.php」結尾。 – Marin 2013-03-12 19:07:43

+0

匹配是針對整個網址的,例如index.php/welcome/index。這裏的最後一個字符串是索引,而不是php – 2013-03-12 21:24:19

+0

感謝你。所以我們說基本上匹配任何'路徑'和'.php',對嗎? – Marin 2013-03-14 14:22:45

0

我想你錯過就像

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

,將嘗試調用index.php文件的URL,如果路徑不存在的規則。

或者,也許,如果你知道這是沒有意義的嘗試其他事情,只是

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

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

這對我的作品......

location ~ ^(.*?\.php)($|/.+) { 
    try_files $1 =404; 

    ... fastcgi conf... 
}