Nginx配置try_files实践二
本文内容承接《Nginx配置try_files实践一》
1. 环境:
OS:Ubuntu 15.10
nginx:nginx/1.9.3 (Ubuntu)
假设有三台虚拟机db1(IP:192.168.68.21)/db2(IP:192.168.68.22)/db3(IP:192.168.68.23),通过try_files等配置,使三台机器的/data/www/upload合集组成网络资源,并且支持HTTPS请求但SSL证书未认证。(注:未验证合法证书的场景)
设计思路如下:

若请求到db2:
- 检索db2是否存在目标资源,若存在则返回,否则请求通过db1-proxy重定向到db1
- 检索db1是否存在目标资源,若存在则返回,否则返回404
- 把404重定向到db3
- 检索db3是否存在目标资源,若存在则返回,否则返回404
- 请求结束
若请求到db1/db3同理。
2. 配置三台机器nginx默认配置
略过具体过程,注意事项
- 配置日志格式
- 生成SSL证书并上传(/etc/nginx/server.crt, /etc/nginx/server.key)
3. 配置db1
- /etc/nginx/conf.d/db1.test.com.conf
server{
listen ;
server_name db1.test.com;
listen ssl;
ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
error_page /.html;
access_log /var/log/nginx/db1_access.log main;
error_log /var/log/nginx/db1_error.log;
location /upload
{
root /data/www;
try_files $uri @db2;
}
location @db2{
proxy_pass http://192.168.68.22:8000/proxy$uri;
proxy_intercept_errors on;
recursive_error_pages on;
error_page = @db3;
}
location @db3{
proxy_pass http://192.168.68.23:8000/proxy$uri;
}
}
- /etc/nginx/conf.d/db1-proxy.test.com.conf
server{
listen ;
server_name db1-proxy.test.com;
error_page /.html;
access_log /var/log/nginx/db1_access.log main;
error_log /var/log/nginx/db1_error.log;
location /proxy/upload
{
alias /data/www/upload;
}
}
- 重启nginx
4. 配置db2
- /etc/nginx/conf.d/db2.test.com.conf
server{
listen ;
server_name db2.test.com;
listen ssl;
ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
error_page /.html;
access_log /var/log/nginx/db2_access.log main;
error_log /var/log/nginx/db2_error.log;
location /upload
{
root /data/www;
try_files $uri @db1;
}
location @db1{
proxy_pass http://192.168.68.21:8000/proxy$uri;
proxy_intercept_errors on;
recursive_error_pages on;
error_page = @db3;
}
location @db3{
proxy_pass http://192.168.68.23:8000/proxy$uri;
}
}
- /etc/nginx/conf.d/db2-proxy.test.com.conf
server{
listen ;
server_name db2-proxy.test.com;
error_page /.html;
access_log /var/log/nginx/db2_access.log main;
error_log /var/log/nginx/db2_error.log;
location /proxy/upload
{
alias /data/www/upload;
}
}
- 重启nginx
5. 配置db3
- /etc/nginx/conf.d/db3.test.com.conf
server{
listen ;
server_name db3.test.com;
listen ssl;
ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
error_page /.html;
access_log /var/log/nginx/db3_access.log main;
error_log /var/log/nginx/db3_error.log;
location /upload
{
root /data/www;
try_files $uri @db1;
}
location @db1{
proxy_pass http://192.168.68.21:8000/proxy$uri;
proxy_intercept_errors on;
recursive_error_pages on;
error_page = @db2;
}
location @db2{
proxy_pass http://192.168.68.22:8000/proxy$uri;
}
}
- /etc/nginx/conf.d/db3-proxy.test.com.conf
server{
listen ;
server_name db3-proxy.test.com;
error_page /.html;
access_log /var/log/nginx/db3_access.log main;
error_log /var/log/nginx/db3_error.log;
location /proxy/upload
{
alias /data/www/upload;
}
}
- 重启nginx
6. 创建测试文件
| server name | location | url |
| db1 | /data/www/upload/db1.html | https://db1.test.com/upload/db1.html |
| /data/www/upload/db1/test.html | https://db1.test.com/upload/db1/test.html | |
| db2 | /data/www/upload/db2.html | https://db2.test.com/upload/db2.html |
| /data/www/upload/db2/test.html | https://db2.test.com/upload/db2/test.html | |
| db3 | /data/www/upload/db3.html | https://db3.test.com/upload/db3.html |
| /data/www/upload/db3/test.html | https://db3.test.com/upload/db3/test.html |
7. 配置本地host
192.168.68.21 db1.test.com
192.168.68.21 db1-proxy.test.com
192.168.68.22 db2.test.com
192.168.68.22 db2-proxy.test.com
192.168.68.23 db3.test.com
192.168.68.23 db3-proxy.test.com
8. 访问结果
| url | http status |
| https://db3.test.com/upload/db1.html | 200 |
| http://db3.test.com/upload/db1.html | 200 |
| https://db3.test.com/upload/db1/test.html | 200 |
| http://db3.test.com/upload/db1/test.html | 200 |
| https://db3.test.com/upload/db2.html | 200 |
| http://db3.test.com/upload/db2.html | 200 |
| https://db3.test.com/upload/db2/test.html | 200 |
| http://db3.test.com/upload/db2/test.html | 200 |
| https://db3.test.com/upload/db3.html | 200 |
| http://db3.test.com/upload/db3.html | 200 |
| https://db3.test.com/upload/db3/test.html | 200 |
| http://db3.test.com/upload/db3/test.html | 200 |
| https://db3.test.com/upload/db3/test1.html | 404 |
| http://db3.test.com/upload/db3/test1.html | 404 |
| https://db3.test.com/upload/dbfdsafas | 404 |
| http://db3.test.com/upload/dbfdsafas | 404 |
Nginx配置try_files实践二的更多相关文章
- Nginx配置try_files实践一
参考资料: http://linuxplayer.org/2013/06/nginx-try-files-on-multiple-named-location-or-serverhttp://stac ...
- MyBatis基本配置和实践(二)
一.前言 从上一篇文章的junit单元测试环节可以看到,每一次调用MyBatis需要先加载SqlMapConfig.xml文件,再通过SqlSessionFactoryBuilder创建SqlSess ...
- Spring MVC基本配置和实践(二)
1. springmvc: 是一个表现层框架,作用是从请求中接收传入的参数,将处理后的结果数据返回给页面展示 2. ssm整合: 1)Dao层 pojo.mapper接口.mapper映射文件(使用逆 ...
- nginx配置反向代理支持session
Nginx反向代理tomcat,很是方便,但是也有些细节的问题需要注意:今天遇到了这样一个问题,tomcat中路径“host/web1”,nginx中直接“host/”代理,这时候session就无法 ...
- Nginx配置支持https协议-应用实践
Nginx配置支持https协议-应用实践 https简介 HTTPS 是运行在 TLS/SSL 之上的 HTTP,与普通的 HTTP 相比,在数据传输的安全性上有很大的提升. TLS是传输层安全协议 ...
- Nginx反向代理与负载均衡应用实践(二)
Nginx反向代理与负载均衡应用实践(二) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...
- 前后端分离项目 nginx配置实践
新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源. 搜索nginx配置大部分都通过url前缀进行转发来做前后端分离,不适用目前项目. 说明 前 ...
- centos7 nginx配置httpsCenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx环境部署(二)
1.yum安装nginx 下载对应当前系统版本的nginx包(package) # wget http://nginx.org/packages/centos/7/noarch/RPMS/ngin ...
- Nginx 配置指令的执行顺序(二)
我们前面已经知道,当 set 指令用在 location 配置块中时,都是在当前请求的 rewrite 阶段运行的.事实上,在此上下文中,ngx_rewrite 模块中的几乎全部指令,都运行在 rew ...
随机推荐
- C51 继电器 个人笔记
一句话 小电流控制大电流的开关. 电路图 给J2端口一个低电平,三极管导通,线圈吸合 一般用P1^4口连接 #include <reg51.h> #define u16 unsigned ...
- 全文搜索(A-5)-推荐算法
基于内容的推荐算法: 协同过滤推荐算法: 混合推荐算法: 基于内容的推荐算法做了如下假设:用户会喜欢和原来喜欢的物品相类似的项目.
- hihoCoder#1119 小Hi小Ho的惊天大作战:扫雷·二
原题地址 没有复杂算法,就是麻烦,写起来细节比较多,比较考验细心,一次AC好开心. 代码: #include <iostream> #include <vector> #inc ...
- 【ZJOI2017 Round1后记】
2017.4.1: NOIP+Round1综合成绩出来,标准分离续命线差了80分,果然还是联赛坑挖太大了…… 不管怎么说能续命的话还是要试一下的…… 发毒誓:Round2前不打手游,不看NGA,不看星 ...
- Linux下汇编语言学习笔记3 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 从零开始写STL-内存部分-内存分配器allocator
从零开始写STL-内存部分-内存分配器allocator 内存分配器是什么? 一般而言,c++的内存分配和释放是这样操作的 >>class Foo{ //...}; >>Foo ...
- codevs——1006 等差数列
1006 等差数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定n(1<=n< ...
- 洛谷——P1044 栈
P1044 栈——卡特兰数 题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈) ...
- maven的安装与环境变量配置
1.下载maven 地址:http://maven.apache.org/download.cgi 点击下载 apache-maven-3.2.1-bin.zip. 2.安装配置,假设maven 解压 ...
- 洛谷 P4379 [USACO18OPEN]Lemonade Line
P4379 [USACO18OPEN]Lemonade Line 题目描述 这是农场上一个炎热的夏日,Farmer John要给他的 NN 头奶牛发柠檬汽水了!所有的 NN 头奶牛(方便起见,编号为 ...