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 解压: ...
随机推荐
- POJ - 2057 The Lost House(树形DP+贪心)
https://vjudge.net/problem/POJ-2057 题意 有一只蜗牛爬上某个树枝末睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面,.现在这只蜗牛要求寻找 ...
- 060、在docker中使用flannel(2019-03-29 周五)
参考https://www.cnblogs.com/CloudMan6/p/7441188.html 配置docker 连接flannel 编辑host1的docker配置文件/etc/sys ...
- windows安装mysql示例
1. 下载mysql安装包,如: mysql-5.6.34-winx64.zip2. 解压安装包到指定目录,如:D盘,即:D:\mysql-5.6.34-winx643. 配置 cd D:\mysql ...
- Win32程序框架
// WinMsg.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #include " ...
- linux 中 如何 搜索 指定目录 下 指定文件 的 指定内容
开发时,经常遇到 全局查找某些代码 linux 中 如何 检索 某 目录下指定文件 的 指定内容如下: //.点为查找当前目录 下 的 所有 *.php 文件里 有 hello 的文件 find . ...
- Java入门系列Java NIO
jdk1.4中新加入的NIO,引入了通道与缓冲区的IO方式,它可以调用Native方法直接分配堆外内存,这个堆外内存就是本机内存,不会影响到堆内存的大小.
- 【51nod 1785】数据流中的算法
Description 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠标轨迹等特征计算用户对于网站的满意程度. 现有的统计工具只能统计某一个窗口中,用户的满意程 ...
- zabbix系列 ~ 自动监控多实例功能
一 场景 监控mongo的多实例端口二 目标 定制一套模板,根据不同的端口进行批量监控项的生成三 步骤 1 编写py脚本实现端口josin化输出,以便zabbix_server能进行识别 ...
- renren-security旧版本 分模块 的模块之间关系浅析
Maven结构,一个父模块 六个子模块 七个pom.xml: \git\renren-security\pom.xml <modules> <module>renren-com ...
- Javascript - ExtJs - TabPanel组件
示例 Ext.create('Ext.tab.Panel', { width: "100%", renderTo: "tabBox", ...