您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關(guān)nginx怎樣實現(xiàn)動靜分離,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
為了加快網(wǎng)站的解析速度,可以把動態(tài)頁面和靜態(tài)頁面由不同的服務器來解析,加快解析速度。降低原 來單個服務器的壓力。 簡單來說,就是使用正則表達式匹配過濾,然后交個不同的服務器。
1、準備環(huán)境
準備一個nginx代理 兩個http 分別處理動態(tài)和靜態(tài)。
1.配置編譯安裝的nginx為反向代理upstream;
upstream static { server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s; } upstream php { server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s; } server { listen server_name #動態(tài)資源加載 80; 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; } #靜態(tài)資源加載 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; } }
靜態(tài)資源配置---10.0.105.196
server { listen 80; server_name localhost; location ~ \.(html|jpg|png|js|css)$ { root /var/www/nginx; } }
上傳圖片
動態(tài)資源配置: 10.0.105.200
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的配置文件:
[root@nginx-server ~]# cd /etc/nginx/conf.d/ [root@nginx-server conf.d]# vim phpserver.conf server {
listen 80;
server_name localhost; location ~ \.php$ {
root /home/nginx/html; #指定網(wǎng)站目錄
fastcgi_pass fastcgi_index fastcgi_param
#站點根目錄,取決于root配置項
include
}
}
127.0.0.1:9000; #指定訪問地址
index.php;
#指定默認文件
SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_params; #包含nginx常量定義
當訪問靜態(tài)頁面的時候location 匹配到 (html|jpg|png|js|css) 通過轉(zhuǎn)發(fā)到靜態(tài)服務器,靜態(tài)服務通過
location的正則匹配來處理請求。
當訪問動態(tài)頁面時location匹配到 .\php 結(jié)尾的文件轉(zhuǎn)發(fā)到后端php服務處理請求。
知識點擴展:
通過請求分離
[root@lb01 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream stack_pools { server 172.25.254.134:80 weight=5; } upstream dynamic_pools { server 172.25.254.135:80 weight=5; } server { listen 80; server_name www.lbtest.com; location / { root html; index index.html index.htm; proxy_set_header Host $host; proxy_pass http://dynamic_pools; } location /image/ { proxy_set_header Host $host; proxy_pass http://stack_pools; } location /dynamic/ { proxy_set_header Host $host; proxy_pass http://dynamic_pools; } } } [root@lb01 conf]# nginx -s reload
根據(jù)擴展名分離
[root@lb01 conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream stack_pools { server 172.25.254.134:80 weight=5; } upstream dynamic_pools { server 172.25.254.135:80 weight=5; } server { listen 80; server_name www.lbtest.com; location / { root html; index index.html index.htm; proxy_set_header Host $host; proxy_pass http://dynamic_pools; } location ~ .*.(jpg|png|gif|css|js|swf|bmp|jsp|php|asp)$ { proxy_set_header Host $host; proxy_pass http://stack_pools; } } } [root@lb01 conf]# nginx -s reload
關(guān)于“nginx怎樣實現(xiàn)動靜分離”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。