参考资料:

http://linuxplayer.org/2013/06/nginx-try-files-on-multiple-named-location-or-server
http://stackoverflow.com/questions/20426812/nginx-try-files-alias-directives

1. 环境:

OS:Ubuntu 15.10

nginx:nginx/1.9.3 (Ubuntu)

假设有两台虚拟机db1(IP:10.0.1.62)/db2(IP:10.0.1.63),通过try_files配置,使两台机器的/data/www/upload合集组成网络资源,设计思路如下:

若请求到db1:

  • 检索db1是否存在目标资源,若存在则返回,否则请求通过db2-proxy重定向到db2
  • 检索db2是否存在目标资源,若存在则返回,否则返回404
  • 请求结束

若请求到db2同理。

2. 配置两台机器的nginx.conf

...

http {

    log_format  main  '[$content_type]--[$remote_addr] - $remote_user [$time_local] "[$request]" '
'[$status] $body_bytes_sent [$request_body] "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for'; ... include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

3. 为db1添加/etc/nginx/conf.d/db1.conf

 server{
listen ;
server_name 10.0.1.62;
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 /proxy/upload
{
alias /data/www/upload;
} location @db2{
proxy_pass http://10.0.1.63/proxy$uri;
}
}

4. 为db2添加/etc/nginx/conf.d/db2.conf

 server{
listen ;
server_name 10.0.1.63;
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 /proxy/upload
{
alias /data/www/upload;
} location @db1{
proxy_pass http://10.0.1.62/proxy$uri;
}
}

5. 重新加载nginx配置

`service nginx reload`

6. 在db1创建测试文件

7. 在db2创建测试文件

8. 访问结果

http://10.0.1.62/upload/db1.html

http://10.0.1.62/upload/db2.html  

http://10.0.1.63/upload/db1.html?a=1  

http://10.0.1.63/upload/db2.html?bfdsfads^*()^&  

http://10.0.1.63/upload/db3.html  

http://10.0.1.62/upload/db1/test.html  

http://10.0.1.62/upload/db2/test.html  

http://10.0.1.63/upload/db1/test.html  

http://10.0.1.63/upload/db2/test.html  

http://10.0.1.63/upload/db3/test.html  

9. 引申

由于生产环境的特殊原因,用户的请求可能是HTTP/HTTPS协议,那么本文的配置有很大的缺陷,解决方案见《Nginx配置try_files实践二

Nginx配置try_files实践一的更多相关文章

  1. Nginx配置try_files实践二

    本文内容承接<Nginx配置try_files实践一> 1. 环境: OS:Ubuntu 15.10 nginx:nginx/1.9.3 (Ubuntu) 假设有三台虚拟机db1(IP:1 ...

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

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

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

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

  4. Nginx中的Rewrite的重定向配置与实践

    阅读目录 一:理解地址重写 与 地址转发的含义. 二:理解 Rewrite指令 使用 三:理解if指令 四:理解防盗链及nginx配置   简介:Rewrite是Nginx服务器提供的一个重要的功能, ...

  5. 使用nginx配置域名及禁止直接通过IP访问网站

    前段时间刚搭建好个人网站,一直没有关注一个问题,那就是IP地址也可以访问我的网站,今天就专门研究了一下nginx配置问题,争取把这个问题研究透彻. 1. nginx配置域名及禁止直接通过IP访问 先来 ...

  6. Nginx 配置简述

    不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...

  7. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  8. thinkphp nginx配置

    安装和配置: http://www.cnblogs.com/Bonker/p/4252588.html  spawn-fcgi 要先安装和启动:(已过时) sudo spawn-fcgi -a -u ...

  9. nginx配置入门

    谢谢作者的分享精神,原文地址:http://www.nginx.cn/591.html nginx配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一 ...

随机推荐

  1. LeetCode 121. Best Time to Buy and Sell Stock (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  2. ajax一个很好的加载效果

    推荐一个常用的jquery加载效果插件: 要引入这个插件的css和js: <link href="<%=path %>/css/showLoading.css" ...

  3. android开发里跳过的坑——button不响应点击事件

    昨天遇到一个头疼的问题,在手机上按钮事件都很正常,但是在平板上(横屏显示的状态),button点击事件不响应,代码简化如下: public class Test extends Activity im ...

  4. ZOJ - 3829 Known Notation(模拟+贪心)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...

  5. 使用idea编译spring-framework5.0源码

    自从迈入java开发这个行当,从来没有好好的研究过源码,深感惭愧,话不多说,今天上一篇使用idea编译spring5.0源码. 以下在win中构建和编译过程分为 jdk环境的配置 gradle的下载和 ...

  6. 18.10.7 POIN 模拟赛

    期望 :80+ +90+40=210+ 实际 :30+90+0=120 链接:https://www.nowcoder.com/acm/contest/175/A来源:牛客网 时间限制:C/C++ 1 ...

  7. 【SQL Server 学习系列】-- 清除SQL Server 2012连接过的服务器名称

    需要删除两个地方,删除前请注意备份! 1.  %AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin 2.  %A ...

  8. tomcat8.5.20配置https

    一.使用cmd下生成证书: d: cd d:/java/jdk/jdk1.8 keytool -v -genkey -alias tomcat -keyalg RSA -keystore D:\jav ...

  9. Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。

    Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.

  10. jmeter的Classpath即类或者jar包的搜索路径设置

    对于master-slave模式,插件和依赖都需要放到slave上才能生效,并且需要重启slave使插件生效 查看配置文件:apache-jmeter-3.1/bin/jmeter.propertie ...