nginx 配置
一、前言
为了防止访问服务器的流量超过服务器最大承载量,需要对服务器访问流量进行限制,因为业务访问通过nginx进行转发,所以采取nginx配置进行限流操作。使用了nginx自带的两个模块ngx_http_limit_conn_module,ngx_http_limit_req_module进行限流,具体参考来源官网文档。
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html
http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
二、nginx详细配置
方法一:ngx_http_limit_req_module
配置实例
http { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ... server { ... location /search/ { limit_req zone=one burst=5; }
以上配置只针对/search 业务有效,如果需要配置所有业务需要将 limit_req zone=one burst=5; 放置到http 段。
方法二:ngx_http_limit_conn_module
http{ #限流 limit_conn_log_level error;#限流log输出级别 limit_conn_status 503;#限流返回状态码 limit_conn_zone $server_name zone=perserver:10m;# 设置变量存储空间 limit_conn_zone $binary_remote_addr zone=addr:10m;#设置变量存储空间 limit_conn addr 100;#最大同时连接数,根据需求设置。 }
请求路径转发配置
location ^~ /8150/ { if ($request_uri ~* "/8150/(.*)") { proxy_pass http://88.9.1.89:8150/$1; } proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Remote_addr $remote_addr; }
请求转发到页面
location ^~ /th_world_cup_fnt/ { rewrite ^(.*)$ /error/404.html; }
内网机器访问外网接口的转发配置
反向代理 后端机器无法访问外网,通过访问前端转发请求
location ^~ /wx_interface_fnt/ {
proxy_pass https://yunying.com;
}
请求为转发到
访问 https://aitest.com/wx_interface_fnt
https://yunying.com/wx_interface_fnt
限制白名单入内的配置
#限制内网访问
location ^~ /jixxx/app/manage/ {
allow 221.1xx.xx.xx;
allow 180.xx.1xx.x6;
deny all;
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

浙公网安备 33010602011771号