nginx虛擬主機(jī)防webshell完美版
我們先來看下nginx.conf
server
{
listen 80;
server_name www.a.com;
index index.html index.htm index.php;
root /data/htdocs/www.a.com/;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
server
{
listen 80;
server_name www.b.com;
index index.html index.htm index.php;
root /data/htdocs/www.b.com/;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
nginx在80端口接受到訪問請求后,會把請求轉(zhuǎn)發(fā)給9000端口的php-cgi進(jìn)行處理
而如果修改php.ini中open_basedir= ../../../../../ ,針對兩個(gè)不同的網(wǎng)站,www.a.com , www.b.com都會把請求發(fā)送給9000處理,而如果先訪問www.a.com那么../../../../../就會變成A網(wǎng)站的根目錄地址,然后這時(shí)候如果你訪問www.b.com,那么open_basedir仍然是A網(wǎng)站的根目錄,但是對于B來說,又是不允許訪問的,所以就造成了,第二個(gè)站點(diǎn)打開以后會出現(xiàn)no input files,那么有什么解決辦法呢?
我們可以把不同的虛擬主機(jī)發(fā)送到不同的php-cgi端口進(jìn)行處理,當(dāng)然響應(yīng)的php-fpm配置文件中的open_basedir也不同。。我們來看看怎么配置。。
首先,nginx.conf配置如下
server
{
listen 80;
server_name www.a.com;
index index.html index.htm index.php;
root /data/htdocs/www.a.com/;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
}
server
{
listen 80;
server_name www.b.com;
index index.html index.htm index.php;
root /data/htdocs/www.b.com/;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fcgi.conf;
}
}
注意:www.a.com 的請求發(fā)送到9000端口 , www.b.com的請求發(fā)送到9001端口,依次類推
nginx配置修改了,相對的,php-fpm.conf也要修改
每個(gè)站點(diǎn)建一個(gè)conf
A站點(diǎn)
#cp /usr/local/webserver/php/etc/php-fpm.conf /usr/local/webserver/php/etc/www.a.com.conf
#vi /usr/local/webserver/php/etc/www.a.com.conf
找到php_defines,添加
<value name="open_basedir">/data/htdocs/www.a.com:/tmp:/var/tmp</value>

B站點(diǎn)
#cp /usr/local/webserver/php/etc/php-fpm.conf /usr/local/webserver/php/etc/www.b.com.conf
#vi /usr/local/webserver/php/etc/www.b.com.conf
找到php_defines,添加
<value name="open_basedir">/data/htdocs/www.b.com:/tmp:/var/tmp</value>

找到listen_address,修改為
<value name="listen_address">127.0.0.1:9001</value> 注意這里的端口號

最后要修改php-fpm啟動腳本
#vi /usr/local/webserver/php/sbin/php-fpm

注釋掉原來的 #$php_fpm_BIN --fpm $php_opts,添加
$php_fpm_BIN --fpm --fpm-config /usr/local/webserver/php/etc/www.a.com.conf
$php_fpm_BIN --fpm --fpm-config /usr/local/webserver/php/etc/www.b.com.conf
啟動服務(wù)
#/usr/local/webserver/php/sbin/php-fpm restart
查看端口
#netstat -tln

開了9000 9001分別處理兩個(gè)站點(diǎn)請求
兩個(gè)php-cgi主進(jìn)程加載不同的conf文件,這樣就完美解決了虛擬主機(jī)webshell能跨目錄的問題
當(dāng)然,啟動之前記得conf里面的max_children,開啟php-cgi子進(jìn)程數(shù),相應(yīng)要減少一些,以免造成內(nèi)存不足
文章來源:DoDo's Blog
原文地址:http://www.sectop.com/post/35.html
相關(guān)文章
解決Nginx location中配置proxy_pass轉(zhuǎn)發(fā)時(shí)斜線‘/‘導(dǎo)致404問題
這篇文章主要介紹了解決Nginx location中配置proxy_pass轉(zhuǎn)發(fā)時(shí)斜線‘/‘導(dǎo)致404問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
詳解Nginx中HTTP的keepalive相關(guān)配置
這篇文章主要介紹了Nginx中HTTP的keepalive相關(guān)配置,以及Nginx的Httpd守護(hù)進(jìn)程相關(guān)的keepalive timeout配置,需要的朋友可以參考下2016-01-01
Linux查看nginx安裝目錄和配置文件路徑的實(shí)現(xiàn)
本文主要介紹了Linux查看nginx安裝目錄和配置文件路徑的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Nginx動態(tài)壓縮gzip的實(shí)現(xiàn)示例
有時(shí)候適當(dāng)?shù)膲嚎s傳輸?shù)奈募PP或網(wǎng)站的性能有極大的提升,本文主要介紹了Nginx動態(tài)壓縮gzip的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
解決Nginx網(wǎng)關(guān)超時(shí)出現(xiàn)504 GATEWAY TIMEOUT的問題
這篇文章主要給大家介紹了如何解決Nginx網(wǎng)關(guān)超時(shí)出現(xiàn)504 GATEWAY TIMEOUT的問題,文章通過代碼示例和圖文結(jié)合介紹的非常詳細(xì),有遇到相同問題的朋友可以參考閱讀本文2023-11-11
nginx中的proxy_set_header參數(shù)指令詳解
本文介紹了Nginx中的proxy_set_header指令,用于自定義代理請求的HTTP頭部信息,實(shí)現(xiàn)更靈活的反向代理功能,提供了實(shí)際應(yīng)用場景和配置示例,幫助讀者更好地理解和使用proxy_set_header指令,感興趣的朋友一起看看吧2025-03-03
Nginx實(shí)現(xiàn)UDP四層轉(zhuǎn)發(fā)的過程
為了解決VPN連接速度慢的問題,可以通過Nginx實(shí)現(xiàn)UDP的四層轉(zhuǎn)發(fā),首先檢查Nginx是否安裝了with-stream模塊,然后修改nginx.conf配置文件進(jìn)行UDP端口的轉(zhuǎn)發(fā)設(shè)置,使用nginx-t檢查配置文件語法,無誤后重新加載Nginx,這樣通過國內(nèi)服務(wù)器中轉(zhuǎn),可以提升連接到國外服務(wù)器的速率2024-09-09

