主要内容:
1.根据客户端的用户设备进行转发
2.动静分离
3.根据文件的不同需求来进行转发
4.cookie与session
一、根据客户端的用户设备进行转发
1.准备环境:
(1).web01
[root@web01 ~]# echo 'this is PC website' >/app/www/lidao.html
(2)web02
[root@web01 ~]# echo 'this is Mobile website' >/app/www/lidao.html
(3)测试
[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
[root@lb01 ~]#
2.修改lb01的配置文件
[root@lb01 ~]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
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;
}
}
}
3.使用curl -A 指定不同的操作系统在xshell上进行检查是否实现
[root@lb01 ~]# curl -A android 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 ~]# curl -A IOS 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 ~]# curl -A windows 10.0.0.5/lidao.html
this is Mobile website
4.使用火狐浏览器添加插件User agent Switcher分别变换为移动端和pc端进行访问
二、动静分离
1.准备环境
(1)web01
mkdir -p /app/www/upload
echo this is upload > /app/www/upload/guoav.html
(2)web02:
mkdir -p /app/www/static
echo this is static > /app/www/static/guoav.html
(3)web03:
echo this is default > /app/www/guoav.html
2.在lb01上进行检查:
[root@lb01 nginx]# curl 10.0.0.7/upload/guoav.html
this is upload
[root@lb01 nginx]# curl 10.0.0.8/static/guoav.html
this is static
[root@lb01 nginx]# curl 10.0.0.9/guoav.html
this is default
3.修改lb01的配置文件
[root@lb01 ~]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream upload {
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;
}
# 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;
}
}
}
4.在xshell上进行测试
[root@lb01 nginx]# curl 10.0.0.7/upload/guoav.html
this is upload
[root@lb01 nginx]# curl 10.0.0.8/static/guoav.html
this is static
[root@lb01 nginx]# curl 10.0.0.9/guoav.html
this is default
5.在浏览器上访问
三、根据不同的文件类型进行转发
lb01的配置文件
[root@lb01 ~]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream upload {
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;
}
# 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|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;
}
}
}
四、cookie与session
1.cookie与session的共同点
存放用户信息
key value 类型 变量和变量内容
2.cookie和session的区别
cookie:
存放在浏览器中
存放简单的信息或者钥匙
session:
存放在服务器上
存放比较敏感的信息
或者说是存放锁头