本文内容承接《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实践二的更多相关文章

  1. Nginx配置try_files实践一

    参考资料: http://linuxplayer.org/2013/06/nginx-try-files-on-multiple-named-location-or-serverhttp://stac ...

  2. MyBatis基本配置和实践(二)

    一.前言 从上一篇文章的junit单元测试环节可以看到,每一次调用MyBatis需要先加载SqlMapConfig.xml文件,再通过SqlSessionFactoryBuilder创建SqlSess ...

  3. Spring MVC基本配置和实践(二)

    1. springmvc: 是一个表现层框架,作用是从请求中接收传入的参数,将处理后的结果数据返回给页面展示 2. ssm整合: 1)Dao层 pojo.mapper接口.mapper映射文件(使用逆 ...

  4. nginx配置反向代理支持session

    Nginx反向代理tomcat,很是方便,但是也有些细节的问题需要注意:今天遇到了这样一个问题,tomcat中路径“host/web1”,nginx中直接“host/”代理,这时候session就无法 ...

  5. Nginx配置支持https协议-应用实践

    Nginx配置支持https协议-应用实践 https简介 HTTPS 是运行在 TLS/SSL 之上的 HTTP,与普通的 HTTP 相比,在数据传输的安全性上有很大的提升. TLS是传输层安全协议 ...

  6. Nginx反向代理与负载均衡应用实践(二)

    Nginx反向代理与负载均衡应用实践(二) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...

  7. 前后端分离项目 nginx配置实践

    新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源. 搜索nginx配置大部分都通过url前缀进行转发来做前后端分离,不适用目前项目. 说明 前 ...

  8. 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 ...

  9. Nginx 配置指令的执行顺序(二)

    我们前面已经知道,当 set 指令用在 location 配置块中时,都是在当前请求的 rewrite 阶段运行的.事实上,在此上下文中,ngx_rewrite 模块中的几乎全部指令,都运行在 rew ...

随机推荐

  1. IIS 注册.NET Framework 4.0 命令

    cmd执行以下命令 32位Windows:C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 64位Windows:C ...

  2. hdu1978

          {          scanf(          {         scanf(         ;i<n;i++) ;j<m;j++)         scanf(   ...

  3. android 上AES解密是报错javax.crypto.BadPaddingException: pad block corrupted

    网上看到两种方法: 1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES"); private sta ...

  4. 自定义日志工具LogUtil

    package com.pingyijinren.test; import android.util.Log; /** * Created by Administrator on 2016/5/20 ...

  5. 使用GSON解析JSON文件

    package com.pingyijinren.test; /** * Created by Administrator on 2016/5/19 0019. */ public class App ...

  6. POJ 2456_Aggressive cows

    题意: 给定N个位置,把C头牛分别放入,求相邻两头牛的最大距离. 分析: 即为求两头牛之间最小距离的最大值.二分搜索答案. 代码: #include<iostream> #include& ...

  7. Redis2019年3.22

    redis缓存技术学习 一. redis基础配置 1. redis简介 1.1 redis 是c语言编写的一个缓存服务器, 是一个内存版本的nosql非关系型数据,大概11w/s访问处理. 数据都在本 ...

  8. Ubuntu 16.04安装***-qt5

    上一篇文章http://www.cnblogs.com/EasonJim/p/7133097.html中,第5步安装本地代理源服务器使用的是electron-ssr,发觉这个东西难配且难用,非常不建议 ...

  9. 我的arcgis培训照片6

    来自:http://www.cioiot.com/successview-556-1.html

  10. c++学习 - int 和 string 的相互转换

    在C++中会碰到int和string类型转换的. string -> int 首先我们先看两个函数: atoi 这个函数是把char * 转换成int的.应该是属于标准库函数.在想把string ...