Apache 或 Nginx 下禁止網站或附件目錄運行 PHP 指令碼

來源:互聯網
上載者:User


如果上傳目錄被植入 PHP 檔案,即可遠程執行。為了安全起見,我們一般會對上傳目錄禁止運行 PHP 指令碼。

在 Apache 下面我們可以通過一下方法來禁止運行 PHP 指令碼:

<Directory /wp-content/uploads> 
php_flag engine off 
</Directory> 

Nginx 方法如下:

location /wp-content/uploads/ { 
 location ~ .*\.(php)?$ { 
 deny all; 
 } 

而對於多個目錄的話,可以一起進行限定:

location ~* ^/(uploads|images)/.*\.(php|php5)$ 

deny all; 


lighthttpd

$HTTP["url"] =~ "^/(forumdata|templates|upload|images)/" {
 fastcgi.server = ()
}

例子,WordPress附件目錄禁止運行PHP

我用的是nginx,說說禁止方法:

location /wp-content/uploads/ {
    location ~ .*\.(php)?$ {
        deny all;
    }
}

插入你的nginx conf中,重啟nginx即可。
附上一個完整的虛擬機器主機的例子供參考:

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 /wp-content/uploads/ {
location ~ .*\.(php)?$ {
deny all;
}
location ~ .*.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
access_log off;
}

添加完執行:/usr/local/nginx/sbin/nginx -t測試組態檔案,執行:/usr/local/nginx/sbin/nginx -s reload 載入設定檔使其生效.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.