gpt4 book ai didi

url - Nginx 和相对路径

转载 作者:行者123 更新时间:2023-12-02 17:24:13 24 4
gpt4 key购买 nike

在 nginx root 上我们有这样的文件:

   - test\
image.jpg
index.html
- ...

在 index.html 中有指向 image.jpg 的链接:

<img src='image.jpg'>

在标准的 Nginx 配置中,我们有这样的行(http://nginx.org/ru/docs/http/ngx_http_core_module.html#try_files)

   server {
server_name example.com;
...
try_files $uri $uri/index.html;
}

对于 URL http://example.com/test/index.html (对于 http://example.com/test/ )一切都会很好,因为相对路径的基础将是 http://example.com/test/我们在这个文件夹中有 image.jpg。

对于 http://example.com/test将显示页面,但没有图像。

在这种情况下最好的解决方案是什么?

最佳答案

我认为你的意思是 $uri 带有 I 而不是 $url 带有 L

问题是 try_files 将在 $uri/index.html 获取文件,但将 URI 保留为 /test 所以任何亲戚URI 将相对于 / 而不是 /test/

如果您要在 HTML 文件中使用相对 URI,则必须在目录引用中强制执行尾随 /。例如:

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

这将导致 /test 重定向到 /test/ 并且 index 指令将自动尝试 index.html 子目录中的文件。

关于url - Nginx 和相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40151686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com