nginx動靜分離

nginx 實現動靜分離 :
爲了加快網站的解析速度,能夠把動態頁面和靜態頁面由不一樣的服務器來解析,加快解析速度。下降原來單個服務器 的壓力。 在動靜分離的tomcat的時候比較明顯,由於tomcat解析靜態很慢,其實這些原理的話都很好理解,簡單來 說,就是使用正則表達式匹配過濾,而後交個不一樣的服務器。
一、準備環境
192.168.62.159 代理服務器 php

192.168.62.157 動態資源 css

192.168.62.155 靜態資源
準備一個nginx代理 兩個http 分別處理動態和靜態。html


192.168.62.159 代理服務器

1.配置nginx反向代理upstream;mysql

[root@nginx-server conf.d]# cat upstream.confnginx

upstream static {web

server 192.168.62.155:80 weight=1 max_fails=1 fail_timeout=60s; 正則表達式

                       } sql

upstream phpserver { vim

                      server 192.168.62.157:80 weight=1 max_fails=1 fail_timeout=60s; 後端

                     }

            server {

            listen 80;

server_name localhost;

#動態資源加載

location ~ \.(php|jsp)$ {

proxy_pass http://phpserver;

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|gif|jpg|png|bmp|swf|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;

                              }

}

192.168.62.155 靜態資源

#靜態資源配置 主配置文件-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.html;

              }

}

[root@nginx-server2 nginx]# cat /home/www/nginx/index.html //模擬靜態資源

hello 155
192.168.62.157 動態資源

#動態資源配置: 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 ~ \.php$ { root /home/nginx/html;                      #指定網站目錄

fastcgi_pass 127.0.0.1:9000;                                         #指定訪問地址

fastcgi_index index.php;                                                 #指定默認文件

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                               #站點根目錄,取 決於root配置項

include fastcgi_params;                                                 #包含nginx常量定義

                                               }

}

[root@nginx-server1 html]# cat /home/nginx/html/index.php            //模擬動態資源

dongtai
當訪問靜態頁面的時候location 匹配到 (html|jpg|png|js|css|gif|bmp|jpeg) 經過轉發到靜態服務器,靜態服務通 過location的正則匹配來處理請求。

當訪問動態頁面時location匹配到 .\php 結尾的文件轉發到後端php服務處理請求。

相關文章
相關標籤/搜索