网站性能大提速:Nginx动静分离与伪静态配置一探究竟

公司资讯 admin 发布时间:2024-10-01 浏览:0 次

Linux | 红帽认证 | IT技术 | 运维工程师👇1000人技术交流QQ群 备注【公众号】 更快通过

根据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 Apaches document root 36 # concurs with nginxs 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 Apaches document root # concurs with nginxs one # #location ~ /\.ht { # deny all; #}}

我们访问域名extlb.yc.com.看实际效果,图片已经正常访问。

检查结果:

可以看到180服务器上的access已经看到有请求访问过来,并且看到实际的客户机IP,192.168.10.8192.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" "-"课程咨询添加:HCIE666CCIE↑或者扫描上方二维码↑你有什么想看的技术点和内容可以在下方留言告诉小盟哦!

在线咨询

点击这里给我发消息售前咨询专员

点击这里给我发消息售后服务专员

在线咨询

免费通话

24h咨询:400-888-8888


如您有问题,可以咨询我们的24H咨询电话!

免费通话

微信扫一扫

微信联系
返回顶部