nginx“虛擬目錄”不支援php的解決辦法
這幾天在配置Nginx,PHP用FastCGI,想裝一個phpMyAdmin管理資料庫,phpMyAdmin不想放在網站根目錄 下,這樣不容易和網站應用程式混在一起,這樣phpMyAdmin的目錄就放在別處,在Apache裡,有alias,比較方便,在Nginx下沒有虛擬目錄 概念的,是用location配合alias使用,我先試了簡單的配置方式
location /web/ {
alias /data/web/;
index index.html index.htm index.php;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
我用http://localhost/web/可以訪問到/data/web目錄下的靜態檔案,但訪問php檔案,卻報No input file specified.的錯誤,而且在Nginx的error日誌上卻什麼資訊也沒有,我在網上搜尋了一下,判斷應該是php檔案並沒有被後端的 FastCGI運行,我又繼續搜尋一些文章,試著增加了一段配置
location /web/ {
alias /data/web/;
index index.html index.htm index.php;
}
location ~ ^/web/.+\.php$ {
root /data/;
rewrite /web/(.*\.php?) /$1 break;
include fcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/web$fastcgi_script_name;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
這下可以了,原理應該是採用rewrite的方法,對於/web/下php類型的的請求交給後端的FastCGI處理,並且指定了php指令碼的位 置,這樣我們就可以配置phpMyAdmin了,配置如下
location /phpmyadmin/ {
alias /data/phpmyadmin/;
index index.html index.htm index.php;
}
location ~ ^/phpmyadmin/.+\.php$ {
root /data/;
rewrite /phpmyadmin/(.*\.php?) /$1 break;
include fcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/phpmyadmin$fastcgi_script_name;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
要注意的是
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
這段,要放在phpmyadmin的後面,放在前面就有問題,這是和Nginx的location規則有關,具體看Nginx的文檔,另 外,phpMyAdmin裡要配置一下URI的絕對路徑,就可以了。
?
?