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 解压: ...
随机推荐
- 前台ajax传参数,后台spring mvc用对象接受
第二种方法:利用spring mvc的机制,调用对象的get方法,要求对象的属性名和传的参数名字一致(有兴趣的同学看 springmvc源码) 1.将参数名直接写成对象的属性名 $.ajax({ ur ...
- HDU - 1540 Tunnel Warfare(线段树区间合并)
https://cn.vjudge.net/problem/HDU-1540 题意 D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 分析 线段树的区间内,我 ...
- 2018牛客网暑期ACM多校训练营(第一场)J Different Integers(树状数组)
题意 给出一串数字以及q次查询,每次查询l,r],要求求出[1,l]和[r,n]的所有不相同的数字个数. 分析 先对数组进行倍增,变为两倍长,然后查询就变成一个完整的区间.离线处理,按r从小到大排序, ...
- Socket远程调试日志之 SocketLog的简单实用
github地址:https://github.com/luofei614/SocketLog 更多信息看这里:https://www.bbsmax.com/A/8Bz8L9Nyzx/ tp5配置co ...
- Python正则的贪婪和非贪婪示例
贪婪匹配 import re info = """ saas12 [STREAM] codec_type=audio111 [/STREAM]-- [STREAM] co ...
- 用jQuery和Json实现Ajax异步请求
这里有两个例子,一个是关于登录验证的,一个是异步加载数据的 1.regist.jsp <%@ page language="java" import="java.u ...
- jqgrid again
之前有提及过jqgrid这个很不错的jquery grid, 非常适合企业MIS系统使用. 本文以一个显示学生成绩结果的页面, 来说明它的一些用法, 手写代码, 运行可能会报错, 不过思路是经过验证的 ...
- google 谷歌地图
https://www.cnblogs.com/yincheng/p/google-map.html https://blog.csdn.net/sinat_21189673/article/deta ...
- [C++]Linux之读取计算机网络数据[/proc/net/dev]
#include<stdlib.h> #include<stdio.h> #define BUFFER_SIZE 256 int main(){ FILE *stream; c ...
- html页面高度问题
首先,上图 说明 1. clientHeight大部分浏览器对 clientHeight 都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,即然是指可看到内 ...