AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 783024
Accepted
jwerre
jwerre
Asked: 2016-06-10 08:08:11 +0800 CST2016-06-10 08:08:11 +0800 CST 2016-06-10 08:08:11 +0800 CST

Nginx 别名不起作用

  • 772

我正在尝试将 location 的所有流量传递^/[a-z0-9]{24}$给不同根目录的 index.html。我的配置设置如下:

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {

    listen 443 ssl;

    server_name example.com;
    root /Users/me/sites/store_front/static;
    index index.html;

    try_files $uri $uri/ $uri.html =404;

    location ~ "^/[a-z0-9]{24}$" {
        alias /Users/me/web_app/static;
        try_files $uri index.html;
    }
}

出于某种原因,当我卷曲这个网址时,我得到一个 404:

$ curl -I https://example.com/55c898e7faa137f42409d403

HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Content-Type: text/html; charset=UTF-8
Content-Length: 168
Connection: keep-alive

有谁知道如何让这个别名工作?

更新:

需要注意的是,我需要 index.html 中的所有相对 url 才能正确加载:

<link href="/styles/main.css" rel="stylesheet" type="text/css"
<script src="/javascript/main.js"></script>

这些文件实际上存在于web_app目录中,但 nginx 尝试从中加载它们store_front

谢谢

nginx alias
  • 1 1 个回答
  • 1651 Views

1 个回答

  • Voted
  1. Best Answer
    Richard Smith
    2016-06-10T08:21:48+08:002016-06-10T08:21:48+08:00

    指向很^/[a-z0-9]{24}$容易index.html。资源文件需要具有唯一的 URI,否则/styles/main.css不知道要提供哪些文件。/javascript/main.jsnginx

    如果store_front 也使用/stylesand/javascript前缀,则需要隔离各个文件:

    location = /styles/main.css {
        root /Users/me/web_app/static;
    }
    location = /javascript/main.js {
        root /Users/me/web_app/static;
    }
    location ~ "^/[a-z0-9]{24}" {
        root /Users/me/web_app/static;
        rewrite ^ /index.html break;
    }
    

    如果/styles,/javascript和^/[a-z0-9]{24}$URI 都是唯一的,您可以将上述内容组合到一个位置:

    location ~ "^/([a-z0-9]{24}|styles|javascript)" {
        root /Users/me/web_app/static;
        rewrite "^/[a-z0-9]{24}$" /index.html break;
    }
    

    甚至:

    location ~ "^/([a-z0-9]{24}|styles/main.css|javascript/main.js)" {
        root /Users/me/web_app/static;
        rewrite "^/[a-z0-9]{24}$" /index.html break;
    }
    
    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve