nginx 配置文件修改以及实现的功能

nginx一个server一个虚拟主机 可以基于端口 基于域名  基于IP地址
基于端口的虚拟主机
server {
     listen 8080;
     server_name www.a.com;
location / {
    root  html;
    index   index.html index.htm;
     }
}


基于域名的虚拟主机
server {
     listen 80;
     server_name www.a.com;
location / {
     root html;
     index  index.html  index.htm;
      }
}


基于IP地址的虚拟主机
server{
   listen 192.168.88.5:80;
   server_name  www.a.com;
location  / {
   root  html;
   index   index.html  index.htm;
     }
}


网络地址重写  访问www.wanbu.com.cn  跳转到www.wanbu.com
server {
    listen 80;
    server_name  www.wanbu.com;
    rewrite  /www.wanbu.com.cn  /www.wanbu.com;
location / {
    root  html;
    index  index.html  index.htm;
        }
}

网络地址重写访问a.html 跳转到b.html 并且域名更改成b.html
server {
    listen 80;
    server_name  www.wanbu.com;
    rewrite  ^/www.a.com$  /www.b.com  redirect;
location / {
    root  html;
    index  index.html  index.htm;
        }
}

访问92.168.88.5跳转到 www.tmooc.com
server {
    listen 80;
    server_name localhost;
    redirect / http://www.tmooc.cn/;
location / {
    root html;
    index index.html index.htm;
        }  
}

访问192.168.88.5/(.*) 跳转到对应的页面
server {
    listen 80;
    server_name localhost;
    rewrite / (.*)  http://www.tmooc.cn/$1;
location / {
    root html;
    index  index.html  index.htm;
        }   
}

用不同的浏览器访问192.168.88.5/abc.html得到不同的结果
server {
    listen 80;
    server_name localhost;
location / {
    root html;
    index  index.html index.htm;
       }
if ($http_user_agent ~* firefox) {
rewrite (.*)  /firefox/$1;
       }
}

last 不再读其他语句,但还会继续匹配到其他的location语句  redirect作用就是地址跳转到c.html
  server {
        listen       80;
        server_name  localhost;
location / {
            root   html;
            index  index.html index.htm;
            rewrite /a.html /b.html last;
            rewrite /b.html /c.html redirect;
        }
}

配置nginx为负载均衡代理机器
http {
    upstream webserver  {
         server  192.168.99.100:80 ;
         server  192.168.99.200:80;
           }
}
server  {
          listen         80;
          server_name localhost;
location / {
          proxy_pass  http://webserver;
      }  
}

配置nginx负载均衡并且设置权重
http {
    upstream webserver {
          server   192.168.99.100:80 weight=2;
          server   192.168.99.200:80
 }
server {
          listen         80;
         server_name   localhost;
location / {
        proxy_pass http://webserver
       }
}


设置健康检查,max_fails可以设置后台服务器的失败次数,fail_timeout可以设置后台服务器的失败超时时间。
http {
       upstream webserver {
          server 192.168.99.100:80
          server 192.168.99.200:80  max_fails=2 fail_timeout=30;             
               }
}
server  {
          listen     80;
          server_name  localhost;
location  / {
         proxy_pass http://webserver;
             }
}

配置nginx负载均衡相同客户端访问相同的IP   (ip_hash)
http {
     upstream webserver {
                          ip_hash;
            server 192.168.99.100:80;
            server 192.168.99.200:80;
      }
}
server {
      listen        80;
      server_name    localhost;
location   /  {
      proxy_pass  http://webserver;
           }
}

设置webserver集群某一台服务器不参与集群活动
http {
      upstream  webserver {
             server 192.168.99.100:80;
             server 192.168.99.200:80  down;
            }
}
server  {
         listen     80;
         server_name   localhost;
location / {
        proxy_pass  http://webserver;
          }
}


 

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值