目录
动静分离的说明
测试环境
动静分离的配置(请求分离)
据客户端类型进行分离
方案
根据目录名称配置
方案
根据后缀名称配置
检查结果
伪静态方法
动静分离的说明
动静分离的配置,我这篇文章中主要是对请求进行分类,分为客户端浏览器类型,机器类型 ,不同目录,文件后缀名进行.请求分离的结果,是让不同的客户端给出不同的页面,还有就是让静态数据nginx处理,动态数据应用服务器处理.
测试环境
192.168.11.179 | 调度服务器 |
192.168.11.180 | 工作机1 |
192.168.11.181 | 工作机2 |
动静分离的配置(请求分离)
据客户端类型进行分离
方案
根据IE浏览器和Chrome浏览器分别处理文件
1 ) 创建域名browserlb.yc.com 指向192.168.11.179
2) 创建配置文件
配置文件修改如下,注意,使用正则表达式匹配,转发的网址后面不能带任何目录.
location / {
root /usr/share/nginx/html/browserlb;
index index.html index.htm;
if ($http_user_agent ~* "MSIE")
{
proxy_pass http://192.168.11.180;
}
if ($http_user_agent ~* "Chrome")
{
proxy_pass http://192.168.11.180;
}
}
3) 在工作机1和工作机2上测试,直接访问出现的结果是不一样的
4) 重新启动调度器nginx
5) 测试,上门是chrome浏览器,转发到181,第二个转发到180了,
根据目录名称配置
方案
- 调度服务器上做一个pathlb.yc.com的域名解析到192.168.11.179
- 配置该调度器上的虚拟主机进行动静分离,在该虚拟主机的的目录下创建image目录,并将day.jpg文件拷贝到此目录.
- 修改工作机1下的配置文件index.html.文件中需要访问的图片放到调度服务器上.
1) 在pathlb虚拟主机上的配置文件/etc/nginx/conf.d/pathlb.conf
location /dynamic {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.11.180/dynamic;
}
location /static {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.11.181/static;
}
2) 重新启动服务器 systemctl start nginx
3) 配置 工作机1,在默认的根目录下面增加文件dymamic目录, index.php
===this is from dynamic
<?php
phpinfo();
?>
======= for staic testing ===
<img src='/static/day.jpg' />
4) 在工作机2(11.181)下增加目录static,并且把day.jpg拷贝进去
5) 直接访问pathlb.yc.com, 可以看到php和图片全部成功
调度器log日志
192.168.10.8 - - [07/Mar/2021:16:31:53 +0800] "GET /dynamic/index.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2536 "http://pathlb.yc.com/dynamic/index.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "-"
192.168.10.8 - - [07/Mar/2021:16:31:53 +0800] "GET /dynamic/index.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.1" 200 2158 "http://pathlb.yc.com/dynamic/index.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "-"
192.168.10.8 - - [07/Mar/2021:16:31:53 +0800] "GET /static/day.jpg HTTP/1.1" 200 961243 "http://pathlb.yc.com/dynamic/index.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "-
工作机1上的日志
192.168.11.179 - - [07/Mar/2021:16:31:53 +0800] "GET /dynamic/index.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.0" 200 2146 "http://pathlb.yc.com/dynamic/index.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "192.168.10.8"
工作机2上的日志
192.168.11.179 - - [07/Mar/2021:16:31:53 +0800] "GET /static/day.jpg HTTP/1.0" 200 961243 "http://pathlb.yc.com/dynamic/index.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "192.168.10.8"
根据后缀名称配置
方案
- 调度服务器上做一个extlb.yc.com的域名解析到192.168.11.179
- 配置该调度器上的虚拟主机进行动静分离,在该虚拟主机的的目录下创建image目录,并将day.jpg文件拷贝到此目录.
- 修改工作机1下的配置文件index.html.文件中需要访问的图片放到调度服务器上.
extlb.yc.com的文件配置如下,主要是对图片文件在本地读取,其他数据到工作机1上去读取.
192.168.11.180的配置文件,主要修改的地方在 6-10,17-18行
第一部分的改动,将客户机器的IP带到工作机
将调度机器的IP带到工作机
[root@centos7v3-nginx extlb]# nl /etc/nginx/conf.d/extlb.conf
1 server {
2 listen 80;
3 server_name extlb.yc.com;
4 #charset koi8-r;
5 #access_log /var/log/nginx/host.access.log main;
6 location / {
7 proxy_set_header Host $host;
8 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 proxy_pass http://192.168.11.180;
10 }
11 #error_page 404 /404.html;
12 # redirect server error pages to the static page /50x.html
13 #
14 error_page 500 502 503 504 /50x.html;
15 location = /50x.html {
16 root /usr/share/nginx/html;
17 }
18 location ~ .*.(jpg|png|gif)$ {
19 root /usr/share/nginx/html/extlb;
20 }
21 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
22 #
23 #location ~ \.php$ {
24 # proxy_pass http://127.0.0.1;
25 #}
26 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
27 #
28 #location ~ \.php$ {
29 # root html;
30 # fastcgi_pass 127.0.0.1:9000;
31 # fastcgi_index index.php;
32 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
33 # include fastcgi_params;
34 #}
35 # deny access to .htaccess files, if Apache's document root
36 # concurs with nginx's one
37 #
38 #location ~ /\.ht {
39 # deny all;
40 #}
41 }
重新启动nginx
再工作机1,11.180上,修改index.html
注意在本机下不需要创建image目录,图片的调度机会在它本机上读取.
<html>
this is 180
<img src='/image/day.jpg' />
</html>
工作机1上的配置文件不需要修改.
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
我们访问域名extlb.yc.com.看实际效果,图片已经正常访问.
检查结果
可以看到180服务器上的access已经看到有请求访问过来,并且看到实际的客户机IP,192.168.10.8
192.168.11.179 - - [07/Mar/2021:14:31:03 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "192.168.10.8"
再看调度器上的access日志,我们发现图片在本地读取,没有转发
KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "-"
192.168.10.8 - - [07/Mar/2021:14:31:03 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "-"
192.168.10.8 - - [07/Mar/2021:14:31:03 +0800] "GET /image/day.jpg HTTP/1.1" 200 10220 "http://extlb.yc.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36" "-"
伪静态方法
一般是讲动态的请求编程静态文件的内容,使得网络爬虫比较喜欢.这个就是那个了rewrite模块,
伪静态的方法多主机可现在.htaccess中.
if (!-e $request_filename) { rewrite ^/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\.html /index.php?m=$1&c=$2&a=$3&$4=$5&$6=$7 last; break; }