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 ...
随机推荐
- MySQL Connector/Python 接口 (三)
本文参见这里. 使用缓冲的 cursor,下例给从2000年加入公司并且还在公司的员工薪水从明天起加15% from __future__ import print_function from dec ...
- Shrio Demo
package com.atguigu.shiro.helloworld; import org.apache.shiro.SecurityUtils; import org.apache.shiro ...
- 动态创建div(鼠标放上显示二维码)
最近的微信大行其道.各个网站上都给出的微信验证码,进行手机扫描加入. 怎么创建类似与点击鼠标弹出一个浮动的div显示二维码的这种效果. 1.首先制作好这样的图片,写css样式 <style ty ...
- sqlite3 新建数据库的过程
有些东西,很简单,不过有坑,就变复杂了.我先说最简单的方法,新建一个空的txt文档,然后把后缀改为db就可以了.-_-蛋疼,其实一开始我是不知道的,也是后来成功新建db后发现db为0kb才大胆地做了这 ...
- 【转】 Java中的IO整理
写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...
- noip模拟赛 星空
分析:非常神的一道题.迭代加深搜索+rand可以骗得20分.状压n的话只有24分,必须对问题进行一个转化. 在爆搜的过程中,可以利用差分来快速地对一个区间进行修改,把一般的差分改成异或型的差分: b[ ...
- 舒适的路线(codevs 1001)
题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤ ...
- 解决webview.getFavicon()返回值总是为空的问题
在webview中,我们需要获取网站的favicon.ico图标,但是默认状态下,WebChromeClient中的onReceivedIcon方法获取到的icon总是为null; webview.g ...
- POJ——T 2728 Desert King
http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27191 ...
- hdu3756(三分)
题意:三维坐标轴,有以原点为圆心,底面在xoy平面上,顶点在z轴上的圆锥,问圆锥的最小体积为多少才能完全覆盖空间里的所有点(n<=10000) 分析: 很容易想到转成二维问题,将其投影到xoz平 ...