nginx限制访问频率

修改服务器上的nginx的配置文件

/usr/local/nginx/conf/nginx.conf

http {

    limit_req_zone $binary_remote_addr zone=one:10m rate=3000r/m;

    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;

    proxy_temp_path /var/tmp;

 

    include       mime.types;

    default_type  application/octet-stream;

 

    gzip on;

    gzip_comp_level 6;

    gzip_vary on;

    gzip_min_length  1000;

    gzip_proxied any;

    gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    gzip_buffers 16 8k;

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

 

    upstream wedate{

        server 10.0.0.48:8080;

        server 10.0.0.50:8080;

    }

   server {

        listen       8080;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

          limit_req zone=wedate_zone burst=30;

          proxy_redirect off;

          proxy_set_header   X-Real-IP            $remote_addr;

          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

          proxy_set_header   X-Forwarded-Proto $scheme;

          proxy_set_header   Host                   $http_host;

          proxy_set_header   X-NginX-Proxy    true;

          proxy_set_header   Connection "";

          proxy_http_version 1.1;

          proxy_cache one;

          proxy_cache_key sfs$request_uri$scheme;

          proxy_pass         http://wedate;

        }

nginx平滑重启、

  

nginx -t -c /usr/local/nginx/conf/nginx.conf

相关文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Nginx的模块来限制同一IP的访问频率。其中一个常用的模块是ngx_http_limit_req_module,它可以帮助您实现这个目标。 要使用该模块,请按照以下步骤进行操作: 1. 打开您的Nginx配置文件,通常位于 `/etc/nginx/nginx.conf` 或 `/etc/nginx/conf.d/default.conf`。 2. 在适当的位置(例如 `http` 块内),添加以下配置: ``` http { # 设置限制频率的zone limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; # 设置限制频率的规则 server { location / { # 使用之前定义的zone进行限制 limit_req zone=one burst=5; # 其他配置项... } } # 其他配置项... } ``` 在上述配置中,`limit_req_zone` 定义了一个名为 `one` 的zone,使用 `$binary_remote_addr` 来标识每个IP地址。`10m` 是用于存储限制信息的内存大小,您可以根据实际情况进行调整。`rate=1r/s` 表示每秒允许一个请求。 在 `server` 块内的 `location` 配置中,使用 `limit_req zone=one burst=5` 来应用限制规则。`burst` 参数表示在超过限制频率时允许的突发请求数量。 3. 保存配置文件并重新加载Nginx,以使更改生效。您可以使用以下命令重新加载Nginx: ``` sudo systemctl reload nginx ``` 这样,Nginx就会根据配置限制同一IP的访问频率。当超过设定的限制时,Nginx会返回一个503错误给客户端。 请注意,您可以根据需要调整 `limit_req_zone` 和 `limit_req` 的参数,以满足您的具体需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值