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 ...
随机推荐
- phtyon,通过while循环简单的用户名和密码登录
_username='zhangxin' _password='abc123' _username1='zhaopeng' _password1='abc1234' _username2=" ...
- 【Codeforces 231C】To Add or Not to Add
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 把数组排个序, 显然优先用大的且小于枚举的数字a[i]的数字变成a[i] 那么肯定有一个范围j. 然后a[j~i-1]都能在k花费以内变成a[ ...
- HDU 1800 hash 找出现最多次数的字符串的次数
乘法hash: 这类hash函数利用了乘法的不相关性 int Hash(char *str){ int seed = 131 , value=0; while(*str != '\0'){ ...
- Spring Boot Jpa 表名小写转大写
今天在使用SpringBoot整合Hibernate后创建表,表名为小写,而在linux下,mysql的表名是区分大小写的,因此在我的数据表中,就出现了两个一样的表 act_id_user 和 AC ...
- request详究
本文主要是对在学习过程中遇到的request用法进行归纳总结,彻底的搞明白request在jsp中的作用. 百度百科的介绍如下: Request对象的作用是与客户端交互,收集客户端的Form.Cook ...
- iis站点内存泄漏问题分析
在一次上线过程中iis内存飙升,随后跟运维要到站点的dump文件,使用windbg分析了clr的内存分配,找到了问题的症结,先记录如下: 使用windbg加载dump文件 1.打开windbg,Fil ...
- NOIP 2009 潜伏者
P1071 潜伏者 题目描述 RR 国和 SS 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 SS 国的 RR 国间谍小 CC 终于摸清了 SS 国军用密码的编码规则: ...
- 怎么让Excel显示时间时候能把秒显示出来
Excel显示时间一般只显示年月日小时分钟怎么能够把秒也显示出来既如下显示 2007-04-11 12:00:00 将单元格格式设为"自定义",在"类型"框中输 ...
- scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld
scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld 学习了: http://blog.csdn.net/wangmuming/article/details/3407911 ...
- modem&NIC&sound card
Rate: Phone:8 k hz radio:22050 hz Digital Video camcorder; miniDV; DAT LP mode:32 k hz Audio CD MP ...