[英]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.