Nginx常⻅问题

news/2025/3/15 18:45:47/文章来源:https://www.cnblogs.com/Basicskill/p/18774048

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状态原理操作系统原理关注⽇志⽇志是否有打开是否有对应请求请求状态码信息符合错误⽇志信息吐出来错误⽇志内容和含义

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/899321.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

石家庄地铁收费app

先建立一个项目,项目结构如图所示先编写app页面activity_main.xml,包括其输入框和按钮<TextViewandroid:id="@+id/tvStartStation"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="起点站&quo…

[WSNesting 设置说明] 数控钻加工限制-不使用加工限制-自动连线

版权声明本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者 BensonLaur 和本文原始地址:

Git更新子模块

1.问题 如图,我们从GitHub上克隆下来一个库,内部还有类似这种右边蓝色S的表示的是该工程的子模块部分,一般初始为空,导致错误2.解决 我们需要用git submodule update --recursive --remote命令去更新初始化我们的子模块

pycharm2024下载安装一键激活2099年

​软件包和激活工具下载地址,关地球号:QStockView,发送pycharm,立刻发给你,全免费; 激活之后如下: 激活方式,打开压缩包,然后找到下面的文件,双击打开提示成功,之后,就可以了;去重新打开pycharm,就可以了; 中文设置如下: 添加图片注释,不超过 140 字(可选…

日期问题中的格式控制

define _CRT_SECURE_NO_WARNINGS include <stdio.h> include <string.h> using namespace std; void NextDay(int &year, int &month, int &day) { //存储一下月份和天数的关系 int dayOfMonth[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int is…

Windows管理小工具

Windows 管理小工具 概述 Windows 管理小工具 是一个基于批处理脚本的多功能工具,旨在帮助用户快速管理 Windows 系统中的常见设置和功能。通过简单的菜单操作,用户可以轻松完成 Windows 11 右键菜单管理、桌面图标小箭头管理、卸载 Windows 11 小组件、安装 Office、激活 Wi…

2025.3.11 php登录,连接数据库

1,$GET和$POST的区别 2, 3, 4, 5,HTML不能直接从编辑器上打开

Vue2学习5-v-model原理、表单类组件封装及v-model简化、.sync修饰符 、ref和$refs、$nextTick、自定义指令(全局、局部)、插槽(默认、具名)及商品列表案例

Vue2 v-model原理 v-model本质就是一个语法糖(一种编程语言的语法特性,允许以更简洁、易读的方式表达某些操作) 例如在输入框中,是value属性和input事件的合并 作用:实现数据的双向绑定数据变,视图跟着变视图变,数据跟着变例: $event可以获取事件的形参 <template&g…

Vue2学习5-

Vue2 v-model原理 v-model本质就是一个语法糖(一种编程语言的语法特性,允许以更简洁、易读的方式表达某些操作) 例如在输入框中,是value属性和input事件的合并 作用:实现数据的双向绑定数据变,视图跟着变视图变,数据跟着变例: $event可以获取事件的形参 <template&g…

C# 23种计模式

以下是23种设计模式:

【阿里淘天】3月15日暑期实习机试-第一题-连续非空子数组

连续非空子数组 题面思路 正向求解的话,需要枚举所有的子数组,复杂度会来到\(O(n^3)\),完全不可行,在观察题目输入描述,\(a_i\)的取值范围非常小,故我们考虑反向求解(这也是非常经典的思路,无法直接计数,我们就计算每个答案的贡献是多少) 利用类似滑动窗口的思想,去…

python的基本运用(7)——函数(内置函数)

一、python的内建函数二、内置函数使用 (1)format()函数 1.定义:是一格式化字符串,该函数增强了字符串格式的功能. 2.基本语法是通过{}来代替一起拿% 3.案例 a.设置指定位置,默认暑顺序 hz="{}".format("dcs","18")print(hz) b.按照指定的索引…