设为首页 收藏本站
查看: 1015|回复: 0

[经验分享] Nginx-location语法总结

[复制链接]

尚未签到

发表于 2018-11-12 11:58:47 | 显示全部楼层 |阅读模式
  upstream www.a.com{
  server 10.1.230.215:80 weight=2 max_fails=3 fail_timeout=20s;
  server 10.1.230.216:80 weight=2 max_fails=3 fail_timeout=20s;
  server 10.1.230.217:80 weight=2 max_fails=3 fail_timeout=20s;
  server 10.1.230.218:80 backup;
  keepalive 32;
  check interval=3000 rise=2 fall=5 timeout=1000;
  }
  upstream tomcat{
  server 10.1.230.215:8080 weight=4 max_fails=3 fail_timeout=20s;
  server 10.1.230.216:8080 weight=1 max_fails=3 fail_timeout=20s;
  server 10.1.230.217:8080 weight=1 max_fails=3 fail_timeout=20s;
  server 10.1.230.218:8080 backup;
  check interval=3000 rise=2 fall=5 timeout=1000;
  }
  upstream iis{
  server 10.1.230.215:80 weight=1 max_fails=3 fail_timeout=20s;
  server 10.1.230.216:80 weight=4 max_fails=3 fail_timeout=20s;
  server 10.1.230.217:80 weight=1 max_fails=3 fail_timeout=20s;
  server 10.1.230.218:80 backup;
  check interval=3000 rise=2 fall=5 timeout=1000;
  }
  upstream php{
  server 10.1.230.217:80;
  server 10.1.230.215:80;
  server 10.1.230.216:80;
  server 10.1.230.218:80;
  ip_hash;
  check interval=3000 rise=2 fall=5 timeout=1000;
  }
  proxy_temp_path /tmp/nginx/temp;
  #设置临时目录
  proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  #设置缓存目录为二级目录,共享内存区大小,非活动时间,最大容量,注意临时目录要跟缓存目录在同一个分区。
  autoindex on;
  # 防止目录浏览
  server {
  listen 80;
  #侦听80端口
  server_name www.a.com;
  #定义使用www.xx.com访问
  access_log /var/log/nginx/www.a.com-access_log main;
  #######负载均衡配置#########
  location ~ /\.ht {
  deny all;
  # 防止.ht文件被请求
  }
  if ($host = 'a.com') {
  rewrite ^/(.*)$ http://www.a.com/$1 permanent;
  }
  #如果访问地址是a.com,则转向www.a.com
  location =/ {
  #这个规则优先级是最高的。仅仅匹配/
  proxy_pass http://www.a.com;
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  #proxy_next_upstream用来定义故障转移策略,当后端服务节点返回500、502、503、504和执行超时等错误时,自动将请求转发到upstream负>载均衡组中的另一台服务器,实现故障转移
  }
  #访问首页负载到www.a.com这组服务器。这里并不包括http://www.a.com/index.html,因为下面html会被缓存。
  location ^~/(images|doc)/ {
  #这个是其它的匹配规则,匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。再向下就按出现的顺序匹配。
  #所有访问http://www.a.com/images/....或者http://www.a.com/doc/...都被缓存。保存24小时。
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_pass http://www.a.com;
  proxy_cache cache_one;
  #对不同的HTTP状态码设置不同的缓存名称
  proxy_cache_key $host$uri$is_args$args;
  #proxy_cache_key 指定了包含在缓存中的缓存关键字(默认情况下服务器的主机名并没有包含到缓存关键字中)
  proxy_cache_valid 200 304 12h;
  #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
  expires 24h;
  #设置过期时间为24H
  }
  location /login {
  proxy_pass http://www.a.com;
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  }
  #访问http://www.a.com/login匹配这条规则。
  #tomcat负载1
  #location ~ \.(do|jsp|action|xml)$ {
  # proxy_pass http://tomcat;
  # include  /etc/nginx/conf.d/proxy.conf;
  # proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  #}
  #这里被区分大小写
  #tomcat负载2
  location ~ .*\.(do|jsp|action|xml)?$ {
  proxy_pass http://tomcat;
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  }
  #这个不区分大小写。
  #location !~ \.xhtml$ {
  #规则F
  #}
  #location !~* \.xhtml$ {
  #规则G
  #}
  #上面两个规则,当访问 http://www.a.com/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到.
  #ASP负载
  location ~*\.(aspx|asp|config)$ {
  proxy_pass http://iis;
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  }
  #PHP负载,perl cgi负载
  location ~*\.(php|cgi)$ {
  proxy_pass http://php;
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  }
  location ~* /purge(/.*)
  #当访问http://www.a.com/purge/...时会匹配这个规则。
  {
  allow      10.1.230.35;
  #allow      10.1.230.0/24;
  deny all;
  proxy_cache_purge  cache_one  $host$1$is_args$args;
  }
  location ~*\.(png|jpg|gif|GIF|jpeg|JPG|PNG|bmp|BMP|JPEG|ico|txt|css|js|htm|html)$
  #访问所有不区分大小写PNG,JPG等结尾的匹配这里。
  {
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_pass http://www.a.com;
  proxy_cache cache_one;
  #对不同的HTTP状态码设置不同的缓存名称
  proxy_cache_key $host$uri$is_args$args;
  #proxy_cache_key 指定了包含在缓存中的缓存关键字(默认情况下服务器的主机名并没有包含到缓存关键字中)
  proxy_cache_valid 200 304 12h;
  #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
  expires 24h;
  #设置过期时间为24H
  }
  #图片文件开启缓存,并允许一些机器清除缓存。
  #如http://www.a.com/image/a.jpb.jpg这个访问图看,则需http://www.a.com/purge/image/a.jpb.jpg来清除缓存。这里的顺序是purge在前,\.(png|jpg等在后。
  location / {
  proxy_pass http://www.a.com;
  include  /etc/nginx/conf.d/proxy.conf;
  proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  #proxy_next_upstream用来定义故障转移策略,当后端服务节点返回500、502、503、504和执行超时等错误时,自动将请求转发到upstream负>载均衡组中的另一台服务器,实现故障转移
  }
  #这个是默认规则,通用规则。当所有规则不匹配时匹配这里的规则。
  #########################nginx状态##############
  location ~*/NginxStatus?$ {
  #这里的~*表示不区分大小写,http://www.a.com/nginxstatus或者NGINXSTATUS都可以打开。如果没有后面的?$表示结尾,则输入NGINXSTATUS...都可以打开。
  stub_status   on;
  #stub_status设置为“on”表示启用StubStatus的工作状态统计功能。.
  access_log       /var/log/nginx/NginxStatus.log;
  #access_log 用来指定StubStatus模块的访问日志文件。
  auth_basic       "NginxStatus";
  #auth_basic是Nginx的一种认证机制。
  auth_basic_user_file  /etc/nginx/htpasswd;
  #auth_basic_user_file用来指定认证的密码文件,由于Nginx的auth_basic认证采用的是与Apache兼容的密码文件,因此需要用Apache的htpasswd命令来生成密码文件.
  #例如要添加一个webadmin用户,可以使用下面方式生成密码文件:
  #/usr/local/apache/bin/htpasswd -c /opt/nginx/conf/htpasswd webadmin
  #会得到以下提示信息:
  #New password:
  #输入密码之后,系统会要求再次输入密码。确认之后添加用户成功。
  }
  #################################################
  location /status {
  check_status;
  access_log  off;
  allow 10.1.230.35;
  deny all;
  }
  }
  以上是一个比较全的配置,实际应用中要根据自己的情况来配置。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-634127-1-1.html 上篇帖子: Nginx Java环境(tomcat)支持 下篇帖子: CentOS 6.5 源码安装Nginx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表