1.前后端分离之静态网站配置
(1)假如我们有一个网站
主页路径:/usr/local/hadluo/qiqi/qiqi-phone/index.html
(2)nginx配置
server {
listen 80;
##你网站的ip,或者域名
server_name localhost;
log_not_found off;
#add_header Access-Control-Allow-Origin $corsHost;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
## 访问localhost/qiqi
location /qiqi {
##alias :指定网站在本机的绝对路径
alias /usr/local/hadluo/qiqi/qiqi-phone/;
index index.html;
}
}
(3)测试访问http://localhost/qiqi
2.负载均衡配置
(1)内置负载策略
- 轮循(默认):Nginx根据请求次数,将每个请求均匀分配到每台服务器。
- 最少连接: 将请求分配给连接数最少的服务器。Nginx会统计哪些服务器的连接数最少。
- IP Hash : 绑定处理请求的服务器。第一次请求时,根据该客户端的IP算出一个HASH值,将请求分配到集群中的某一台服务器上。后面该客户端的所有请求,都将通过HASH算法,找到之前处理这台客户端请求的服务器,然后将请求交给它来处理。
(2)定义一个upstream集群
upstream hadluo-zull{
server 127.0.0.1:8882 weight=10;
server 127.0.0.1:8883 weight=10;
server www.examp.com:8883 weight=10;
server 192.168.0.102:8080 backup;
}
hadluo-zull集群名称,server为机器,weight为权重。
backup : 指定备份机,所有服务挂了就走backup 机。
(3)使用这个集群
server {
listen 80;
location / {
# upstream指定的集群名称
proxy_pass http://hadluo-zull;
}
}
当访问nginx机器的80端口时,就会映射到hadluo-zull集群里面。
3.集成upstream_check_module模块
模块点的作用
nginx自带的针对后端节点健康检查的功能比较简单,无法主动识别后端节点状态,后端即使有不健康节点, 负载均衡器依然会把该请求转发给该不健康节点,只能等待超时时间后转发到其他节点,这样就会造成响应延迟性能降低的问题。
安装使用
(1)下载最新的稳定版本nginx-1.16.1源码包并解压
[root@nginx ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@nginx ~]# tar -zxvf nginx-1.16.1.tar.gz
(2)nginx_upstream_check_module模块下载
如果没有unzip命令可以通过’yum -y install unzip’安装。
[root@nginx ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@nginx ~]# yum -y install unzip
[root@nginx ~]# unzip master.zip
(3)安装相关包
分别安装依赖包gcc、pcre、zlib、OpenSSL。
[root@nginx ~]# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
(4)进入nginx源码目录,打上模块补丁
[root@nginx ~]# yum -y install patch
[root@nginx ~]# cd nginx-1.16.1
[root@nginx nginx-1.16.1]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.16.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
(5)编译安装nginx
[root@nginx nginx-1.16.1]# ./configure --add-module=/root/nginx_upstream_check_module-master
[root@nginx nginx-1.16.1]# make && make install
(6)安装到了/usr/local/nginx目录下,检验并设置软连接
[root@nginx nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
[root@nginx nginx-1.16.1]# nginx -v
nginx version: nginx/1.16.1
[root@nginx nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --add-module=/root/nginx_upstream_check_module-master
[root@nginx nginx-1.16.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
(7)配置模块
配置一个集群
配置监控地址
访问成功
4.ip限流(一个ip只能每秒访问N次)
(1)定义http全局限流规则
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
- limit_req_zone定义在http块中,$binary_remote_addr表示保存客户端IP地址的二进制形式。
- Zone定义IP状态及URL访问频率的共享内存区域。zone=keyword标识区域的名字,以及冒号后面跟区域大小。16000个IP地址的状态信息约1MB,所以示例中区域可以存储160000个IP地址。
- Rate定义最大请求速率。示例中速率不能超过每秒10个请求。
(2)设置限流
location / {
limit_req zone=mylimit burst=20 nodelay;
proxy_pass http://real_server;
}
burst
这个配置的意思是设置一个大小为5的缓冲区漏桶,当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到漏桶进行排队,超过的请求会直接报503的错误然后返回。由于进行了排队,延迟大大增加,在很多场景下仍然是不能接受的。
nodelay
把开始执行请求的时间提前,以前是delay到从桶里漏出来才执行,现在不delay了,只要入桶就开始执行。
要么立刻执行,要么被拒绝,请求不会因为限流而增加延迟了。
5.指定白名单ip内 不限流
geo $limit {
default 1;
192.168.2.0/24 0;
}
map $limit $limit_key {
1 $binary_remote_addr;
0 "";
}
limit_req_zone $limit_key zone=mylimit:10m rate=1r/s;
location / {
limit_req zone=mylimit burst=1 nodelay;
proxy_pass http://real_server;
}
上述配置中,192.168.2.0/24网段的IP访问是不限流的,其他限流。
IP后面的数字含义:
- 24表示子网掩码:255.255.255.0
- 16表示子网掩码:255.255.0.0
- 8表示子网掩码:255.0.0.0
6.限制带宽
限制每个连接的带宽,可以用于限制下载速度,限制流量等。
# 限制下载速度为5m/s 可以配置在http{},server{},location{}中。
limit_rate 5m;
===============================================
# 前500kb速度可以全速下载,之后限速100k/s 可以配置在http{},server{},location{}中。
limit_rate_after 500k;
limit_rate 100k;
6.nginx接口缓存
(1)http块声明缓存
#############################################################################
###cache声明
##############################################################################
proxy_temp_path /opt/tmp;
proxy_cache_path /opt/neicun levels=1:2 keys_zone=my_cache:20m inactive=30 max_size=2g;
- proxy_cache_path 缓存文件路径。
- levels 设置缓存文件目录层次;levels=1:2 表示两级目录。
- keys_zone 设置缓存名字和共享内存大小。
- inactive 在指定时间内没人访问则被删除。
- max_size 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。
(2)配置location
location / {
proxy_pass http://hadluo-zull;
}
## nginx接口缓存
include /opt/include/nginx-cache-hadluo-zull.conf;
这里配置到了外部文件
(3)nginx-cache-hadluo-zull.conf
#/clients 为你的接口 路径 http://hadluo-zull/clients
location ^~ /clients {
proxy_pass http://hadluo-zull;
proxy_cache my_cache;
# 对于返回code为200 的缓存60s
proxy_cache_valid 200 60s;
#缓存的http method类型
proxy_cache_methods GET HEAD POST;
#当多个客户端请求一个缓存中不存在的文件(或称之为一个MISS),只有这些请求中的第一个被允许发送至服务器。>其他请求在第一个请求得到满意结果之后在缓存中得到文件。
proxy_cache_lock on;
proxy_cache_use_stale updating error timeout http_404 http_500 http_502 http_503 http_504;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
##### $arg_xx : 为参数是否参与缓存
#proxy_cache_key "$scheme$host$uri=$arg_trackType=$arg_pageSize=$arg_storeType=$arg_max=$arg_cursor=$arg_hideNoStock";
proxy_cache_key "$scheme$host$uri";
# 接口带cookie时加这行,忽略 cookie
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
}
proxy_cache_use_stale updating error timeout http_404 http_500 http_502 http_503 http_504 参数说明:
- updating:当缓存内容过期,有一个请求正在访问上游试图更新缓存时,其他请求直接使用过期内容返回客户端。
- error:当与上游建立连接、发送请求、读取响应头部等情况出错时,使用缓存。
- timeout:当与上游建立连接、发送请求、读取响应头部等情况出现超时,使用缓存。
- http_xxx:缓存以上错误响应吗的内容。
(4)nginx reload 测试
访问你的接口:http://hadluo-zull/clients 就会生成nginx缓存目录:
7.nginx恶意请求
分析nginx日志,找出恶意ip或user-agent
cat /var/log/nginx/access.log | awk -F\" '{A[$(NF-1)]++}END{for(k in A)print A[k],k}' | sort -n |tail
122 58.144.7.66
337 106.91.201.75
2270 122.200.77.170 #显然这个ip不正常,而且这不是nginx所知道的真实ip,而是$http_x_forwarded_for变量