Nginx常⻅问题

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;done

2.准备多份相同 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 -t

3.测试访问效果
[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/java

2.配置 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 reload

3.测试 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.html

alias 路径配置
[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_Pass
		LNMP LNMT 等
		动静分离
2.设计评估
	硬件 CPU、内存、磁盘 8H16 1G
	系统(⽤户权限、⽇志⽬录存放)
	代理服务/负载均衡 (CPU、内存)
	静态资源服务(硬盘容量、硬盘转速)
	动态资源服务(硬盘转速、读写效率)
	缓存资源服务(SSD固态)
3.配置注意事项
	合理配置
	了解原理
		http协议原理
		http状态原理
		操作系统原理
	关注⽇志
		⽇志是否有打开
		是否有对应请求
		请求状态码信息符合
		错误⽇志信息吐出来
		错误⽇志内容和含义
posted @ 2025-03-15 18:44  basickill  阅读(13)  评论(0)    收藏  举报