大网数据广告活动
服务器运维问答

Rate-limiting only the homepage with Nginx limit_req_zone module?

· 52CE 我爱测

I have a Wordpress multisite installation and I have some bots hammering the home pages. I would like to set a rate limit only on the home page(s) of the site(s) but leave the other pages unlimited. My trouble is crafting an NGIN…

问题原文

原帖链接 · Elkrat · CC BY-SA 4.0

I have a Wordpress multisite installation and I have some bots hammering the home pages. I would like to set a rate limit only on the home page(s) of the site(s) but leave the other pages unlimited.

My trouble is crafting an NGINX location that will match the home page's "blank" address of /index.php I can limit the entire site by matching "/" but that's not what I need.

I've tried using "index" as a defined location, but that does not work. Is there a trick I'm missing? This is a difficult case to search for, because most selective limiting questions are from people doing the opposite, people limiting resources with more specific addresses such as login scripts.

Here is what I have tried:

limit_req_zone $binary_remote_addr zone=mainlimit:10m rate=1r/m;

server {

    # SSL configuration

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;
    server_name example.com www.example.com;
    client_max_body_size 120M;

    root /usr/share/nginx/example;
    index index.php index.html index.htm;

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    #subdomain multi site with wp in 'wp' subdir
    if (!-e $request_filename) {
    # Redirect wp-* files/folders
    rewrite ^(/[^/]+)?(/wp-.*) /wp/$2 last;

    # Redirect other php files
    rewrite ^(/[^/]+)?(/.*\.php) /wp/$2 last;
    }

    location index { #THIS DOES NOT MATCH THE HOME PAGE
        limit_req zone=mainlimit burst=3 nodelay;
        try_files $uri $uri/ /index.php?$args ;
    }

    location / {
        #limit_req zone=mainlimit burst=3 nodelay; THIS MATCHES EVERYTHING
        try_files $uri $uri/ /index.php?$args ;
    }
    location ~* /wp-content/uploads/.*\.php$ {
    return 503;
    }

    location ~* /data/.*\.php$ {
    return 503;
    }
 ***MORE STUFF...

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

Use the exact match for location:

location = / {
    limit_req zone=mainlimit burst=3 nodelay;
    try_files $uri $uri/ /index.php?$args;
}

location / {
    try_files $uri $uri/ /index.php?$args;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-01-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。