Home Backend Development PHP Problem How to ban access to php in nginx

How to ban access to php in nginx

Nov 22, 2022 am 09:52 AM
php nginx

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

How to ban access to php in nginx

本教程操作环境:Windows7系统、PHP8.1版、Dell G3电脑。

nginx怎么禁止访问php?

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;
        }
Copy after login

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

放置在server标签内:

    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;
            }
         
        ......
        ......
    }
Copy after login

nginx下配置禁止访问*.txt和*.doc文件

配置如下:

放置在server标签内:

      location ~* \.(txt|doc)$ {
            if (-f $request_filename) {
            root /data/www/www;
            #rewrite ...    #可以重定向到某个URL;
            break;
            }
        }
        location ~* \.(txt|doc)$ {
            root /data/www/www;
            deny all;
        }
Copy after login

二、禁止访问指定目录下的所有文件和目录

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

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

放置在server标签内:

       location ~ ^/(static)/ {
           deny all;
       }
 
       location ~ ^/static {
           deny all;
       }
Copy after login

禁止访问多个目录的配置如下:

location ~ ^/(static|js) {
    deny all;
}
Copy after login

禁止访问目录并返回指定的http状态码,配置如下:

放置在server标签内:

    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;
            }
Copy after login

作用:禁止访问目录下的指定文件或者禁止访问指定目录下的所有内容。

三、限制网站来源IP访问

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

在server标签内配置如下:

        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;
        }
Copy after login

说明:该配置只允许192.168.0.4IP访问mysql_loging目录

限制IP或IP段访问,配置如下:

添加在server标签内:

        location / {
            deny 192.168.0.4;
            allow 192.168.1.0/16;
            allow 10.0.0.0/24;
            deny all;
        }
Copy after login

说明:此限制是对某些IP做整个网站的限制访问。

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

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

if ( $remoteaddr = 10.0.0.7 ) {
    return 403;
}
 
if ( $remoteaddr = 218.247.17.130 ) {
    set $allow_access_root 'ture';
}
Copy after login

方法2:利用deny和allow只允许IP访问,配置如下:

location / {
    root html/blog;
    index index.php index.html index.htm;
    allow 10.0.0.7;
    deny all;
}
Copy after login

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

location / {
    root html/blog;
    index indx.php index.html index.htm;
    deny 10.0.0.7;
    allow all;
}
Copy after login

注意事项:

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; 结尾,表示除了上面允许的,其他的都禁止。如:

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;
Copy after login

四、配置nginx,禁止非法域名解析访问企业网站

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

server {
listen 80 default_server;
server_name _;
return 501;
}
Copy after login

方法2:通过301跳转主页,配置如下:

server {
listen 80 default_server;
server_name _;
rewrite ^(.*) http://www.dmtest.com/$1 permanent;
}
Copy after login

方法3:发现某域名恶意解析到公司的服务器IP,在server标签里添加以下代码即可,若有多个server要多处添加。

if ($host !~ ^www/.dmtest/.com$) {
        rewrite ^(.*) http://www.dmtest.com.com$1 permanent;
}
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of How to ban access to php in nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to start nginx in Linux How to start nginx in Linux Apr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check the running status of nginx How to check the running status of nginx Apr 14, 2025 am 11:48 AM

The methods to view the running status of Nginx are: use the ps command to view the process status; view the Nginx configuration file /etc/nginx/nginx.conf; use the Nginx status module to enable the status endpoint; use monitoring tools such as Prometheus, Zabbix, or Nagios.

See all articles