文章目录
文件服务器搭建
在http内配置一个server虚拟主机,我这里配置的是模拟的域名,通过修改windows hosts文件,将该域名指向我服务器的ip即可,location指定匹配规则,匹配到后直接可以访问服务器/data/fileserver文件目录下的相应文件,
例如:我服务器/data/fileserver目录下有一个router.png图片,那么我可以通过 http://domgo.com/app/file/router.png来访问这张图片
http {
# file server
server {
listen 80;
server_name domgo.com; #实际备案的域名
location /app/file {
alias /data/fileserver/;
}
}
}
#注意点:
#在 location / 中匹配root目录
#在 location /path 中配置alias虚拟目录,目录后的"/"符号一定要带上
nginx访问日志access.log的挖掘
做一些统计类的工作,例如:站点访问IP来源,访问频率,状态响应码,接口的访问量等等
- 默认配置解析
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#样例180.164.48.98 - - [04/Dec/2020:16:19:37 +0800] "GET /app/file/router.png HTTP/1.1" 200 28324 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
#$remote_addr 对应的是真实日志里的180.164.48.98,即访问的客户端IP
#$remote_user 对应第二个"-",没有远程用户,使用"-"填充
#[$time_local]对应[04/Dec/2020:16:19:37 +0800]
#$request 对应GET /app/file/router.png HTTP/1.1
#$status 对应的是状态码200,表示正常访问
#$body_bytes_sent 对应的是28324字节,即响应body的大小
#$http_referer对应的是”http://domgao.top/“,若是直接打开域名浏览的时,referer就会没有值,为”-“
#$http_user_agent 对应的是Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
#$http_x_forwarded_for 对应的是”-“或者空
- 统计访问最频繁的前100个IP
awk '{print $1}' access.log | sort -n |uniq -c | sort -rn | head -n 100
- 统计访问最多的前20个URL
cat access.log |awk '{print $7}'| sort | uniq -c | sort -rn| head -20 | more
- 上述案例解释说明
awk命令,linux统计常用命令之一,查询linux海量数据统计的命令有很多,包含grep,sed,awk,cut等
sort命令用于文本内容的排序, -n按照第一列数值排, -r倒序排
uniq去除重复出现的行列, -c在每列旁边显示该行重复出现的次数
head命令查询头部的内容, -n查询的行数
- 自定义日志格式,统计接口响应耗时
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time' ;
#加入了$request_time:从接受⽤户请求的第⼀个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应数据时间
#$upstream_response_time:指从Nginx向后端建⽴连接开始到接受完数据然后关闭连接为⽌的时间
#$request_time⼀般会⽐upstream_response_time⼤,因为⽤户⽹络较差,或者传递数据较⼤时,前者会耗时⼤很多
- 统计接口耗时,列出传输时间超过两秒的接口
cat access.log | awk '($NF > 2){print $7}'| sort | uniq -c | sort -rn | head -10
#$NF 表示最后一列
upstream负载均衡–nginx默认轮询策略
样例:
upstream lbs {
server 106.54.33.142:9090;
server 106.54.33.142:9091;
}
location /api/ {
proxy_pass http://lbs;
proxy_redirect default;
}
################# 轮询策略 #################
介绍:每个请求按顺序分配到不同的后端服务器
场景:会造成服务可靠性低和负载分配不均,适合静态服务器
############## weight权重策略 ##############
介绍:weight和访问比率成正比,数字越大,分配得到的流量越高
场景:服务器性能差异大时使用
upstream lbs {
server 106.54.33.142:9090 weight=10;
server 106.54.33.142:9091 weight=20;
}
############## ip hash策略 ##############
介绍:根据请求按访问ip的hash结果分配,这样每个用户可以固定访问某台服务器
场景:服务器业务分区、缓存、Session单点等
upstream lbs {
ip_hash;
server 106.54.33.142:9090;
server 106.54.33.142:9091;
}
############## 备用 ##############
upstream还可以为每个节点设置状态值,例如down,down的服务器不参与负载;
backup,当其他所有非backup机器down掉的时候,会请求backup机器;
upstream lbs {
server 106.54.33.142:9090 down;
server 106.54.33.142:9091;
}
upstream lbs {
server 106.54.33.142:9090;
server 106.54.33.142:9091 backup;
}
节点可用性探测与配置
参数: max_fails 允许请求失败的次数,默认值1,当超过最大次数时就不会请求
max_timeout max_fails次失败后,暂停的时间,默认值为10s
释义: max_fails=N 设置nginx与后端节点通信的尝试失败次数
max_timeout 设置这一时间内失败的次数达到N,那么该节点在下一个fail_timeout时间段内就不可用
如果设置N为0就会停止统计失败尝试次数,认为服务器一直可用
什么情况下nginx认为请求失败:
通过指令proxy_next_upstream来配置什么是失败的尝试。
默认配置时 http_404状态不认为是失败的尝试
upstream lbs {
server 106.54.33.142:9090 max_fails=2 fail_timeout=60s;
server 106.54.33.142:9091 max_fails=10 fail_timeout=300s;
}
location /api/ {
proxy_pass http://lbs;
proxy_next_upstream error timeout http_500 http_503 http_404;
}
nginx全局异常兜底数据配置
server {
listen 80;
server_name domgao.top;
#charset koi8-r;
#access_log logs/host.access.log main;
access_log logs/access.log main;
location / {
proxy_pass http://lbs;
proxy_redirect default;
#存放用户的真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_503 non_idempotent;
#开启错误拦截配置(必须)
proxy_intercept_errors on;
}
location /app/file {
alias /data/fileserver/;
}
location /api/ {
proxy_pass http://lbs;
proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 403 500 502 503 504 =200 /default_api;
location = /default_api {
default_type application/json;
return 200 '{"code":"-1","msg":"invoke fail, not found page"}';
}
}
nginx封禁恶意IP
- linux层iptables
- nginx层封禁后请求还是会进来,让nginx返回403占用资源
#单独网站屏蔽IP,把include xxx;放到网址对应的server{}语句块,
server {
listen 80;
server_name domgao.top;
#charset koi8-r;
#access_log logs/host.access.log main;
access_log logs/access.log main;
#IP屏蔽
include blacklist.conf;
location / {
proxy_pass http://lbs;
proxy_redirect default;
#存放用户的真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_503 non_idempotent;
#开启错误拦截配置(必须)
proxy_intercept_errors on;
}
location /app/file {
alias /data/fileserver/;
}
location /api/ {
proxy_pass http://lbs;
proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 403 500 502 503 504 =200 /default_api;
location = /default_api {
default_type application/json;
return 200 '{"code":"-1","msg":"invoke fail, not found page"}';
}
}
#虚拟主机所有网址屏蔽IP,把include xxx;放到http{}语句块内;
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream lbs {
server 106.54.33.142:9090;
server 106.54.33.142:9091;
}
#IP屏蔽
include blacklist.conf;
}
blacklist.conf
deny 192.168.107.206;
nginx跨域
- JSONP方式
- Http响应头配置,可以nginx和后端配置
非同源即是跨域,同源指 协议相同,域名相同,端口相同
浏览器控制台跨域提示:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
#nginx开启跨域配置
location / {
add_header 'Access-Control-AllowOrigin' $http_origin;
add_header 'Access-Control-AllowCredentials' 'true';
add_header 'Access-Control-AllowHeaders' 'DNT,web-token,apptoken,Authorization,Accept,Origin,KeepAlive,User-Agent,X-Mx-ReqToken,X-DataType,X-Auth-Token,X-Requested-With,IfModified-Since,Cache-Control,ContentType,Range';
add_header Access-Control-AllowMethods 'GET,POST,OPTIONS';
#如果预检请求则返回成功,不需要转发到后端
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 200;
}
}