参考资料:

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. Discuz 论坛修改admin账户密码

    打开Navicat for MySQL 找到数据表 pre_ucenter_members 把密码修改为123456789 password:047099adb883dc19616dae0ef2adc ...

  2. Spring框架中 配置c3p0连接池

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  3. java获取类的全类名----类名.class.getName()的作用是获取这个类的全类名

    类名.class.getName()的作用是获取这个类的全类名

  4. xtu summer individual 6 B - Number Busters

    Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  5. 九度oj 题目1068:球的半径和体积

    题目1068:球的半径和体积 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6148 解决:2269 题目描述: 输入球的中心点和球上某一点的坐标,计算球的半径和体积 输入: 球的中心点和 ...

  6. JavaEE JDBC RowSet行集

    RowSet行集 @author ixenos 应用背景 1.基于结果集的缺点:在与用户的整个交互过程中,必须始终与数据库保持连接 后果:当用户长时间离开时,数据库连接长时间被占用,而这属于稀缺资源: ...

  7. hihoCoder#1062 最近公共祖先·

    原题地址 A和A的共同祖先是A,即使A没有在之前的家谱中出现过!被这个坑了,WA了很久... 比如:小头爸爸是大头儿子他爹,问:隔壁王叔叔和隔壁王叔叔的最近祖先是谁?,答:隔壁王叔叔. 代码: #in ...

  8. Codeforces913E. Logical Expression

    现有串x=11110000,y=11001100,z=10101010,通过这三个串只用与或非三种操作到达给定的串,优先级非>或>与,可以加括号,问表达式最短的里面字典序最小的是谁,有&l ...

  9. AjaxFileUpload文件上传组件(php+jQuery+ajax)

    jQuery插件AjaxFileUpload可以实现ajax文件上传,下载地址:http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupl ...

  10. 剑指Offer —— BFS 宽度优先打印

    https://www.nowcoder.net/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tqId=11175&tPage= ...