为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。 简单来说,就是使用正则表达式匹配过滤,然后交个不同的服务器。
1、准备环境
准备一个nginx代理 两个http 分别处理动态和静态,三台机子
expires功能说明---(为客户端设置静态资源缓存时间)
1.静态资源配置
server {
listen 80;
server_name localhost;location ~ \.(html|jpg|png|js|css) {
root /www/nginx;
expires 1d; #为客户端设置静态资源缓存时间
}
}
测试:
[root@nginx-yum2 conf.d]# curl -I http://10.0.105.200/test.jpg
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Mon, 07 Sep 2019 11:35:08 GMT
Content-Type: image/jpeg
Content-Length: 27961
Last-Modified: Mon, 07 Sep 2019 11:31:17 GMT
Connection: keep-alive
ETag: "5f561a05-6d39"
Expires: Tue, 08 Sep 2019 11:35:08 GMT #缓存到期时间
Cache-Control: max-age=86400 #缓存持续时间秒
Accept-Ranges: bytes注意创建相关目录mkdir -p /www/nginx,并在目录下放入相关静态资源,图片就行
2.动态资源配置:
yum 安装php7.1
[root@nginx-server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@nginx-server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx-server ~]# yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[root@nginx-server ~]# yum install -y php71w-fpm[root@nginx-server ~]# systemctl start php-fpm
[root@nginx-server ~]# systemctl enable php-fpm
#动态服务器编辑nginx连接php
编辑nginx的配置文件:
server {
listen 80;
server_name localhost;
location ~ \.php$ {
root /nginx/html; #指定网站目录
fastcgi_pass 127.0.0.1:9000; #开启fastcgi连接php地址
fastcgi_index index.php; #指定默认文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
include fastcgi_params; #包含fastcgi使用的常量
}
}
创建相关目录mkdir -p /nginx/html,目录底下指定文件index.php
vim index.php
<?php phpinfo(); ?>
重启nginx !!!
访问测试
3.配置nginx反向代理upstream,并实现客户端缓存时间
#vim /etc/nginx/nginx.conf (这个配置要写在主配置里)
upstream static {
server 192.168.174.160;
}
upstream php {
server 192.168.174.162;
}server {
listen 80;
server_name localhost;
#动态资源加载
location ~ \.(php|jsp)$ {
proxy_pass http://php;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#静态资源加载
location ~ .*\.(html|jpg|png|css|js)$ {
proxy_pass http://static;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
测试成功!