Server优先级
Nginx 多个相同 Server_name 优先级1.环境准备
[root@nginx ~]# mkdir /soft/code{1..3} -p
[root@nginx ~]# for i in {1..3};do echo "<h1>Code $i</h1>" >
/soft/code"$i"/index.html;done2.准备多份相同 Nginx 配置⽂件
[root@Nginx conf.d]# ll
总⽤量 12
-rw-r--r-- 1 root root 123 4⽉ 19 19:08 testserver1.conf
-rw-r--r-- 1 root root 123 4⽉ 19 19:09 testserver2.conf
-rw-r--r-- 1 root root 123 4⽉ 19 19:09 testserver3.conf
//内容如下
[root@Nginx conf.d]# cat testserver{1..3}.conf
server {listen 80;server_name testserver1 10.1.106.70;location / {root /soft/code1;index index.html;}
}
server {listen 80;server_name testserver2 10.1.106.70;location / {root /soft/code2;index index.html;}
}
server {listen 80;server_name testserver3 10.1.106.70;location / {root /soft/code3;index index.html;}
}//检测语法
[root@Nginx conf.d]# nginx -t
nginx: [warn] conflicting server name "10.1.106.70" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "10.1.106.70" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful//重启Nginx
[root@Nginx conf.d]# nginx -t3.测试访问效果
[root@Nginx conf.d]# curl 10.1.106.70
<h1>Code 1</h1>
[root@Nginx conf.d]# mv testserver1.conf testserver5.conf
[root@Nginx conf.d]# nginx -s reload
[root@Nginx conf.d]# curl 10.1.106.70
<h1>Code 2</h1>
location优先级
⼀个 server 出现多个 location= 进⾏普通字符精确匹配, 完全匹配
^~ 表示普通字符匹配, 使⽤前缀匹配
正则匹配 匹配后会继续查找更精确匹配的location
~ 区分⼤⼩写匹配
~* 不区分⼤⼩写
/ 通⽤匹配(默认匹配)1.实例准备
[root@Nginx conf.d]# cat testserver.conf
server {listen 80;server_name 10.1.106.70;root /soft;index index.html;location = /code1/ {rewrite ^(.*)$ /code1/index.html break;}location ^~ /code1/ {# 处理类似于 /code1/something 的请求try_files $uri $uri/ /code1/index.html;}location ^~ /code {rewrite ^(.*)$ /code2/index.html break;}location ~ /code* {rewrite ^(.*)$ /code3/index.html break;}
}2.测试效果
[root@Nginx conf.d]# curl http://10.1.106.70/code1/
<h1>Code 1</h1>//注释掉精确匹配=, 重启Nginx
[root@Nginx ~]# curl http://10.1.106.70/code1/
<h1>Code 2</h1>//注释掉^~, 重启Nginx
[root@Nginx ~]# curl http://10.1.106.70/code1/
<h1>Code 3</h1>// 测试默认
[root@Nginx]# mkdir -p /soft/default/
[root@Nginx]# echo "default /" > /soft/default/index.html
[root@Nginx]# curl http://10.1.106.70/default/index.html
default /
try_files的使⽤
nginx 的 try_files 按顺序检查⽂件是否存在location / {
try_files $uri $uri/ /index.php;
}
#1.检查⽤户请求的uri内容是否存在本地,存在则解析
#2.将请求加/, 类似于重定向处理
#3.最后交给index.php处理1.演示环境准备
[root@Nginx ~]# echo "Try-Page" > /soft/code/index.html
[root@Tomcat ~]# echo "Tomcat-Page" > /soft/tomcat-8080/webapps/ROOT/index.html
//启动tomcat
[root@Tomcat ~]# sh /soft/app/tomcat-8080/bin/startup.sh
//检查tomcat端⼝
[root@Tomcat ~]# netstat -lntp|grep 8080
tcp6 0 0 :::8080 :::* LISTEN
104952/java2.配置 Nginx 的 tryfiles
[root@Nginx ~]# cat /etc/nginx/conf.d/try.conf
server {listen 80;server_name 10.1.106.70;root /soft/code;index index.html;location / {try_files $uri @java_page;}location @java_page {proxy_pass http://10.1.106.66:8080;}
}
//重启Nginx
[root@Nginx ~]# nginx -s reload3.测试 tryfiles
[root@Nginx ~]# curl http://10.1.106.70/index.html
Try-Page//将/soft/code/index.html⽂件移⾛
[root@Nginx ~]# mv /soft/code/{index.html,index.html_bak}//发现由Tomcat吐回了请求
[root@Nginx ~]# curl http://10.1.106.70/index.html
Tomcat-Page
alias与root区别
root 路径配置
[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p
[root@Nginx ~]# echo "Root" > /local_path/code/request_path/code/index.html
//Nginx的root配置
[root@Nginx ~]# cat /etc/nginx/conf.d/root.conf
server {listen 80;index index.html;location /request_path/code/ {root /local_path/code/;}
}
//请求测试
[root@Nginx conf.d]# curl http://10.1.106.70/request_path/code/index.html
Root
//实际请求本地⽂件路径为
/local_path/code/request_path/code/index.htmlalias 路径配置
[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p
[root@Nginx ~]# echo "Alias" > /local_path/code/index.html
//配置⽂件
[root@Nginx ~]# cat /etc/nginx/conf.d/alias.conf
server {listen 80;index index.html;location /request_path/code/ {alias /local_path/code/;#root /local_path/code/}
}
//测试访问
[root@Nginx ~]# curl http://10.1.106.70/request_path/code/index.html
Alias
//实际访问本地路径
/local_path/code/'index.html'
获取⽤户真实IP
Nginx 传递⽤户的真实IP地址
$remote_addr 只能获取到最近⼀台服务器访问IP
x_forwarded_for 头部信息容易被篡改
常⻅HTTP状态码
200 正常请求
301 永久跳转
302 临时跳转
400 请求参数错误
401 账户密码错误(authorization required)
403 权限被拒绝(forbidden)
404 ⽂件没找到(Not Found)
413 ⽤户上传⽂件⼤⼩限制(Request Entity Too Large)
502 后端服务⽆响应(boy gateway)
504 后端服务执⾏超时(Gateway Time-out)
Nginx优化⽅案
Nginx优化
#安全
1.隐藏Nginx名称和版本号
2.Nginx加密传输优化
3.配置防盗链,防⽌资源被盗⽤
#访问控制
4.防DDOS、cc攻击, 限制单IP并发请求连接
5.禁⽌通过IP地址访问,禁⽌恶意域名解析,只允许域名访问
6.限制上传资源⽬录被程序访问,防⽌⽊⻢⼊侵系统#性能
1.gzip压缩
2.expires静态⽂件缓存
3.调整⽹络IO模型,调整Nginx worker进程的最⼤连接数
4.配置错误⻚⾯,根据错误代码指定⽹⻚反馈⽤户
Nginx架构总结
基于Nginx中间件的架构
1.了解需求(定义Nginx在服务体系中的⻆⾊)静态资源服务的功能设计类型分类(视频、图⽚、html)浏览器缓存防盗链流量限制防资源盗⽤压缩(压缩模式, 压缩⽐例, 压缩类型)代理服务协议类型正向代理反向代理负载均衡代理缓存头信息处理Proxy_PassLNMP LNMT 等动静分离
2.设计评估硬件 CPU、内存、磁盘 8H16 1G系统(⽤户权限、⽇志⽬录存放)代理服务/负载均衡 (CPU、内存)静态资源服务(硬盘容量、硬盘转速)动态资源服务(硬盘转速、读写效率)缓存资源服务(SSD固态)
3.配置注意事项合理配置了解原理http协议原理http状态原理操作系统原理关注⽇志⽇志是否有打开是否有对应请求请求状态码信息符合错误⽇志信息吐出来错误⽇志内容和含义