nginx location实战
nginx location高级实战
location是nginx的核心重要功能,可以设置网站的访问路径,一个web server会有多个路径,那么location就得设置多个。
Nginx的locaiton作用是根据用户请求的URI不同,来执行不同的应用。
针对用户请求的网站URL进行匹配,匹配成功后进行对应的操作。
官网文档
https://nginx.org/en/docs/http/ngx_http_core_module.html#location
1.语法介绍
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location 官网用法 location = / {
[ configuration A ]
} location / {
[ configuration B ]
} location /documents/ {
[ configuration C ]
} location ^~ /images/ {
[ configuration D ]
} location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
} 测试用法,如果定义了如上的5个location,则 http://yuchaoit.cn/ 匹配A http://yuchaoit.cn/hello 匹配B http://yuchaoit.cn/documents/hello 匹配C http://yuchaoit.cn/images/葫芦娃.gif 匹配D http://yuchaoit.cn/documents/德玛西亚.gif 匹配E
2.location语法优先级
优先级从高到低
匹配符 | 匹配规则 | 优先级 |
---|---|---|
= | 定义 URI 和位置的精确匹配。 | 1 |
^~ | 以某个字符串开头,不检查正则 | 2 |
~ | 区分大小写的正则匹配 | 3 |
~* | 不区分大小写的正则匹配 | 4 |
3.测试location实战
# 配置文件如下
server {
listen 22333;
server_name _; # 最低级匹配,不符合其他locaiton就来这
# 属于通用url规则
location / {
return 200 "location / \n";
} # 优先级最高,等于号后面可以指定url
location = / {
return 200 "location = / \n";
} #以/documents/开头的url,来这里,如符合其他locaiton,则以其他优先
location /documents/ {
return 200 "location /documents/ \n";
} #匹配任何以/images/开头的请求,不匹配正则
location ^~ /images/ {
return 200 "location ^~ /images/ \n";
} #匹配任何以.gif结尾的请求,支持正则
location ~* \.(gif|jpg|jpeg)$ {
return 200 "location ~* \.(gif|jpg|jpeg) \n";
} access_log off; }
4.客户端测试访问
[root@master-61 ~]#
# 精确匹配
[root@master-61 ~]#curl 10.0.0.9:22333
location = /
[root@master-61 ~]#
# 依然是精确匹配
[root@master-61 ~]#curl 10.0.0.9:22333/
location = /
[root@master-61 ~]#
# 没有满足的条件,因此匹配 /
[root@master-61 ~]#curl 10.0.0.9:22333/yuchaoit
location /
[root@master-61 ~]#
# 没有满足的条件,因此匹配 / ,这里注意结尾的斜线
[root@master-61 ~]#curl 10.0.0.9:22333/documents
location /
[root@master-61 ~]# # 符合匹配规则,匹配到了location /documents/
[root@master-61 ~]#curl 10.0.0.9:22333/documents/
location /documents/
[root@master-61 ~]#
# 符合匹配规则,匹配到了location /documents/
[root@master-61 ~]#curl 10.0.0.9:22333/documents/yuchaoit.html
location /documents/
[root@master-61 ~]# # 依然是没有符合的规则,默认匹配 /
[root@master-61 ~]#curl 10.0.0.9:22333/yuchaoit/documents/yuchaoit.html
location /
[root@master-61 ~]# # 通过正则表示匹配内容,只要是.jpg结尾
[root@master-61 ~]#curl 10.0.0.9:22333/yuchaoit/documents/yuchaoit.jpg
location ~* \.(gif|jpg|jpeg)
[root@master-61 ~]# # 即使前面匹配到了/documents/,但是结尾符合jpg优先级更高
[root@master-61 ~]#curl 10.0.0.9:22333/documents/yuchaoit.jpg
location ~* \.(gif|jpg|jpeg) # 除非结尾文件名不符合,因此匹配到/documents/
[root@master-61 ~]#curl 10.0.0.9:22333/documents/yuchaoit.jpgggg
location /documents/
[root@master-61 ~]#
[root@master-61 ~]# # 证明 ^~ 优先级 大于 ~*
[root@master-61 ~]#curl 10.0.0.9:22333/images/yuchaoit.jpgggg
location ^~ /images/
5.实际工作使用
实际工作中,会有至少3个匹配规则如下,需要同学们学习了nginx负载均衡即可理解,以及具体的网站部署实践。 # 1.必选规则,设置反向代理,官网也推荐该用法,可以加速处理,因为首页会频繁被访问
# 该location 一般直接设置反向代理,转发给后端应用服务器,或者是静态页;
location = / {
proxy_pass http://yuchaoit.cn;
} # 2.静态文件处理,nginx强项
# 两个模式,二选一即可
# 这个表示当用户请求是 http://yuchaoit.cn/static/hello.css 这样的请求时
# 进入/www目录下,寻找static文件夹, 也就是/www/static/hello.css文件
location ^~ /static/ {
root /www/;
} # 这个表示请求是以如下静态资源结尾的,进入到/www/下寻找该文件
#匹配任何以.gif结尾的请求,支持正则
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /www/;
} # 3.还有就是通用规则,用于处理未定义的url,默认匹配
# 一般网站除了静态文件的请求,默认就是动态请求,因此直接转发给后端
location / {
proxy_pass http://my_django:8080/;
}
6. location的root和alias
nginx的location路径匹配功能
1.匹配用户的url
2.匹配到之后决定动作,代理转发,或者设置网页根目录,提供静态数据。 静态页面的设置,就得设置linux中的文件路径,nginx提供了2个参数
root和alias 语法:
1.root是定义最上层目录,目录路径结尾的斜线可有可无;
2.alias是定义目录别名,结尾必须以 "/" 结束,否则找不到文件。
7.root、alias实践。
# 1.例如目前有一个静态数据目录 /static
# 静态图片数据如下
[root@web-9 /www]#ls /www/static/cai1.jpg
/www/static/cai1.jpg # 2.前端网页html文件
[root@web-9 /www]#cat /www/index.html
welcome chaoge linux course. <img src='/static/cai1.jpg'> # 3.nginx测试配置文件(分别测试root、和alias两种写法)
[root@web-9 /etc/nginx/conf.d]#cat movie.yuchaoit.conf
server {
listen 80;
server_name movie.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/movie.yuchaoit.log;
error_log /var/log/nginx/error.movie.yuchaoit.log;
error_page 404 https://error.taobao.com/app/tbhome/common/error.html;
location / {
root /www;
index index.html index.htm;
} location /static/ {
root /www;
} # location /static/ {
# alias /www/static/;
#} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
8.图解root、alias
location后面的url到底如何写
先记住语法,location 后面支持的四个符号
# 先看懂如下语法!!!
# 先看懂如下语法!!!
# 先看懂如下语法!!! location = /xixi/ {} # 精确匹配 example.com/xixi/ location ^~ /static/ {}
# 不支持正则,完全的字符串匹配 example.com/static/ location ~* /media/ {}
# 不区分大小写的正则匹配
example.com/media/xxxxxxxxxxxxxxx
example.com/Media/xxxxxxxxxxxxxxx
example.com/MEDIA/xxxxxxxxxxxxxxx location ~ /media/ {}
example.com/media/xxxxxxxxxxxxxxx location url {} # 比如 # 最低优先级
location / {} example.com/xxixixix
example.com/hahaha
example.com/aoligei
example.com/你谁啊 # 优先级也很低的,没有符号,优先级就很低 # 如果你上面有基于符号的location匹配,优先级高于这里
# 只是带你看看,这个写法,能匹配到什么url
# 通用型匹配
# 一般情况下,不会过多的去考虑,大小写的问题, 你知道这个语法就行
# 这个location不是你运维先去定义的
# 你们公司的官网,可以通过什么URL去访问,这是开发工程师去写代码限制好的!!! ############################################################
# 根据开发提供的网站说明书,去写你的nginx location匹配!!!
# 你要达到的能力就是,吧老师说的语法,看懂,以及,我也给大家提供后续的生产级别的nginx部署案例,你就看懂了! ############################################################
location /static/ {} example.com/static/aaaaaaaaaaaaaa
example.com/static/bbbbbbbbbbbbbbbbbbb
example.com/static/xcccccccccccc # 你要注意,这个url结尾的 斜线,尽量,尽量给写上 location /AAA/ {} # 听懂 1 不懂 2 location /BBB/ {} # 看懂 扣 6 不懂 7 ,你学过,练过的写法,就是这些了。 记不住意思,正常,但是你学过了,看笔记,就得知道他们的区别
1.location url {} ,是运维,要配合开发去写的。不是你自己瞎琢磨的。 # 开发写的url,什么意思呢?
# 你,获取一个开发写好的源代码,这个源代码里面,设计好了url的规则
#你的nginx要按照这个规则去写, 后面学到jumpserver,你就知道了,例如如下的生产级别nginx设置 # 这些配置,是为了让nginx,找到,匹配堡垒机,每一个组件的设置,请看 server {
listen 80;
# server_name www.yuchaoit.cn; client_max_body_size 5000m; 文件大小限制 # Luna 配置
# 经过实测,这个v12版本,只能http://10.0.0.61:4200/luna/这样去访问,前端这里有点难处理。 # myjumpserver.com/luna/aaaaaaaaaaaaaaa
# myjumpserver.com/luna/bbbbbbbbbbbb
location /luna/ {
proxy_pass http://luna:4200;
} # Core data 静态资源
# myjumpserver.com/media/replay/aaaaaaaaaaaaaaaaaaa
# myjumpserver.com/media/replay/bbbbbbbbbbbb location /media/replay/ {
add_header Content-Encoding gzip;
root /opt/jumpserver-v2.12.0/data/;
} # myjumpserver.com/media/aaaaaaa
# myjumpserver.com/media/bbbbbb
# myjumpserver.com/media/cccc
location /media/ {
root /opt/jumpserver-v2.12.0/data/;
} # 只要你部署这个堡垒机产品,人家产品的html网页中,发送了大量的请求是如下,就进入这些location去找资源
# myjumpserver.com/static/xxxxxxxxxxxxxxxx
location /static/ {
root /opt/jumpserver-v2.12.0/data/;
} # KoKo Lion 配置
# 关于基础组件的,反向代理了
# 只要你的url,访问的是 myjumpserver.com/koko/xxxxxx
location /koko/ {
proxy_pass http://koko:5000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} # lion 配置
# myjumpserver.com/lion/xxxxxxxxx
location /lion/ {
proxy_pass http://lion:8081;
proxy_buffering off;
proxy_request_buffering off;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_ignore_client_abort on;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 6000;
} # Core 配置
# myjumpserver.com/ws/xxxxxxx
location /ws/ {
proxy_pass http://core:8070;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# myjumpserver.com/api/xxxxxx
location /api/ {
proxy_pass http://core:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# myjumpserver.com/core/xxxxxx
location /core/ {
proxy_pass http://core:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} # 前端 Lina
# myjumpserver.com/ui/xxxxxxx
location /ui/ {
proxy_pass http://lina:9528;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 通用型匹配,上述的url,都匹配不到,就进入到这里!!!
# 这里具体意思,等我讲解堡垒机时候,再说
#但是这个写法,你应该已经看懂了!!!
#看不懂,就是你练习的不够多!!!
location / {
rewrite ^/(.*)$ /ui/$1 last;
}
} # 为什么要写这些location,是人家堡垒机,定义好了这些功能路径
#你 要在前端部署一个nginx作为代理的,话,得基于这些url,去对应的写location。。
nginx location实战的更多相关文章
- nginx 入门实战
nginx入门实战 nginx 安装与卸载 下载安装 进入 http://nginx.org/en/download.html 下载自己想要的版本,我选择的stable版本 tar -zxvf ngi ...
- Docker深入浅出系列 | 单机Nginx+Springboot实战
目录 Nginx+Springboot实战 前期准备 实战目标 实战步骤 创建Docker网络 搭建Mysql容器 搭建额度服务集群 搭建Nginx服务 验证额度服务 附录 Nginx+Springb ...
- Nginx location 匹配顺序整理
Nginx location模块整理 具体的Nginx安装就不在这里描述了,这里只是为了对location的描述 Nginx环境 a. 查看当前系统cat /etc/redhat-release [r ...
- Nginx Location配置总结
Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...
- nginx location配置
nginx location配置 location在nginx中起着重要作用,对nginx接收到的请求字符串进行处理,如地址定向.数据缓存.应答控制.代理转发等location语法location ...
- nginx location的配置
文章转自:http://www.ttlsa.com/nginx/nginx-location-configure/ location的语法配置规则: 语法规则: location [=|~|~*|^~ ...
- nginx location配置(URL)
语法规则: location [=|~|~*|^~] /uri/ { … }= 表示精确匹配,这个优先级也是最高的^~ 表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url ...
- Nginx location配置详细解释
nginx location配置详细解释 语法规则: location [=|~|~*|^~] /uri/ { - } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 ur ...
- nginx location匹配顺序及CI框架的nginx配置
Nginx location匹配顺序如下: 用前缀字符串定义的location规则对URI进行匹配测试. =号定义了精确的前缀字符串匹配,如果发现精确匹配则使用当前规则.否则继续下一步匹配. 匹配其它 ...
- nginx Location 语法基础知识
URL地址匹配是Nginx配置中最灵活的部分 Location 支持正则表达式匹配,也支持条件匹配,用户可以通过location指令实现Nginx对动丶静态网页的过滤处理. Nginx locatio ...
随机推荐
- 【阿里云采购季】3月采购完,IT运维躺赢一年
阿里云2020上云采购季正式上线啦!今年的采购季可以逛些啥? 采购季正式期时间: 3月2日-3月31日 在这段时间里,想买啥就买吧,别忘了把想买的产品加入购物车噢,特惠产品叠加购物车满减,更划算噢! ...
- Hologres揭秘:如何支持超高QPS在线服务(点查)场景
简介: 本期我们将揭秘Hologres如何支持超高QPS在线服务(点查)场景. Hologres(中文名交互式分析)是阿里云自研的一站式实时数仓,这个云原生系统融合了实时服务和分析大数据的场景,全面兼 ...
- dotnet ConditionalWeakTable 的底层原理
在 dotnet 中有一个特殊的类,这个类能够做到附加属性一样的功能.也就是给某个对象附加一个属性,当这个对象被回收的时候,自然解除附加的属性的对象的引用.本文就来聊聊这个类的底层原理 小伙伴都知道弱 ...
- kubenetes1.26中安装kubesphere3.4版本
一.安装前环境准备 # kubesphere官网:https://kubesphere.io/zh/docs/v3.4/introduction/what-is-kubesphere/ # 1.kub ...
- 揭露 FileSystem 引起的线上 JVM 内存溢出问题
作者:来自 vivo 互联网大数据团队-Ye Jidong 本文主要介绍了由FileSystem类引起的一次线上内存泄漏导致内存溢出的问题分析解决全过程. 内存泄漏定义(memory leak):一个 ...
- 将字节数组输入流拷贝成字节数组输出流,将ByteArrayInputStream转成ByteArrayOutputStream
/** 将 ByteArrayInputStream 拷贝成 ByteArrayOutputStream * 将 字节数组输入流 拷贝成 字节数组输出流 */ public static ByteAr ...
- ansible(11)--ansible的user和group模块
1. group模块 功能:管理被控端用户组: 主要参数如下: 参数 说明 name 指定创建的组名 gid 为组设置gid state 是否将组创建在远程主机上,创建:present(Default ...
- C#中的对象深拷贝和浅拷贝
目录 C#中的对象深拷贝和浅拷贝 概述 1. 浅拷贝 2. 深拷贝 总结 引用 C#中的对象深拷贝和浅拷贝 概述 在C#中,对象拷贝是指将一个对象的副本创建到另一个对象中.对象拷贝通常用于数据传输或创 ...
- 不同模式下删除Oracle数据表的三个实例
首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1 ...
- vue3.4中defineModel中默认值是复杂数据类型 (注意!!!)
const drillFields = defineModel<string[]>('drillFields', { get(val) { return reactive(val || [ ...