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;
}
}