Nginx动态、静态分离,Nginx配置中做适配
Nginx[靜態(tài),動態(tài)]
頁面是html,mp3,mp4,txt,doc,pdf
動態(tài)腳本語言:shell,PHP,java
?一:? ??? ?LNMP
(Linux+Nginx+MariaDB==mysql+PHP|Python)
在192.168.4.5
軟件包列表:LNMP
實驗1:部署LNMP環(huán)境
nginx[web服務,接收用戶的請求]
php?? ? [解釋器]
yum -y localinstall php-fpm-5.4.16<tab> [服務]
mariadb?? ??? ??? ??? ?[數(shù)據(jù)庫客戶端]
mariadb-server [數(shù)據(jù)庫服務器]
mariadb-devel ?[依賴包]
php-mysql?? ??? ?[php連接mysql的擴展包]
啟動所有服務:
nginx?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?啟動[80]
systemctl start php-fpm?? ??? ?啟動[9000]
systemctl start mariadb?? ??? ?啟動[3306]
netstat -nutlp |grep 80 | 9000 |3306
實驗2:Nginx動靜分離
? ??? ??? ??? ??? ?nginx[靜態(tài)]
?? ??? ??? ??? ??? ?root html
?? ??? ??? ??? ??? ?nginx[動態(tài)]
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?PHP:9000
?? ?
判斷,如果用戶訪問的是靜態(tài)頁面,則找到,直接返回
?? ??? ?如果用戶訪問的是動態(tài)頁面,則轉發(fā)給9000
?? ??? ?location / {
?? ??? ??? ??? ?root html;
?? ??? ?}
?? ??? ?location ~ \.php$
?? ??? ??? ?root ?html;
?? ??? ??? ?fastcgi_pass ?127.0.0.1:9000;
?? ??? ??? ?include fastcgi.conf;
?? ?}
vim ?/usr/local/nginx/html/test.php
<?php
phpinfo();
?>
常見問題:
1.出現(xiàn)下載頁面 【配置文件中沒有php轉發(fā)】
2.File not found. 【轉發(fā)設置不正確】[SELinux]
3.An error occurred.
? ? 【轉發(fā)給9000后,無響應,php未啟動】
?
后臺靜態(tài)頁面:
# vim /usr/local/nginx/conf/nginx.conf server{#admin靜態(tài)頁面listen 80;server_name admin.abc.xxxxxx.com;access_log logs/frontaccess.log main;error_log logs/fronterror.log;location ~ \.(html|js|css|png|jpg|gif|woff|woff2|ttf|apk|ipa)$ {root /usr/local/html/dist/;index index.html index.htm;}location ^~ /api/{proxy_pass http://127.0.0.1:10001/;}}
二 : 地址重寫
?? ? rewrite ?正則 ? 跳轉后的URL [選項];
案例1:訪問a.html跳轉到b.html
vim /usr/local/nginx/conf/nginx.conf
... ...
?? ?server {
?? ??? ??? ?listen 80
?? ??? ??? ?server_name localhost;
?? ?location / {
?? ??? ??? ?rewrite a.html /b.html ?redirect;
?? ?}
#echo "BB" > /usr/local/nginx/html/b.html
#nginx -s reload
案例2:訪問192.168.4.5跳轉到www.tmooc.cn
vim /usr/local/nginx/conf/nginx.conf
... ...
?? ?server {
?? ??? ??? ?listen 80
?? ??? ??? ?server_name localhost;
?? ?location / {
?? ??? ??? ?rewrite ^/ ?http://www.tmooc.cn;
?? ?}
附加:
訪問舊的網站頁面,跳轉到新的網站相同頁面
? rewrite ^/(.*) ? ? http://www.jd.com/$1;
?保留和粘貼
案例2:不同瀏覽器訪問相同頁面返回結果不同
ie ?http://192.168.4.5/test.html?
firefox http://192.168.4.5/test.html
uc ?http://192.168.4.5/test.html
nginx【內置變量】
vim /usr/local/nginx/conf/nginx.conf
? server {
?? ??? ?... ...
?? ?if ($http_user_agent ~* curl){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 識別客戶端curl瀏覽器
?? ??? ?rewrite ^/(.*) ?/curl/$1;
?? ?} ??
#cd ?/usr/local/nginx/html
#echo "1" >test.html
#mkdir curl
#echo "2" >curl/test.html
#nginx -s reload
firefox http://192.168.4.5/test.html
curl http://192.168.4.5/test.html
案例:如果用戶訪問的頁面不存則轉到首頁
vim /usr/local/nginx/conf/nginx.conf
?server {
?? ??? ?... ...
?? ?if (!-e ?$request_filename){
?? ??? ?rewrite ^/ ?http://192.168.4.5;
?? ?} ?
#nginx -s reload
rewrite 正則 ?URL [選項]
rewrite選項:
last?? ??? ?停止執(zhí)行其他rewrite
break?? ??? ?停止執(zhí)行其他rewrite,并結束請求
redirect?? ?臨時重定向
permament?? ?永久重定向
?
三 : 生產環(huán)境中應用
PC端和手機端做適配:
# vim /usr/local/nginx/conf/nginx.conf server{listen 80;server_name www.xxxxx.com;access_log logs/indexaccess.log main;error_log logs/indexerror.log;location / {root /usr/local/html/dist/static/index/pc/;if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" ){root /usr/local/html/dist/static/index/mobile/;}index index.html index.htm;}}?
四 :? Nginx 中做app 跳轉下載
?
# vim /usr/local/nginx/conf/nginx.conf server {listen 80;server_name back.xxxxxx.com;access_log logs/backaccess.log main;error_log logs/backerror.log;location / {proxy_http_version 1.1;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://127.0.0.1:10001;}}?
?
?
?
?
總結
以上是生活随笔為你收集整理的Nginx动态、静态分离,Nginx配置中做适配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx调度器(反向代理),TCP/U
- 下一篇: nginx 域名跳转 ---IP跳转--