Nginx配置try_files实践一
参考资料:
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实践一的更多相关文章
- Nginx配置try_files实践二
本文内容承接<Nginx配置try_files实践一> 1. 环境: OS:Ubuntu 15.10 nginx:nginx/1.9.3 (Ubuntu) 假设有三台虚拟机db1(IP:1 ...
- 前后端分离项目 nginx配置实践
新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源. 搜索nginx配置大部分都通过url前缀进行转发来做前后端分离,不适用目前项目. 说明 前 ...
- Nginx配置支持https协议-应用实践
Nginx配置支持https协议-应用实践 https简介 HTTPS 是运行在 TLS/SSL 之上的 HTTP,与普通的 HTTP 相比,在数据传输的安全性上有很大的提升. TLS是传输层安全协议 ...
- Nginx中的Rewrite的重定向配置与实践
阅读目录 一:理解地址重写 与 地址转发的含义. 二:理解 Rewrite指令 使用 三:理解if指令 四:理解防盗链及nginx配置 简介:Rewrite是Nginx服务器提供的一个重要的功能, ...
- 使用nginx配置域名及禁止直接通过IP访问网站
前段时间刚搭建好个人网站,一直没有关注一个问题,那就是IP地址也可以访问我的网站,今天就专门研究了一下nginx配置问题,争取把这个问题研究透彻. 1. nginx配置域名及禁止直接通过IP访问 先来 ...
- Nginx 配置简述
不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...
- thinkphp nginx配置
安装和配置: http://www.cnblogs.com/Bonker/p/4252588.html spawn-fcgi 要先安装和启动:(已过时) sudo spawn-fcgi -a -u ...
- nginx配置入门
谢谢作者的分享精神,原文地址:http://www.nginx.cn/591.html nginx配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一 ...
随机推荐
- Webbrowser 在web项目中的使用
string htmlstr = string.Empty; [STAThread] public string GetHtmlByWeb(string url) { try { RunWithSin ...
- Selenium打开谷歌浏览器提示chromedriver.exe停止运行且浏览器显示请关闭开发者模式
提示关闭开发者模式,其实是chromedriver.exe与chrome 浏览器版本不一致导致的, 查看浏览器版本方法 在浏览框输入,chrome://version/. 显示是 Google Ch ...
- postman工具的应用实战(二)
在接口测试工具中,最好的应该是soapui,jmeter,postman,但是soapui需要安装和破解,当然也是有破解版的,但是不够灵活,jmeter工具 做接口测试还是性能测试,功能测试,都是一个 ...
- IntelliJ IDEA配置本地Tomcat方法---亲测有效
https://blog.csdn.net/hello_ljl/article/details/79258165
- 全文搜索(A-2)-推荐算法
一般来说推荐算法分为两类. 基于内容过滤的推荐: 基于协同过滤的推荐: 基于内容过滤的推荐,基于特征码描述项目. 协同过滤算法的设计基于一个假设,“和目标用户相似度高的用户,其感兴趣的物品目标用户也会 ...
- 贪吃的九头龙(tyvj P1523)
T2 .tyvj P1523贪吃的九头龙 描述 传说中的九头龙是一种特别贪吃的动物.虽然名字叫“九头龙”,但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于 ...
- Less Time, More profit 最大权闭合子图(最大流最小割)
The city planners plan to build N plants in the city which has M shops. Each shop needs products fro ...
- JSP的隐藏对象
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/implicit-objects.html: JSP隐式对象是Java对象,JSP容器使隐式对象在每一个页 ...
- hbase definitive guide 笔记
ext3 file system 优化 ext3 在用在hbase上可以做如下优化: 1. mount的时候加上noatime选项.这可以减少管理开销 2. 用命令tune2fs -m 0 /dev/ ...
- iOS开发项目实战——Swift实现图片轮播与浏览
近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...