一、多个Server优先级
nginx多个相同server_name的优先级
环境准备
mkdir /soft/code{1..3}/ -p
for i in {1..3};do echo "<h1>code $i</h1>" > /soft/code"$i"/index.html;done
三个conf文件分别为: cat testserver{1…3}.conf
server {
listen 80;
server_name testserver1 192.168.26.136;
location / {
root /soft/code1;
index index.html;
}
}
server {
listen 80;
server_name testservers 192.168.26.136;
location / {
root /soft/code2;
index index.html;
}
}
server {
listen 80;
server_name testserver3 192.168.26.136;
location / {
root /soft/code3;
index index.html;
}
}
访问测试
[root@lb-136 conf.d]# curl 192.168.26.136
<h1>code 1</h1>
可看出最初是 testserver1.conf优先被访问到,随后将 testserver1.conf改名为testserver5.conf ,此时再次访问。效果是 testserver2.conf匹配到了。 看样子他是根据文件“排序先后”来做对应匹配的。
有个参数listen 80 default_server; ,default_server他的效果是nginx上host匹配不到server,才会走default_server处理请求。
参考:https://blog.csdn.net/qq_35720307/article/details/88842288 《nginx host和default_server》
二、多个location优先级
一个 server 出现多个 location。下图是结论:
test_mul_location.conf 内容:
server {
listen 80;
server_name 192.168.26.136;
root /soft;
index index.html;
# location = /code1/ {
# rewrite ^(.*)$ /code1/index.html break;
# }
location ~ /code* {
rewrite ^(.*)$ /code3/index.html break;
}
location ^~ /code {
rewrite ^(.*)$ /code2/index.html break;
}
}
三、tryfile使用
nginx 的 try_files 按顺序检查文件是否存在。 (类似shell中的${var:=string}) 一般效果是如果页面不存在,则重定向到某个页面(像tx的)
配置 Nginx 的 tryfiles
测试
四、root与alias区别
也可参考朱哥的:https://www.zsythink.net/archives/3186 《在location中使用root和alias》
root
alias
五、获取真实客户端IP
$remote_addr 只能获取到最近一台服务器访问IP
x_forwarded_for头部信息容易被篡改
六、http返回状态码
七、网站ip、pv、uv
定义: (内容出自百度百科)
7.1问题:
如果一栋大厦里所有工作人员通过1个IP公网接口上网,总共100个设备,当所有人同时请求一个网站,并且刷新了5次,那么请求pv、ip、uv分别是多少?
pv: 页面浏览量 500
uv: 唯一设备 100
ip: 唯一出口 1
7.2 一些统计pv的工具
awk统计 取值 -> 排序 -> 去重 -> 统计 -> html文件
awk ‘{print $1}’ access.log-20210910 | sort | uniq -c | awk ‘{print $1}’ | xargs | awk ‘{print $1+$2}’
piwiki , 站长分析,谷歌统计,百度统计
公司开发写的统计脚本
八、网站访问流程
具体:
1.按照分层结构
CDN层->负载层->WEB层->存储层->缓存层->数据库层
同时需要注意, 每一层都有对应的缓存机制
如何优化nginx
1.gzip压缩
https://www.cnblogs.com/lovelinux199075/p/9057077.html
https://blog.csdn.net/aaa333qwe/article/details/85276590
2.expires静态文件缓存
https://www.cnblogs.com/architectforest/p/12795141.html
3.调整网络IO模型,调整Nginx worker进程的最大连接数
https://blog.csdn.net/weixin_34067102/article/details/91681928
5.隐藏Nginx名称和版本号
https://www.cnblogs.com/toughlife/p/5475180.html
6.配置防盗链,防止资源被盗用
https://www.cnblogs.com/javasl/p/12864249.html
7.禁止通过IP地址访问,禁止恶意域名解析,只允许域名访问
https://www.cnblogs.com/mafeng/p/11671793.html
8.防DDos、cc攻击,限制单IP并发请求连接
https://blog.csdn.net/weixin_34253126/article/details/92333794
9.配置错误页面,根据错误代码指定网页反馈用户
https://www.cnblogs.com/lasdaybg/p/9883795.html
10.限制上传资源目录被程序访问,防止木马入侵系统
https://blog.csdn.net/weixin_33809981/article/details/91497055
https://blog.csdn.net/EedWeek/article/details/103275488
11.Nginx加密传输优化
https://www.cnblogs.com/dazhidacheng/p/7772451.html 《nginx的优化》
https://mp.weixin.qq.com/s?__biz=MzAxOTE5NjQwOA==&mid=2650112750&idx=1&sn=28a6b0cdfa2615cc5e52ca6260b221b3&scene=23&srcid=0722sKyHmKIKp0tDUKSyz97u#rd