nginx“虛擬目錄”不支援php的解決方案

來源:互聯網
上載者:User
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的絕對路徑,就可以了。

?

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.