nginx虚拟目录支持PHP,nginx“虚拟目录”不支持php的解决方法
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的絕對路徑,就可以了。
?
?
相關文章
相關視頻
總結
以上是生活随笔為你收集整理的nginx虚拟目录支持PHP,nginx“虚拟目录”不支持php的解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 怎么执行unoconv,web执
- 下一篇: php抢购排队是怎样做的,基于swool