什么是動靜分離
為了提高網站的響應速度,減輕程序服務器(apache+php,nginx+php等)的負載,對于靜態資源比如圖片,js,css,html等靜態文件,我們可以在反向代理服務器中設置,將訪問不同類型的資源分別調度到后端不同服務器組,這樣客戶端請求一個靜態資源時,代理服務器將請求代理到靜態資源服務器組。用戶請求的動態文件比如php,jsp則轉發給后端php服務器或tomcat應用服務器處理,這就是動靜分離。這也是反向代理服務器的一個重要的作用。
本文的動靜分離主要是通過nginx+httpd和php+nginx來實現,其中一臺nginx作為前端反向代理服務器負責分離用戶訪問的請求,httpd+php處理php的動態請求,另外一臺nginx處理圖片、html、JS、CSS等靜態文件。
基礎知識了解完后,咱們來具體實踐一下動靜分離的感覺。。
環境介紹
服務器名稱 | 系統類型 | 預裝的軟件 | ip地址 | 域名 |
nginx代理服務器 | Centos7.5 | nginx | 192.168.2.221 | www.aa1.com |
httpd+php動態解析服務器 | Centos7.5 | httpd+php | 192.168.2.222 | www.aa2.com |
靜態資源服務器 | Centos7.5 | nginx | 192.168.2.225 | www.aa5.com |
????1、代理服務器:
????????服務:Nginx,并配置代理與動靜分離到后端兩臺動態與靜態服務器上
????2、動態服務器:????
????????服務:httpd、PHP,負責處理客戶端請求php動態頁面
????3、靜態服務器
????????服務:nginx,負責處理客戶端請求的圖片、js、css、html等靜態資源
配置過程
一、配置前的初始配置,配置好ip地址、關閉防火墻、selinux、配置hosts解析文件(使用虛擬主機的不同域名)實現各主機互訪;這個步驟三個主機都操作,我只操作一個
[root@centos01?/]#?systemctl?stop?firewalld [root@centos01?/]#?setenforce?0 [root@centos01?/]#?vim?/etc/hosts 192.168.2.221???www.aa1.com 192.168.2.222???www.aa2.com 192.168.2.225???www.aa5.com
二、給各服務器安裝需要的軟件
代理服務器安裝nginx軟件
[root@centos01?/]#?yum?install?-y?nginx
httpd+php服務器安裝軟件
[root@centos02?/]#?yum?install?-y?php?php-mysql?php-mbstring?php-gd?php-common?httpd
靜態服務器安裝軟件
[root@centos03?/]#?yum?install?-y?nginx
三、各服務器配置參數
配置httpd+php服務器
????1.配置httpd支持php解析,添加index.php主頁支持,添加在httpd配置調用php模塊
[root@centos02?/]#?vim?/etc/httpd/conf/httpd.conf? <IfModule?log_config_module>LogFormat?"%{X-Real-IP}i?%l?%u?%t?\"%r\"?%>s?%b?\"%{Referer}i\"?\"%{User-Agent}i\""?proxylog????##代理的請求日志格式CustomLog?"logs/access_log"?proxylog????????##調用代理的日志格式(顯示真實客戶端ip地址,默認日志只顯示代理服務器ip) </IfModule> <IfModule?dir_module>DirectoryIndex?index.php?index.html </IfModule> ...略AddType?application/x-httpd?.php ...略LoadModule?php5_module?modules/libphp5.so
????2.在網站根目錄新建一個php測試頁,將httpd服務加入開啟自啟然后啟動httpd服務
[root@centos02?/]#?vim?/var/www/html/index.php <?php phpinfo(); ?> [root@centos02?/]#?systemctl?enable?httpd Created?symlink?from?/etc/systemd/system/multi-user.target.wants/httpd.service?to?/usr/lib/systemd/system/httpd.service. [root@centos02?/]#?systemctl?start?httpd
????3.使用客戶端添加hosts解析,瀏覽器訪問http://www.aa2.com 驗證php解析是否正常
配置nginx代理服務器
????1.新建一個虛擬主機配置文件,添加代理服務器配置參數
[root@centos01?/]#?vim?/etc/nginx/conf.d/proxy.conf
server?{listen?80;server_name?www.aa1.com;
#設置真實客戶端的ip地址proxy_set_header?X-Real-IP?$remote_addr;proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
#匹配php請求,將請求轉發到動態服務器組location?~*?\.php$?{proxy_pass?http://www.aa2.com;}
#匹配直接訪問流量,將請求轉發到動態服務器組location?/?{proxy_pass?http://www.aa2.com;}
#匹配以靜態資源后綴結尾的,將請求轉發到靜態服務器組location?~*?\.(png|jpg|jpeg|html|htm|js|css|xml)$?{proxy_pass?http://www.aa5.com;}
}
????2.檢查nginx配置文件語法是否正確并啟動nginx代理服務
[root@centos01?/]#?nginx?-t? [root@centos01?/]#?nginx? [root@centos01?/]#?ss?-tnl?|?grep?80
????3.使用客戶端訪問代理服務器域名www.aa1.com驗證動態資源反代是否成功
靜態服務器配置
????1.新建靜態web虛擬主機,配置靜態資源網站根目錄,配置訪問日志格式使用proxy格式(顯示真實的客戶端ip地址)
[root@localhost?/]#?vim?/etc/nginx/conf.d/static.conf server?{listen?80;server_name?www.aa5.com;access_log?/var/log/nginx/accecc.log?proxy;location?/?{root?/web/;} }
????2.修改靜態服務器主配置文件,添加一個proxy日志格式(名稱一定和上面一致);測試配置文件語法是否正確,啟動nginx服務
[root@localhost?/]#?vim?/etc/nginx/nginx.conf?
http?{
...略log_format??proxy??'$http_x_forwarded_for?-?$remote_user?[$time_local]?"$request"?''$status?$body_bytes_sent?"$http_referer"?''"$http_user_agent"?"$http_x_forwarded_for"';
...略
}
[root@localhost?/]#?nginx?-t?
[root@localhost?/]#?nginx
????3.創建靜態網站根目錄,新建一個靜態頁面文件
[root@localhost?/]#?mkdir?/web
[root@localhost?/]#?vim?/web/aa5.html
<h1>This?is?static?server?resources?<h1>
最后的結果驗證
????方法一:
????1.使用客戶端訪問http://www.aa1.com/index.php或者直接訪問主頁,結果反饋的是httpd+php服務器的php信息頁面
????2.客戶端訪問代理服務器http://www.aa1.com/aa5.html的靜態html頁面,結果返回的是靜態服務器上面的靜態資源網頁(httpd服務器沒有)
????方法二:搭建phpMyAdmin網站程序驗證動靜分離
????1.將phpMyAdmin的網站源碼上傳至httpd網站根目錄并解壓,然后客戶端訪問代理服務器http://www.aa1.com/查看結果;可以看到php頁面可以訪問,靜態圖片無法正常顯示
???????
????2.將phpMadmin源碼上傳到靜態服務器網站根目錄,然后再次訪問代理服務器的www.aa1.com網頁查看結果(可以發現頁面已經顯示正常)
轉載于:https://blog.51cto.com/13777759/2396984