一、nginx根据客户端类型进行转发
基于http_user_agent
第1步:如何配置
在web01上执行:echo this is PC website >/app/www/lidao.html
在web02上执行:echo this is Mobile website >/app/www/lidao.html
第2步:然后在lb01上面测试
[root@lb01 ~]# curl 10.0.0.7/lidao.html
this is PC website
[root@lb01 ~]# curl 10.0.0.8/lidao.html
this is Mobile website
第3步:配置负载的nginx配置文件
upstream default {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream mobile {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($http_user_agent ~* "Android|IOS" ) {
proxy_pass http://mobile;
}
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
第4步:在命令行或者浏览器进行测试
[root@lb01 /etc/nginx]# curl 10.0.0.5/lidao/html
[root@lb01 /etc/nginx]# curl -A ios 10.0.0.5/lidao/html
二、nginx根据用户请求uri进行转发(动静分离)
第1步:配置负载的nginx配置文件
upstream upload { (有上传就到这里) upload组 上传
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static { (静态资源:html 图片 视频 ccs) static组 静态资源
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default { (动态资源:默认 查看博客 书写博客) default组 默认
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
第2步,在web01,02,03上面创建目录
web01
mkdir -p /app/www/upload
echo this is upload >/app/www/upload/guoav.html
web02
mkdir -p /app/www/static
echo this is static >/app/www/static/guoav.html
web03
mkdir -p /app/www/
echo this is upload >/app/www/guoav.html
第3步:在命令行测试,然后在浏览器测试
[root@lb01 /etc/nginx]# curl 10.0.0.7/upload/guoav.html
this is upload
[root@lb01 /etc/nginx]# curl 10.0.0.8/static/guoav.html
this is static
[root@lb01 /etc/nginx]# curl 10.0.0.9/guoav.html
this is default
三、nginx根据请求的文件类型进行转发
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location ~* "\.(png|jpg|jpeg|gif|bmp|html|css)$" { (根据文件类型进行转发)
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
四、
轮询算法 round robin (rr)
加权轮询 weight (wrr)
最小连接数 least conn (根据后端服务繁忙程度进行分配)
ip_hash 只要客户端ip相同 就会被转发到同一台服务器上 (缺点:会出现负载失衡)
如何配置ip_hash
upstream upload {
ip_hash; (只需要这样配置就好)
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
共同点:
存放用户信息
key value类型 变量和变量内容
区别:
cookie
存放在浏览器
存放钥匙
开发设置
响应服务器给你设置
session
存放在服务器 reids中
存在敏感
锁头
网友评论