欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > Nginx >内容正文

Nginx

Nginx动态、静态分离,Nginx配置中做适配

发布时间:2025/4/16 Nginx 131 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Nginx动态、静态分离,Nginx配置中做适配 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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<tab> [服务]
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
<?php
phpinfo();
?>

常见问题:
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;}}

 

 

 

 


 

总结

以上是生活随笔为你收集整理的Nginx动态、静态分离,Nginx配置中做适配的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。