Nginx Location 匹配到多个静态文件路径

新手上路,请多包涵

问题描述

RT 想设置两个不同的前端项目访问不同的静态资源,但是不知道如何设置,例如
wechat 访问 wechat 路径下的 css 和 js 文件
manager 访问 manager 路径下的 css 和 js 文件

问题出现的环境背景及自己尝试过哪些方法

Nginx 配置中 用了好几种正则和直接目录访问 但是都无效, 使用通配符的话就没法返回指定的 js

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

你期待的结果是什么?实际看到的错误信息又是什么?

server {
        listen 80;
        server_name 127.0.0.1;
        root /opt/;
        location / {
            try_files $uri @gunicorn_proxy;
        }

        location @gunicorn_proxy {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_pass http://localhost:5000;
        }

        location /wechat/ {
            index index.html;
            try_files $uri $uri/ /index.html;
        }

        location /manager/ {
           index index.html
           try_files $uri $uri/ /index.html;
        }

        location ~*.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {
            root /opt/manager/;
        }

        location /wechat/(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {
            root /opt/wechat/;
        }
}
阅读 15.2k
2 个回答

location之间是讲究先后顺序的,把最后一个location放在最前面试试。
而且/wechat/(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$这个正则有点问题吧?能匹配图片嘛?

新手上路,请多包涵
location /wechat {
            index index.html;
            try_files $uri $uri/ /wechat/index.html;
        }

        location /manager {
           index index.html
           try_files $uri $uri/ /manager/index.html;
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题