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 ...
随机推荐
- STM32——通用定时器基本定时功能
STM32——————通用定时器基本定时功能 1. ...
- linux-NMON监控
- windows10开启内置ubuntu系统,使用xshell连接
windows安装配置ubuntu系统内置子系统 官方文档:https://docs.microsoft.com/zh-cn/windows/wsl/about https://www.jianshu ...
- hihoCoder#1082 然而沼跃鱼早就看穿了一切
原题地址 字符串匹配+替换 注意替换串和原串长度是不等的,所以替换完还要进行收缩 可以顺带练习一下KMP 代码: #include <iostream> #include <cstr ...
- VS调试STL问题总结
---恢复内容开始--- 以前写代码总觉用自己写的东西比较牛逼,vector?stack?为什么不自己实现.后来才认识到这是个幼稚的想法!首先每次都自己实现是一种重复劳动:其次,自己写的话很难保证没有 ...
- World is Exploding 树状数组+离散化
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...
- MongoDB小结18 - find【查询条件$not】
not 是元条件句,可以用于任何条件之上,意为取反
- 镜像二叉树——剑指Offer
https://www.nowcoder.net/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&tPage= ...
- SaltStack学习笔记之安装zabbix_agentd(jinja和pillar)
一.环境说明 机器 IP 主机名 Master 192.168.0.23 minion.saltstack.com Minion 192.168.0.35 minion-node2.saltstack ...
- 理解C语言中指针的声明以及复杂声明的语法
昨天刚把<C程序设计语言>中"指针与数组"章节读完,最终把心中的疑惑彻底解开了.如今记录下我对指针声明的理解.顺便说下怎样在C语言中创建复杂声明以及读懂复杂声明. 本文 ...