nginx 静态动态文件分离部署

第一种配置

在server模块中配置

if ( $http_user_agent ~* "MSIE [6-9].[0-9]") {

rewrite /* /index_ie.html break;

}

代码先判断请求的user_agent中是否含有MSIE ie浏览器标识。如果有的话,使用rewirte来找到目标静态资源。

资源地址是:/usr/local/nginx/html/index_ie.html

所以必须配置root 根目录为usr开始的一个目录。否则会默认为nginx的一个目录(我的环境是:/etc/nginx/html/),再拼接上页面地址,导致地址不正确,触发404文件找不到异常。

复制代码
server {
    listen 80;
    server_name adass.messs.com;
    root /usr/local/nginx/html;
    if ( $http_user_agent ~* "MSIE [6-9].[0-9]") {
            rewrite /* /index_ie.html break;
        }


    location /adao{
        alias /usr/local/nginx/html/adauto;
        index index.html index.html;
        try_files $uri $uri/ /adao/index.html /adao/index_ie.html;
                    
        }

}

第二种配置:

文件路径中有其他路径规则,使用break 还会去再次匹配吗?答案不会去匹配。

修改后的nginx如下,注意文件位置要移动到/usr/local/nginx/html/adao/index_ie.html。请求中并不会再次去匹配adao路径规则的。因为使用了break ,不会再继续匹配规则。

复制代码
server {
    listen 80;
    server_name adass.messs.com;
    root /usr/local/nginx/html/adao;
    if ( $http_user_agent ~* "MSIE [6-9].[0-9]") {
            rewrite /* /index_ie.html break;
        }


    location /adao{
        alias /usr/local/nginx/html/adauto;
        index index.html index.html;
        try_files $uri $uri/ /adao/index.html /adao/index_ie.html;
                    
        }

}

实验下来还是能请求到index_ie.html文件

第三种配置:

map的使用

复制代码
 map $http_user_agent $outdated {
        default                                 1;
        "~MSIE [1-10]\."                        0;
        "~Trident/[5-7]\."                      0;
    }

map放置于http模块。不可放置其他模块。

完整iginx文件配置方式为:(http模块可以省略)

复制代码
upstream mockserver {
    # server 127.0.0.1:5002;
    server localhost:5002;
}

 map $http_user_agent $outdated {
        default                                 1;
        "~MSIE [1-10]\."                        0;
        "~Trident/[5-7]\."                      0;
    }

server {
    listen 80;
    server_name adauto.mediav.com;

    location /index_ie{
        alias /usr/local/nginx/html/adao;
        index index_ie.html;
    }

    location /adao{
        alias /usr/local/nginx/html/adao;
        index index.html index.html;
        try_files $uri $uri/ /adao/index.html;
       
         if ( $outdated = 0) {
            rewrite /* /index_ie redirect;
}
    }

  
}

rewrite 替换后一个路径/index_ie,并不是文件的地址。这个路径请求后会继续匹配index_ie,找到文件地址index_ie.html

注意:nginx路径匹配规则有一条是按照文件顺序

相关推荐
北海-cherish2 小时前
vue中的 watchEffect、watchAsyncEffect、watchPostEffect的区别
前端·javascript·vue.js
一个响当当的名号3 小时前
一些主要应用和NAT
运维·服务器·网络
2501_915909063 小时前
HTML5 与 HTTPS,页面能力、必要性、常见问题与实战排查
前端·ios·小程序·https·uni-app·iphone·html5
white-persist4 小时前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
新中地GIS开发老师5 小时前
Cesium 军事标绘入门:用 Cesium-Plot-JS 快速实现标绘功能
前端·javascript·arcgis·cesium·gis开发·地理信息科学
Superxpang5 小时前
前端性能优化
前端·javascript·vue.js·性能优化
Rysxt_5 小时前
Element Plus 入门教程:从零开始构建 Vue 3 界面
前端·javascript·vue.js
隐含5 小时前
对于el-table中自定义表头中添加el-popover会弹出两个的解决方案,分别针对固定列和非固定列来隐藏最后一个浮框。
前端·javascript·vue.js
大鱼前端5 小时前
Turbopack vs Webpack vs Vite:前端构建工具三分天下,谁将胜出?
前端·webpack·turbopack
你的人类朋友5 小时前
先用js快速开发,后续引入ts是否是一个好的实践?
前端·javascript·后端