[NGINX] Ограничение скорости по IP.

DavyJohnes
На сайте с 05.01.2011
Offline
84
3280

Задача: ограничить суммарную скорость отдачи для все подключений с одного IP.

Вычитал что можно сделать это с помощью переменной $limit_rate

Вот часть моего конфига:


server {
listen 176.119.4.2:80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

#root /usr/share/nginx/www;
#index index.html index.htm;

# Make site accessible from http://localhost/
#server_name localhost;

limit_conn addr 10;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
set $limit_rate 300k;

proxy_pass http://127.0.0.1/;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 128k;

client_body_temp_path /etc/nginx/client_body_temp;

proxy_connect_timeout 70;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_temp_path /etc/nginx/proxy_temp;
}

location ~* ^/(?!monitorix).*\.(ico|jpg|jpeg|png|gif|css|js|swf)$ {
root /var/www;
access_log off;
expires 7d;
}

location /nginx_status {
# copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/
stub_status on;
access_log off;
allow 176.62.66.80;
deny all;
}

#location /doc/ {
# alias /usr/share/doc/;
# autoindex on;
# allow 127.0.0.1;
# allow ::1;
# deny all;
#}

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

Почему то не робит, где я ошибся ?

https://handy-tools.io (https://handy-tools.io) - Набор полезных утилит для всех!
Andreyka
На сайте с 19.02.2005
Offline
822
#1

Сначала научитесь показывать конфиг без комментариев и пустых строк

Не стоит плодить сущности без необходимости
DavyJohnes
На сайте с 05.01.2011
Offline
84
#2
Andreyka:
Сначала научитесь показывать конфиг без комментариев и пустых строк

Ок босс.


server {
listen 176.119.4.2:80; ## listen for ipv4; this line is default and implied
limit_conn addr 10;
location / {
set $limit_rate 300k;
proxy_pass http://127.0.0.1/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 10m;
client_body_buffer_size 128k;
client_body_temp_path /etc/nginx/client_body_temp;
proxy_connect_timeout 70;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /etc/nginx/proxy_temp;
}
location ~* ^/(?!monitorix).*\.(ico|jpg|jpeg|png|gif|css|js|swf)$ {
root /var/www;
access_log off;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 176.62.66.80;
deny all;
}
}
L
На сайте с 07.07.2008
Offline
89
#3

в nginx нет ограничения скорости по IP, только на сессию

_
На сайте с 13.11.2012
Offline
2
_A_
#4
Модуль ngx_http_limit_req_module (0.7.21) позволяет ограничить скорость обработки запросов по заданному ключу или, как частный случай, скорость обработки запросов, поступающих с одного IP-адреса. Ограничение обеспечивается с помощью метода “leaky bucket”.

http://nginx.org/ru/docs/http/ngx_http_limit_req_module.html

Самый стабильный хостинг (http://www.beget.ru/?id=27692)
B
На сайте с 31.01.2012
Offline
125
#5

В http{ добавьте:

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

В каждый (если много виртуалхостов) location / { добавьте:

limit_req zone=one burst=5;

service nginx restart

1r/s - означает 1 запрос в секунду. Если нужно ограничить до 1 запроса в 2 секунды, то нужно писать так:

30r/m (30 запросов/минута) итд по аналогии

_
На сайте с 13.11.2012
Offline
2
_A_
#6

Если же вам нужно ограничить скорость именно в MiB/сек, то смотрите в сторону iptables и tc(Traffic Control).

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий