nginx 返回动态Html,Nginx动态、静态分离,Nginx配置中做适配

本文介绍了如何在Nginx中实现静态和动态内容的分离,通过配置示例展示了如何处理HTML等静态文件及转发PHP请求。同时,详细讲解了Nginx的地址重写功能,包括重定向和URL规则匹配,以及在生产环境中如何根据设备进行页面适配和APP下载跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx[静态,动态]

页面是html,mp3,mp4,txt,doc,pdf

动态脚本语言:shell,PHP,java

一:       LNMP

(Linux+Nginx+MariaDB==mysql+PHP|Python)

在192.168.4.5

软件包列表:LNMP

实验1:部署LNMP环境

nginx[web服务,接收用户的请求]

php     [解释器]

yum -y localinstall php-fpm-5.4.16[服务]

mariadb                [数据库客户端]

mariadb-server [数据库服务器]

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[静态]

root html

nginx[动态]

PHP:9000

判断,如果用户访问的是静态页面,则找到,直接返回

如果用户访问的是动态页面,则转发给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

常见问题:

1.出现下载页面 【配置文件中没有php转发】

2.File not found. 【转发设置不正确】[SELinux]

3.An error occurred.

【转发给9000后,无响应,php未启动】

后台静态页面:

# vim /usr/local/nginx/conf/nginx.conf

server{

#admin静态页面

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        停止执行其他rewrite

break        停止执行其他rewrite,并结束请求

redirect    临时重定向

permament    永久重定向

三 : 生产环境中应用

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值