网站经过nginx进行动静分离,可是个别静态文件开发在tomcat端配置的虚拟路径,此路径对nginx来讲是不存在的,须要将这部分路径过滤并反向代理至后端处理。css
location ~.*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root/code/qhfaxweb_re; expires 1d; }
upstreamweb_cluster { server 192.168.1.62:6805 weight=1 max_fails=2fail_timeout=30s; server 192.168.1.63:6806 weight=2 max_fails=2fail_timeout=30s; }
/path/down.myapp.com/Android.apkhtml
a)URL rewrite设置nginx
rewrite ^/path/(.*) http://path.web.com/path/$1 last;web
b) server配置后端
server{ listen 80; server_name path.web.com; location/ { proxy_pass http://web_cluster; } }
Nginx会按照配置文件出现的顺序来执行全部的rewrite阶段的指令,因此在location内设置if语句时,须要进行增长break指令,使其跳出location模块。centos
if ($uri ~ "/path/.*" ) { proxy_pass http://web_ cluster; break; }
location ^~/path/ { proxy_passhttp://image.qhfax.com:8800; break; } location ~.*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root/code/qhfaxweb_re; expires 1d; }
各位运维小伙伴,必定要注意nginx在执行rewrite是顺序执行的,在出现if选择时,if模块和以后执行的步骤有冲突就须要使用break跳出,再也不匹配以后的rewrite了,你们平时配置nginx时,值得留意此坑!!!tomcat
分享:如何使用nginx对动态请求中参数进行跳转 问题:有时候们在作rewrite,发现新的URL中获取参数的方法不同了,若是在rewrite中直接去该参数为变量,那么你会发 现,规则是不成功的,nginx过滤了参数,这个使用就要使用nginx提供的$arg功能模块了,具体操做以下: if ( $arg_function ~'GetNewDetail') { rewrite ^/cgi/news/NewAndNotice?(.*) /about/getArticleDetail/news/$arg_newIdlast; break; } 上面$arg_function中的function为URL中的参数名,相似于/promotion/recommend.jsp?campaign_id=94926c22-9df4-11e3-82d5-b8ca3aecba9a&c_channel=tyq&c_keywords=01 中campaign_id或c_channel或keywords,直接抓取参数不一样的进行匹配,而后再rewrite