Nginx 最实用的配置技巧( 二 )


location /ops-coffee/ {proxy_pass http://192.168.106.135:8181;}http://domain/ops-coffee/ --> http://192.168.106.135:8181/ops-coffee/http://domain/ops-coffee/action/abc --> http://192.168.106.135:8181/ops-coffee/action/abc2. 目标地址中带uri(proxy_pass http://tomcats/,/也是uri),此时新的目标url中,匹配的uri部分将会被修改为该参数中的uri 。
location /ops-coffee/ {proxy_pass http://192.168.106.135:8181/;}http://domain/ops-coffee/ --> http://192.168.106.135:8181http://domain/ops-coffee/action/abc --> http://192.168.106.135:8181/action/abc 
nginx upstream开启keepaliveupstream tomcat {server ops-coffee.cn:8080;keepalive 1024;}server {location / {proxy_http_version 1.1;proxy_set_header Connection "";proxy_pass http://tomcat;}}nginx在项目中大多数情况下会作为反向代理使用,例如nginx后接tomcat,nginx后接php等,这时我们开启nginx和后端服务之间的keepalive能够减少频繁创建TCP连接造成的资源消耗,配置如上
keepalive:指定每个nginxworker可以保持的最大连接数量为1024,默认不设置,即nginx作为client时keepalive未生效
proxy_http_version 1.1:开启keepalive要求HTTP协议版本为HTTP 1.1
proxy_set_header Connection "":为了兼容老的协议以及防止http头中有Connection close导致的keepalive失效,这里需要及时清掉HTTP头部的Connection
 
404自动跳转到首页server {location / {error_page 404 = @ops-coffee;}location @ops-coffee {rewrite .* / permanent;}}网站出现404页面不是特别友好,我们可以通过上边的配置在出现404之后给自动跳转到首页去 。
来源:本文转自公众号“运维咖啡吧” 。

【Nginx 最实用的配置技巧】


推荐阅读