lnmp去掉nginx上傳目錄的PHP執(zhí)行權(quán)限
LNMP有一個(gè)缺點(diǎn)就是目錄權(quán)限設(shè)置上不如Apache,有時(shí)候網(wǎng)站程序存在上傳漏洞或類似pathinfo的漏洞從而導(dǎo)致被上傳了php木馬,而給網(wǎng)站和服務(wù)器帶來比較大危險(xiǎn)。建議將網(wǎng)站目錄的PHP權(quán)限去掉,當(dāng)訪問上傳目錄下的php文件時(shí)就會返回403錯(cuò)誤。下面VPS偵探詳細(xì)介紹如何把lnmp環(huán)境下去掉指定目錄的PHP執(zhí)行權(quán)限。
首先要編輯nginx的虛擬主機(jī)配置,在fastcgi的location語句的前面按下面的內(nèi)容添加:
1、單個(gè)目錄去掉PHP執(zhí)行權(quán)限
location ~ /attachments/.*\.(php|php5)?$ {
deny all;
}
deny all;
}
將attachments目錄的PHP執(zhí)行權(quán)限去掉。
2、多個(gè)目錄去掉PHP執(zhí)行權(quán)限
location ~ /(attachments|upload)/.*\.(php|php5)?$ {
deny all;
}
deny all;
}
將attachments、upload這二個(gè)目錄的PHP執(zhí)行權(quán)限去掉。
附上一個(gè)完整的虛擬主機(jī)的例子供參考:
server
{
listen 80;
server_name bbs.vpser.net;
index index.html index.htm index.php;
root /home/wwwroot/bbs.vpser.net;include discuz.conf;
location ~ /(attachments|upload)/.*\.(php|php5)?$ {
deny all;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}access_log off;
}
{
listen 80;
server_name bbs.vpser.net;
index index.html index.htm index.php;
root /home/wwwroot/bbs.vpser.net;include discuz.conf;
location ~ /(attachments|upload)/.*\.(php|php5)?$ {
deny all;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}access_log off;
}
添加完執(zhí)行:/usr/local/nginx/sbin/nginx -t測試配置文件,執(zhí)行:/usr/local/nginx/sbin/nginx -s reload 載入配置文件使其生效。
如有問題歡迎在本文下方留言或到VPS論壇交流反饋。
原創(chuàng)文章,謝絕轉(zhuǎn)載!
>>轉(zhuǎn)載請注明出處:VPS偵探 本文鏈接地址:http://m.0794baidu.com/security/lnmp-remove-nginx-php-execute.html










需要從根目錄開始加上目錄的路徑
location ~ /(attachments|upload)/
如果目錄不是在網(wǎng)站根目錄下,需要寫全路徑嗎?
修改nginx配置文件后需要reload或restart nginx才能生效
http跳到https了的話只在https的虛擬主機(jī)上設(shè)置就可以
不成功建議論壇貼配置代碼看一下
你好,為什么我關(guān)閉不了PHP的權(quán)限?我是在VHOST網(wǎng)站文件里改的,是不是要到NGINX的配置文件里更改才能生效 ,明明檢測代碼是對的,有些主題能成功有些不能,一定要寫到80服務(wù)端嗎,443可以不,我都是HTTPS跳轉(zhuǎn)了, 我的修改文件跟你的有不同,首先80那里就沒有fastcgi.也不知道對應(yīng)文件的什么位置修改才能正確。
BTW:為什么我媒體庫上傳了一張照片,但是現(xiàn)實(shí)是不同尺寸的上傳了5張, 這個(gè)請問知道嗎。
@lige9, LNMPA下的設(shè)置和LNMP是一樣的,因?yàn)镹ginx是前端,禁用執(zhí)行權(quán)限的代碼要在轉(zhuǎn)發(fā)php的前面。
我安裝的是lnmpa,該怎么去掉上傳目錄的PHP執(zhí)行權(quán)限
@dong4567, 你看看圖片鏈接是什么,如果是通過php顯示的圖片,并且該文件在禁止執(zhí)行的目錄的話會無法顯示。
您好
我這樣設(shè)置后,站點(diǎn)的圖片都無法顯示,不懂是什么問題啊?3Q