如何在Apache和Nginx禁止上傳目錄裡PHP的執行許可權_Linux

來源:互聯網
上載者:User

Apache下禁止指定目錄運行PHP指令碼

在虛擬機器主機設定檔中增加php_flag engine off指令即可,配置如下:

 Options FollowSymLinks AllowOverride None Order allow,deny Allow from all  php_flag engine off

另外一種方法,是設定在htaccess裡面的,這個方法比較靈活一點,針對那些沒有apapche安全操作許可權的網站管理員:
Apache環境規則內容如下:Apache執行php指令碼限制 把這些規則添加到.htaccess檔案中
代碼如下:

RewriteEngine on RewriteCond % !^$RewriteRule uploads/(.*).(php)$ – [F]RewriteRule data/(.*).(php)$ – [F]RewriteRule templets/(.*).(php)$ –[F]

Nginx下禁止指定目錄運行PHP指令碼

Nginx更簡單,直接通過location條件匹配定位後進行許可權禁止,可在server配置段中增加如下的配置。

如果是單個目錄:

location ~* ^/uploads/.*\.(php|php5)${ deny all;}

如果是多個目錄:

location ~* ^/(attachments|uploads)/.*\.(php|php5)${ deny all;}

注意:這段設定檔一定要放在下面配置的前面才可以生效。

location ~ \.php$ {fastcgi_pass  127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include    fastcgi_params;}

最後給一個完整的配置樣本

location ~ /mm/(data|uploads|templets)/*.(php)$ { deny all;}location ~ .php$ { try_files $uri /404.html; fastcgi_pass  127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include    fastcgi_params;}

配置完後記得重啟Nginx生效。

聯繫我們

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