為了加快網(wǎng)站的解析速度梭灿,可以把動態(tài)頁面和靜態(tài)頁面由不同的服務(wù)器來解析画侣,加快解析速度。降低原來單個服務(wù)器的壓力堡妒。 在動靜分離的tomcat的時候比較明顯配乱,因為tomcat解析靜態(tài)很慢方咆,其實這些原理的話都很好理解荸实,簡單來說妖异,就是使用正則表達式匹配過濾隶校,然后交個不同的服務(wù)器。
1侵贵、準備環(huán)境
192.168.62.159 代理服務(wù)器
192.168.62.157 動態(tài)資源
192.168.62.155 靜態(tài)資源
192.168.62.159 代理服務(wù)器
1.配置nginx反向代理upstream东抹;
[root@nginx-server conf.d]# cat upstream.conf
upstream static {
server 192.168.62.155:80 weight=1 max_fails=1 fail_timeout=60s;
}
upstream phpserver {
server 192.168.62.157:80 weight=1 max_fails=1 fail_timeout=60s;
}
server {
listen 80;
server_name localhost;
#動態(tài)資源加載
location ~ .(php|jsp)host:
remote_addr;
proxy_set_header X-Forwarded-For {
proxy_pass http://static;
proxy_set_header Host server_port;
proxy_set_header X-Real-IP proxy_add_x_forwarded_for;
}
}
192.168.62.155 靜態(tài)資源
靜態(tài)資源配置 主配置文件-include /etc/nginx/conf.d/*.conf
vim /etc/nginx/conf.d/static.conf
server {
listen 80;
server_name localhost;
location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) {
root /home/www/nginx;
index index.html index.htm;
}
}
[root@nginx-server2 nginx]# cat /home/www/nginx/index.html //模擬靜態(tài)資源
hello 155
192.168.62.157 動態(tài)資源
動態(tài)資源配置:
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的配置文件:
server {
listen 80;
server_name localhost;
location ~ .phpdocument_root$fastcgi_script_name; #站點根目錄介蛉,取決于root配置項
include fastcgi_params; #包含nginx常量定義
}
}
[root@nginx-server1 html]# cat /home/nginx/html/index.php //模擬動態(tài)資源
dongtai
當訪問靜態(tài)頁面的時候location 匹配到 (html|jpg|png|js|css|gif|bmp|jpeg) 通過轉(zhuǎn)發(fā)到靜態(tài)服務(wù)器爆阶,靜態(tài)服務(wù)通過location的正則匹配來處理請求休溶。
當訪問動態(tài)頁面時location匹配到 .\php 結(jié)尾的文件轉(zhuǎn)發(fā)到后端php服務(wù)處理請求。