繁体   English   中英

Nginx位置块-排除特定的PHP文件

[英]Nginx Location Block - Exclude Specific PHP File

对于客户端服务器群集上的站点,我具有以下Nginx服务器块:

# Create shared memory zone for managing requests from IP's. Request rate set at 60 requests per minute, equates to 1 per second.
# Any quicker than this and nginx will serve 404, instead of the resource.
limit_req_zone $binary_remote_addr zone=xmlrpc:10m rate=60r/m;

# Create shared memory zone for managing active connections. Works almost the same as above. Not currently using this.
limit_conn_zone $binary_remote_addr zone=addr:10m;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  REMOVED;
        root         /var/www/REMOVED/html;
        client_max_body_size 20M;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/ *.conf;

        location / {
        #root         /var/www/REMOVED/html;
        index  index.php index.htm index.html;
        proxy_read_timeout 200;
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?$args;
        include /var/www/REMOVED/html/nginx.conf;
        }

        location /xmlrpc.php {
        limit_req zone=xmlrpc;
        #limit_conn addr 10;#DO NOT USE
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                root           /var/www/REMOVED/html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_read_timeout 200;
                fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

        location ~ ^/(status|ping)$ {
        access_log off;
        allow 127.0.0.1;
        allow 10.10.10.0/24;
        allow 10.10.33.11;
        deny all;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        }

        error_page 404 /404.html;
            location = /404.html {
        }

        error_page 500 /500.html;
            location = /500.html {
        }
    }

这里感兴趣的区域是前几行,在这些行中,我试图将给定时间范围内的单个给定IP地址的请求数量限制为单个资源。 具体来说,我试图在wordpress安装中将每秒请求数从1个IP限制为xmlrpc.php。

如果我创建了一个测试htm文件,并将位置块从xmlrpc.php更改为testfile.htm,则这一切都可以按预期工作,任何频率高于每秒1的请求都会被阻止,并改为打404。 但是,如果我将位置阻止设置为xmlrpc.php,则无法正常工作,我仍然可以按我想要的方式访问domain.com/xmlrpc.php,而且我也不会受到阻止。

我使用php-fpm处理所有PHP请求,如您所见,Nginx仅充当PHP文件的代理。 location ~ \\.php$ {位置块负责处理。

我当前的理论是,由于xmlrpc.php请求不是由Nginx直接处理和服务的,因此它们忽略了Nginx中设置的请求限制。

有没有一种方法可以将xmlrpc.php从主location ~ \\.php$ {位置排除在外,因此它们受请求限制的影响,但是以合法方式访问xmlrpc.php文件时仍可以按预期工作吗? 谢谢。

解决。 问题是nginx继续处理位置块,并使用catch所有.php文件。 忽略我原来的xmlrpc.php一个。

我使用了“ =”运算符来查找与/xmlrpc.php的精确匹配,因此导致nginx在匹配该位置之后停止处理更多的位置块。

我的最终位置块看起来像这样:

    location = /xmlrpc.php {
    limit_req zone=xmlrpc;
    #limit_conn addr 10;#DO NOT USE
            root           /var/www/REMOVED/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_read_timeout 200;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }

这已经过测试和验证,可以正常工作。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM