首頁 運維 Nginx nginx如何禁止存取php

nginx如何禁止存取php

May 17, 2023 pm 09:19 PM
php nginx

nginx禁止访问php的方法:1、配置nginx,禁止解析指定目录下的指定程序;2、将“location ~^/images/.*\.(php|php5|sh|pl|py)${deny all...}”语句放置在server标签内即可。

nginx站点目录及文件URL访问控制

一、根据扩展名限制程序和文件访问

利用nginx配置禁止访问上传资源目录下的PHP、Shell、Perl、Python程序文件。

配置nginx,禁止解析指定目录下的指定程序。

location ~ ^/images/.*\.(php|php5|sh|pl|py)$
        {
            deny all;
        }
         
location ~ ^/static/.*\.(php|php5|sh|pl|py)$
        {
            deny all;
        }
         
location ~ ^/data/(attachment|avatar).*\.(php|php5)$
        {
            deny all;
        }
登入後複製

对上述目录的限制必须写在nginx处理PHP服务配置的前面,如下:

放置在server标签内:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> server { listen 80; server_name www.dmtest.com; location / { root html; index index.php index.html index.htm; } location ~ ^/images/.*\.(php|php5|sh|pl|py)$ { deny all; } location ~ ^/static/.*\.(php|php5|sh|pl|py)$ { deny all; } location ~ ^/data/(attachment|avatar).*\.(php|php5)$ { deny all; } ...... ...... }</pre><div class="contentsignin">登入後複製</div></div><p>nginx下配置禁止访问*.txt和*.doc文件 </p>

配置如下:

放置在server标签内:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> location ~* \.(txt|doc)$ { if (-f $request_filename) { root /data/www/www; #rewrite ... #可以重定向到某个URL; break; } } location ~* \.(txt|doc)$ { root /data/www/www; deny all; }</pre><div class="contentsignin">登入後複製</div></div><p>二、禁止访问指定目录下的所有文件和目录 </p>

配置禁止党文指定的单个或多个目录。

禁止访问单个目录的命令如下:

放置在server标签内:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> location ~ ^/(static)/ { deny all; } location ~ ^/static { deny all; }</pre><div class="contentsignin">登入後複製</div></div><p>禁止访问多个目录的配置如下: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>location ~ ^/(static|js) { deny all; }</pre><div class="contentsignin">登入後複製</div></div><p>禁止访问目录并返回指定的http状态码,配置如下: </p>

放置在server标签内:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> server { listen 80; server_name www.dmtest.com; location / { root html; index index.php index.html index.htm; } location /admin/ { return 404; }    #访问admin目录返回404; location /templates/ { return 403; }  #访问templates目录返回403 location ~ ^/images/.*\.(php|php5|sh|pl|py)$ { deny all; }</pre><div class="contentsignin">登入後複製</div></div><p>作用:禁止访问目录下的指定文件或者禁止访问指定目录下的所有内容。 </p>

三、限制网站来源IP访问

禁止目录让外界访问,但允许某IP访问该目录且支持PHP解析,配置如下:

在server标签内配置如下:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> location ~ ^/mysql_loging/ { allow 192.168.0.4; deny all; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }</pre><div class="contentsignin">登入後複製</div></div><p>说明:该配置只允许192.168.0.4IP访问mysql_loging目录</p><p>限制IP或IP段访问,配置如下:</p><p>添加在server标签内:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> location / { deny 192.168.0.4; allow 192.168.1.0/16; allow 10.0.0.0/24; deny all; }</pre><div class="contentsignin">登入後複製</div></div><p>说明:此限制是对某些IP做整个网站的限制访问。 </p>

nginx做反向代理的时候也可以限制客户端IP,具体如下:

方法1:使用if来控制,配置如下:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>if ( $remoteaddr = 10.0.0.7 ) { return 403; } if ( $remoteaddr = 218.247.17.130 ) { set $allow_access_root &amp;#39;ture&amp;#39;; }</pre><div class="contentsignin">登入後複製</div></div><p>方法2:利用deny和allow只允许IP访问,配置如下: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>location / { root html/blog; index index.php index.html index.htm; allow 10.0.0.7; deny all; }</pre><div class="contentsignin">登入後複製</div></div><p>登录后复制</p>

方法3:只拒绝某些IP访问,配置如下:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>location / { root html/blog; index indx.php index.html index.htm; deny 10.0.0.7; allow all; }</pre><div class="contentsignin">登入後複製</div></div><p>注意事项: </p>

deny一定要加一个IP,否者会直接跳转到403,不在往下执行了,如果403默认页在同一域名下,会造成死循环访问。

对于allow的IP段,从允许访问的段位从小到大排列,如127.0.0.0/24的下面才能是10.10.0.0/16,其中:

  24表示子网掩码:255.255.255.0

  16表示子网掩码:255.255.0.0

  8表示子网掩码:255.0.0.0

以deny all; 结尾,表示除了上面允许的,其他的都禁止。如:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>deny 192.168.1.1; allow 127.0.0.0/24; allow 192.168.0.0/16; allow 10.10.0.0/8; deny all;</pre><div class="contentsignin">登入後複製</div></div><p>四、配置nginx,禁止非法域名解析访问企业网站 </p>

方法1:让使用IP访问网站的用户,或恶意接卸域名的用户,收到501错误,配置如下:

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>server { listen 80 default_server; server_name _; return 501; }</pre><div class="contentsignin">登入後複製</div></div><p>方法2:通过301跳转主页,配置如下: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>server { listen 80 default_server; server_name _; rewrite ^(.*) http://www.dmtest.com/$1 permanent; }</pre><div class="contentsignin">登入後複製</div></div><p>方法3:发现某域名恶意解析到公司的服务器IP,在server标签里添加以下代码即可,若有多个server要多处添加。 </p>

if ($host !~ ^www/.dmtest/.com$) {
        rewrite ^(.*) http://www.dmtest.com.com$1 permanent;
}
登入後複製

以上是nginx如何禁止存取php的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP的目的:構建動態網站 PHP的目的:構建動態網站 Apr 15, 2025 am 12:18 AM

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

PHP:處理數據庫和服務器端邏輯 PHP:處理數據庫和服務器端邏輯 Apr 15, 2025 am 12:15 AM

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

為什麼要使用PHP?解釋的優點和好處 為什麼要使用PHP?解釋的優點和好處 Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

See all articles