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 解压: ...
随机推荐
- 转---Python——numpy random类
numpy中利用random类获取随机数. numpy.random.random() 生成随机浮点数 默认为生成一个随机的浮点数,范围是在0.0~1.0之间,也可以通过参数size设置返回数据的si ...
- ZooKeeper基础
======================================ZooKeeper 背景======================================ZooKeeper 是一 ...
- docker 系列 - 基础镜像环境和Docker常用命令整理
=======================docker 基础镜像环境 alpine=======================可以使用 docker search 命令搜索指定的 image, ...
- 01.Redis 初体验
0. Redis安装 官网下载Redis 解压缩 make make install 安装后的执行命令被保存在了/usr/local/bin目录下 1. 配置文件:redis.conf daemoni ...
- Python中json一点小知识
import json dic={ "name":"杨林" } ret=json.dumps(dic,ensure_ascii=False) #因为json.d ...
- js强制将页面放到最大
<!DOCTYPE html> <html> <head> <title></title> <script language=&quo ...
- Django之CRM项目Day3-客户展示及分页
1.展示客户 模板的查找顺序: 先找全局的templates--> 按照app的注册顺序找templates中的文件 使用admin添加数据: 创建超级用户 python manage.py ...
- python 装饰器前之闭包和装饰器
装饰器: 一, 例如: # vim yue7.py def foo(): print ("fool-------------------") foo() 运行: [root@l ...
- vue之v-for循环的使用
- Vue中的双向数据绑定简单介绍
1. 文本框绑定v-module <div id="app"> <input type="text" v-model="msg&qu ...