详解nginx rewrite和根据url参数location
最近项目中涉及到旧老项目迁移,需要在nginx上做些配置,所以简单学习了下,好记性不如烂笔头,先记下来。
rewrite
首先查看下nginx是否支持rewrite:
./nginx-V
不支持说明安装nginx时候缺少pcre,需要重新安装nginx:
#安装pcre wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz tar-zxvfpcre-8.34.tar.gz cdpcre-8.34 ./configure make makeinstall #安装nginx cdnginx-1.0.12 ./configure--conf-path=/usr/local/nginx/conf/nginx.conf\ --pid-path=/usr/local/nginx/nginx.pid\ --with-http_ssl_module\ --with-pcre=/usr/local/src/pcre-8.34\ make makeinstall #启动nginx ./nginx #重启nginx ./nginx–sreload
示例:
比如现有如下的nginx配置:
worker_processes24;
#worker_cpu_affinity0000000000000001;
worker_rlimit_nofile65535;
error_loglogs/error.logcrit;
pidlogs/nginx.pid;
events{
useepoll;
worker_connections2048000;
}
http{
includemime.types;
default_typeapplication/octet-stream;
charsetutf-8;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout60;
client_max_body_size10m;
client_body_buffer_size128k;
upstreamlog{
server192.168.80.147:8338;
}
server{
listen6061;
server_name192.168.71.51;
location/{
proxy_passhttp://log;
proxy_redirectoff;
proxy_set_headerHost$host;
proxy_set_headerRemote_Addr$remote_addr;
proxy_set_headerX-REAL-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_connect_timeout90;
proxy_send_timeout90;
proxy_read_timeout90;
proxy_buffer_size4k;
proxy_buffers432k;
proxy_busy_buffers_size64k;
proxy_temp_file_write_size64k;
}
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
log_formatlog'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
access_loglogs/access_log.loglog;
#设定查看Nginx状态的地址
location/NginxStatus{
#stub_statuson;
access_logon;
auth_basic"NginxStatus";
#auth_basic_user_fileconf/htpasswd;
}
}
}
现在需要作如下的重定向:
192.168.71.51/log.aspx–>192.168.80.147:8338/log
192.168.71.51/do.aspx–>192.168.80.147:8338/do
192.168.71.51/uplog.aspx–>192.168.80.147:8338/log
可以如下配置:
server{
listen6061;
server_name192.168.71.51;
rewrite^(.*)(?i)uplog.aspx(.*)$$1log$2break;
rewrite^(.*)(?i)log.aspx(.*)$$1log$2break;
rewrite^(.*)(?i)do.aspx(.*)$$1do$2break;
location/{
proxy_passhttp://log;
proxy_redirectoff;
proxy_set_headerHost$host;
proxy_set_headerRemote_Addr$remote_addr;
proxy_set_headerX-REAL-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_connect_timeout90;
proxy_send_timeout90;
proxy_read_timeout90;
proxy_buffer_size4k;
proxy_buffers432k;
proxy_busy_buffers_size64k;
proxy_temp_file_write_size64k;
}
关于这里的rewrite配置主要说明以下几点:
- rewrite用法:rewrite正则替换标志位
- 第一行配置和第二行配置顺序不能颠倒,因为nginx会从上往下依次rewrite(break在这里不起作用);
- (?!)表示忽略大小写匹配(网上说的是~*,但好像不起作用,我的nginx版本是1.0.12);
- 1,1,2表示前面正则表达式匹配到的部分;
- rewrite可以在server里也可以在location里,nginx会首先执行server里的rewrite,然后才会执行location,意味着location的是重写后的url,之后还会执行location里的rewrite,最后nginx还会拿结果去执行剩下的location。
根据url参数location
实际开发中经常有根据请求参数来路由到不同请求处理者的情况,根据POST请求参数需要些nginx插件,这里主要简单介绍下如何根据GET参数来路由。
还是上面的配置文件。比如我们希望访问http://192.168.71.51:6061/do1.aspx?t=1212&c=uplog当url中的参数c为config或uplog的时候(忽略大小写)我们路由到其他地方:
首先增加一个upstream,比如:
……
upstreamother{
server192.168.71.41:2210;
}
……
然后在location里增加如下的判断即可:
……
location/{
if($query_string~*^(.*)c=config\b|uplog\b(.*)$){
proxy_passhttp://other;
}
……
关键是标红的行,$query_string表示url参数,后面是标准的正则匹配,需要的注意的是nginx中if有很多限制,语法很苛刻,具体参看上面的文档。
很简单却很实用的配置,希望能帮到正在找这方面信息的同学。