nginx多虚拟主机优先级location匹配规则及tryfiles的使用
nginx多虚拟主机优先级location匹配规则及tryfiles的使用
.相同server_name多个虚拟主机优先级访问 .location匹配优先级 .try_files使用 .nginx的alias和root区别 .用什么方法传递用户的真实IP .相同server_name多个虚拟主机优先级访问 环境准备 [root@test8_hadoop_kaf conf.d]# cat server01.conf
server {
listen ;
server_name server01 es.chinasoft.com; location / {
root /opt/app/code1;
index index.html index.htm;
} error_page /50x.html; location = /50x.html {
root /usr/share/nginx/html;
}
}
[root@test8_hadoop_kaf conf.d]# cat server02.conf
server {
listen ;
server_name server02 es.chinasoft.com; location / {
root /opt/app/code2;
index index.html index.htm;
} error_page /50x.html; location = /50x.html {
root /usr/share/nginx/html;
}
} [root@test8_hadoop_kaf conf.d]# diff server01.conf server02.conf
3c3
< server_name server01 es.chinasoft.com;
---
> server_name server02 es.chinasoft.com;
6c6
< root /opt/app/code1;
---
> root /opt/app/code2; [root@test8_hadoop_kaf conf.d]# cat /opt/app/code1/index.html
<h1>server01</h1>
[root@test8_hadoop_kaf conf.d]# cat /opt/app/code2/index.html
<h1>server02</h1> 测试
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/index.html
<h1>server01</h1> 修改配置文件,重新加载nginx,再次测试发现访问server02了
[root@test8_hadoop_kaf conf.d]# mv server01.conf server03.conf
[root@test8_hadoop_kaf conf.d]# systemctl reload nginx
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/index.html
<h1>server02</h1> .location匹配优先级
= 进行普通字符精确匹配,也就是完全匹配
^~ 表示普通字符匹配,使用前缀匹配
~ \~* 表示执行一个正则匹配() 环境准备: nginx的配置
[root@test8_hadoop_kaf conf.d]# cat location_test.conf
server { listen ; server_name testserver01 es.chinasoft.com; root /opt/app; location = /code1/ {
rewrite ^(.*)$ /code1/index.html break;
} location ~ /code.* {
rewrite ^(.*)$ /code3/index.html break;
} location ^~ /code {
rewrite ^(.*)$ /code2/index.html break;
}
} 代码:
[root@test8_hadoop_kaf conf.d]# mkdir /opt/app/{code1,code2,code3} [root@test8_hadoop_kaf conf.d]# echo "<h1>code1</h1>" >> /opt/app/code1/index.html
[root@test8_hadoop_kaf conf.d]# echo "<h1>code2</h1>" >> /opt/app/code2/index.html
[root@test8_hadoop_kaf conf.d]# echo "<h1>code3</h1>" >> /opt/app/code3/index.html 测试:
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/code1/
<h1>server01</h1>
<h1>code1</h1> 注释掉code1部分 #location = /code1/ {
# rewrite ^(.*)$ /code1/index.html break;
#} 重新加载nginx,测试发现就匹配到了code2中
[root@test8_hadoop_kaf conf.d]# systemctl reload nginx
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/code1/
<h1>server02</h1>
<h1>code2</h1> .nginx中try_files的使用 环境准备:
nginx的配置
[root@test8_hadoop_kaf conf.d]# cat tryfiles_test.conf
server { listen ; server_name testserver01 es.chinasoft.com; root /opt/app; location / {
root /opt/app/code/cache;
try_files $uri @java_page;
} location @java_page {
proxy_pass http://127.0.0.1:9090;
} } /opt/app/code/cache/目录下的html页面
[root@test8_hadoop_kaf conf.d]# cat /opt/app/code/cache/jack.html
cache tomcat下的html页面
[root@test8_hadoop_kaf conf.d]# cat /data/yunva/test_tomcat8..37_9090/webapps/ROOT/jack.html
<html>
<head>
<meta charset="utf-8">
<title>server </title>
</head>
<body>
<h1>java page</h1>
</body>
</html> 测试:
[root@test8_hadoop_kaf cache]# curl http://es.chinasoft.com/jack.html
cache 将/opt/app/code/cache目录下的html页面重命名,模拟页面不存在,可以看到再次访问就到了@java_page
[root@test8_hadoop_kaf cache]# pwd
/opt/app/code/cache
[root@test8_hadoop_kaf cache]# mv jack.html jack.html.bak
[root@test8_hadoop_kaf cache]# curl http://es.chinasoft.com/jack.html
<html>
<head>
<meta charset="utf-8">
<title>server </title>
</head>
<body>
<h1>java page</h1>
</body>
</html> .nginx的alias和root区别
Root会做拼接路径处理,alias就不拼接,而是直接访问alias目录下的文件
nginx多虚拟主机优先级location匹配规则及tryfiles的使用的更多相关文章
- 前端开发掌握nginx常用功能之server&location匹配规则
nginx主要是公司运维同学必须掌握的知识,涉及到反向代理.负载均衡等服务器配置.前端开发尤其是纯前端开发来说对nginx接触的并不多,但是在一些情况下,nginx还是需要前端自己来搞:例如我们公司的 ...
- nginx教程1:location 匹配规则
worker_process # 表示工作进程的数量,一般设置为cpu的核数 worker_connections # 表示每个工作进程的最大连接数 server{} # 块定义了虚拟主机 liste ...
- Nginx日志参数、location匹配规则、设置密码
1.三个参数 a)$http_referer:记录此次请求是从哪个链接访问过来的: 是直接访问,还是从其他网站跳转过来的. 例如:访问:http://www.etiantian.com/,其页面首页是 ...
- Nginx的alias与root的用法区别和location匹配规则
1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...
- Nginx之Location匹配规则
概述 经过多年发展,nginx凭借其优异的性能征服了互联网界,成为了各个互联网公司架构设计中不可获取的要素.Nginx是一门大学问,但是对于Web开发者来说,最重要的是需要能捋的清楚Nginx的请求路 ...
- Nginx之location 匹配规则详解
有些童鞋的误区 1. location 的匹配顺序是“先匹配正则,再匹配普通”. 矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”.我这么说,大家一定会反驳我,因为按“先匹配普通, ...
- location 匹配规则 (NGINX)
转:https://moonbingbing.gitbooks.io/openresty-best-practices/ngx/nginx_local_pcre.html location 匹配规则 ...
- Nginx中虚拟主机配置
一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...
- [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向
Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...
随机推荐
- 百度编辑器ueditor 光标位置的坐标
项目需求: 输入某个字符时,弹出一个弹框 弹框位置跟随光标处 经查找和亲测,下面记录一下代码: // 下面计算坐标 let domUtils = UE.dom.domUtils let bk_star ...
- 对两个数求和的str_echo函数
void str_echo(int sockfd) { long arg1, arg2; ssize_t n; char line[MAXLINE]; for ( ; ; ) { ) { return ...
- java-solr solrj的使用
新建一个maven项目,引入依赖: <dependencies> <dependency> <groupId>org.apache.solr</groupId ...
- Docker 创建 mongo 容器
获取 docker 认证 mongo 镜像: docker pull mongo 创建运行 mongo 容器: docker run -d -it -p : --name mongo3 -m 512M ...
- nnet3中的数据类型
目标与背景 之前的nnet1和nnet2基于Component对象,是一个组件的堆栈.每个组件对应一个神经网络层,为简便起见,将一个仿射变换后接一个非线性表示为一层网络,因此每层网络有两个组件.这些旧 ...
- MLE
独立同分布的采样x1,x2,…,xn,θ为模型参数,f为我们所使用的模型.参数为θ的模型f产生上述采样可表示为 f(x1,x2,…,xn|θ)=πf(xi|θ) 已知的为x1,x2,…,xn,未知为θ ...
- Java基础_0205: 程序逻辑结构
使用if语句进行判断 public class TestDemo { public static void main(String args[]) { double score = 90.0; // ...
- spring-core 中 asm 包的作用
asm包中主要有以下这些类 其中, AnnotationVisitor类:是一个抽象类,定义在解析注解时会触发的事件,如解析到一个基本值类型的注解.enum值类型的注解.Array值类型的注解.注解值 ...
- CentOS6.8合并DVD1和DVD2作为本地yum源
转载自:http://www.study365.org/blog/45.html CentOS一般都会提供DVD1和DVD2两个镜像文件,形如CentOS-6.8-x86_64-bin-DVD1.is ...
- 《一头扎进SpringMvc视频教程》Rest风格的资源URL
5.@PathVariable和@RequestParam的区别 请求路径上有个id的变量值,可以通过@PathVariable来获取 @RequestMapping(value = "/ ...