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的使用的更多相关文章

  1. 前端开发掌握nginx常用功能之server&location匹配规则

    nginx主要是公司运维同学必须掌握的知识,涉及到反向代理.负载均衡等服务器配置.前端开发尤其是纯前端开发来说对nginx接触的并不多,但是在一些情况下,nginx还是需要前端自己来搞:例如我们公司的 ...

  2. nginx教程1:location 匹配规则

    worker_process # 表示工作进程的数量,一般设置为cpu的核数 worker_connections # 表示每个工作进程的最大连接数 server{} # 块定义了虚拟主机 liste ...

  3. Nginx日志参数、location匹配规则、设置密码

    1.三个参数 a)$http_referer:记录此次请求是从哪个链接访问过来的: 是直接访问,还是从其他网站跳转过来的. 例如:访问:http://www.etiantian.com/,其页面首页是 ...

  4. Nginx的alias与root的用法区别和location匹配规则

    1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...

  5. Nginx之Location匹配规则

    概述 经过多年发展,nginx凭借其优异的性能征服了互联网界,成为了各个互联网公司架构设计中不可获取的要素.Nginx是一门大学问,但是对于Web开发者来说,最重要的是需要能捋的清楚Nginx的请求路 ...

  6. Nginx之location 匹配规则详解

    有些童鞋的误区 1. location 的匹配顺序是“先匹配正则,再匹配普通”. 矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”.我这么说,大家一定会反驳我,因为按“先匹配普通, ...

  7. location 匹配规则 (NGINX)

    转:https://moonbingbing.gitbooks.io/openresty-best-practices/ngx/nginx_local_pcre.html location 匹配规则 ...

  8. Nginx中虚拟主机配置

    一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...

  9. [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向

    Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...

随机推荐

  1. 谈谈java程序代码保护及license设计

    理论上讲,不存在牢不可破的漏洞,只是时间和成本问题.通常我们认为的不可破解,说的是破解需要难以接受的时间和成本.对于java程序来说,class文件很容易被反编译,所以理论上而言,对java程序做li ...

  2. nohup命令执行退出后进程退出

    nohup命令常常用于让进程在后台执行.但是如果仅仅是执行: nohup command & 之后直接关闭终端的话,会发现之前已经启动进程也会退出.解决办法:nohup command &am ...

  3. 关于Unicode

    http://www.unicode.org/faq/utf_bom.html 有关UTF或编码表单的一般问题 Unicode是16位编码吗? Unicode文本可以以多种方式表示吗? 什么是UTF? ...

  4. C# 异步方法(AM)

    Ø  前言 C# Asynchronous Programming(异步编程)有几种实现方式,其中 Asynchronous Method(异步方法)就是其中的一种.异步方法是 C#5.0 才有的新特 ...

  5. 深度学习 ——style reconstruction

    多层神经网络的实质就是为了找出更复杂,更内在的features...图像的style, how to describe, impossible! 但是人眼却可以分辨. (参考论文 A Neural a ...

  6. python中的*和**的用途

    def function_with_one_star(*t):    print(t, type(t)) def function_with_two_stars(**d)    print(d, ty ...

  7. [C++]2-3 倒三角形

    /* 倒三角形(Triangle) 输入正整数n<=20,输出一个n层的倒等腰三角形. 0 ######### 9 = 2* n-1 1 ####### 7 = 2*(n-1)-1 2 #### ...

  8. Spark On Yarn报警告信息 WARN yarn.Client: Neither spark.yarn.jars nor spark.yarn.archive is set, falling back to uploading libraries under SPARK_HOME.

    1 贴出完整日志信息 // :: INFO client.RMProxy: Connecting to ResourceManager at hdp1/ // :: INFO yarn.Client: ...

  9. Java基础_0308:String类的常用方法

    取出指定索引的字符 -- 使用charAt()方法 public class StringDemo { public static void main(String args[]) { String ...

  10. struts2简单入门-关于Result标签Type属性的说明

    Result标签 作用 当action执行完毕,后要返回什么样的视图. Type属性 决定返回的是什么视图. struts-default.xml的Type属性的定义 <result-types ...