nginx proxy_pass & rewrite exp.

简介:
昨天新建的一个站点, phppgadmin, 用于连接pg-xl, greenplum, postgresql , 作为一个数据库demo站点.
参考
原来的做法是新建一个server, server_name和dba.sky-mobi.com相同, 新增一个监听端口8001来区分这个server和老的80端口所在的wiki站点. 这样的话访问db demo站点就是dba.sky-mobi.com:8001.
这样的话, 如果我以后还想用这个域名新增站点的话, 还要加端口, 对外端口就比较多了, 而且如果新增的站点IP地址变更的话, 就需要新增域名, 所以不利于多站点的扩展.
一个比较通俗可行的办法是利用nginx的代理功能和重写功能, 这样对外都是80, 内部可以有多个站点, 也不用局限于1台主机.

修改nginx.conf, 新增一个location.
vi nginx.conf
在80 端口的server下面新增一个location, 注意使用^~来旁路规则表达式的location.
        location ^~ /demo/db {
            rewrite ^/demo/db(.*)$ $1 break;  # 重写uri, break不再继续匹配location.
            proxy_pass http://127.0.0.1:8001;   # http代理
        }


原来配置的8001的server保持不变, (甚至可以架设到其他主机上, 只要这台nginx http代理能访问到即可).
    server {
        listen       0.0.0.0:8001;
        server_name  dba.sky-mobi.com;
        root /site/phppgadmin;
        location / {
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
                include /opt/nginx1.6.0/conf/fastcgi_params;
                fastcgi_pass   127.0.0.1:9000;   # php-fpm的监听
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }
    }

重载配置文件 : 
nginx -s reload

这样的话访问dba.sky-mobi.com /demo/db/* 就会代理到  http://127.0.0.1:8001. (因为代理的数据完全由代理服务器转发, 不需要客户端直接访问后端的web server.)

注意, 如果访问 dba.sky-mobi.com /demo/db, 会报错, 因为rewrite后的uri为空, 所以报错如下

2014/07/23 09:19:25 [error] 9300#0: *2713 the rewritten URI has a zero length, client: 172.16.8.29, server: dba.sky-mobi.com, request: "GET /demo/db HTTP/1.1", host: "dba.sky-mobi.com"


务必使用 dba.sky-mobi.com /demo/db/ 访问 , 那么rewrite后$uri='/', 就不会报错了.

[后记]
感谢Channer的热心回复, 文档上有相关的说明, 眼拙没有看仔细, 哈. 
对于使用了代理处理的location , 末尾加斜线的处理, 不会导致匹配不到 .

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass,fastcgi_passuwsgi_passscgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}
加了斜线后, 如果使用proxy_pass 处理URI的话, 如果请求的URL没有/结束, 则自动添加这个/.

[参考]
目录
相关文章
|
应用服务中间件 nginx Perl
Nginx系列教程(09) - rewrite
Nginx系列教程(09) - rewrite
327 0
|
应用服务中间件 nginx
Nginx rewrite(URL)地址重定向
Nginx rewrite(URL)地址重定向
899 0
|
应用服务中间件 nginx C语言
3分钟教你搞定 nginx 编译安装报错:error: the HTTP rewrite module requires the PCRE library.
3分钟教你搞定 nginx 编译安装报错:error: the HTTP rewrite module requires the PCRE library.
4815 0
3分钟教你搞定 nginx 编译安装报错:error: the HTTP rewrite module requires the PCRE library.
|
9月前
|
缓存 负载均衡 应用服务中间件
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇
通过使用Nginx的反向代理功能,可以有效地提高Web应用的性能、安全性和可扩展性。配置过程中需要注意不同场景下的具体需求,如负载均衡、SSL终止和缓存策略等。正确配置和优化Nginx反向代理可以显著提升系统的整体表现。
1445 20
|
搜索推荐 Java 应用服务中间件
Nginx Rewrite 规则
【8月更文挑战第21天】Nginx Rewrite 规则
183 2
|
缓存 应用服务中间件 API
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(三)
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(三)
234 3
|
缓存 安全 应用服务中间件
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(二)
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(二)
658 1
|
应用服务中间件 Apache nginx
apache、nginx开启rewrite重写服务及伪静态
apache、nginx开启rewrite重写服务及伪静态
627 4
|
负载均衡 应用服务中间件 网络安全
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(一)
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(一)
2677 0
|
应用服务中间件 nginx
百度搜索:蓝易云【Nginx【Nginx核心指令(rewrite指令、实战rewrite 、if指令、set和break指令】】
这些核心指令在Nginx的配置文件中发挥重要作用。使用rewrite指令可以实现URL的重写和重定向,if指令可以根据条件执行不同的操作,set指令可以创建自定义变量并设置其值,而break指令可以中断请求处理流程。理解和灵活运用这些指令,可以帮助我们更好地配置和管理Nginx服务器。
166 1