濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > nginx實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解

nginx實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解

熱門標(biāo)簽:廈門防封電銷電話卡 四川保險(xiǎn)智能外呼系統(tǒng) 云南電商智能外呼系統(tǒng)哪家好 地圖標(biāo)注能更改嗎 地圖標(biāo)注員有發(fā)展前景嗎 外呼系統(tǒng)全國(guó) 高德地圖標(biāo)注公司需要錢 宜賓銷售外呼系統(tǒng)軟件 濰坊寒亭400電話辦理多少錢

為了加快網(wǎng)站的解析速度,可以把動(dòng)態(tài)頁(yè)面和靜態(tài)頁(yè)面由不同的服務(wù)器來(lái)解析,加快解析速度。降低原 來(lái)單個(gè)服務(wù)器的壓力。 簡(jiǎn)單來(lái)說(shuō),就是使用正則表達(dá)式匹配過(guò)濾,然后交個(gè)不同的服務(wù)器。

1、準(zhǔn)備環(huán)境

準(zhǔn)備一個(gè)nginx代理 兩個(gè)http 分別處理動(dòng)態(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
#動(dòng)態(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;
}
}

上傳圖片

動(dòng)態(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

#站點(diǎn)根目錄,取決于root配置項(xiàng)

include

}

}

127.0.0.1:9000; #指定訪問(wèn)地址

index.php;

#指定默認(rèn)文件

SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_params; #包含nginx常量定義

當(dāng)訪問(wèn)靜態(tài)頁(yè)面的時(shí)候location 匹配到 (html|jpg|png|js|css) 通過(guò)轉(zhuǎn)發(fā)到靜態(tài)服務(wù)器,靜態(tài)服務(wù)通過(guò)

location的正則匹配來(lái)處理請(qǐng)求。

當(dāng)訪問(wèn)動(dòng)態(tài)頁(yè)面時(shí)location匹配到 .\php 結(jié)尾的文件轉(zhuǎn)發(fā)到后端php服務(wù)處理請(qǐng)求。

知識(shí)點(diǎn)擴(kuò)展:

通過(guò)請(qǐng)求分離

[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ù)擴(kuò)展名分離

[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實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解的文章就介紹到這了,更多相關(guān)nginx實(shí)現(xiàn)動(dòng)靜分離內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:巴彥淖爾 滁州 德州 湛江 回訪 廊坊 廣安 紅河

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《nginx實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解》,本文關(guān)鍵詞  nginx,實(shí)現(xiàn),動(dòng)靜,分離,實(shí)例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《nginx實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于nginx實(shí)現(xiàn)動(dòng)靜分離實(shí)例講解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    昌乐县| 乌兰浩特市| 合江县| 五原县| 平定县| 日照市| 新郑市| 新乡县| 崇仁县| 邯郸市| 西乡县| 香格里拉县| 靖边县| 株洲市| 彰化县| 三门峡市| 海淀区| 新密市| 巴南区| 建平县| 漳平市| 庄浪县| 门头沟区| 安仁县| 景宁| 元谋县| 伊吾县| 田林县| 修文县| 蕉岭县| 即墨市| 阿拉善盟| 无锡市| 临漳县| 嘉鱼县| 龙游县| 竹北市| 乳山市| 林西县| 巨野县| 合阳县|