【问题标题】:How to make nginx include any of the index files that are hidden in the URL?如何让 nginx 包含任何隐藏在 URL 中的索引文件?
【发布时间】:2020-07-09 19:45:17
【问题描述】:

所以当我浏览我的网站https://www.example.com/somefolder 时,有一个索引文件(例如:index.php)并且页面加载正常。但是,在浏览器 URL 栏中,它只显示“https://www.example.com/somefolder”。我知道 nginx 提供了 index.php 文件,因为它列在索引部分。我们如何确保任何索引文件始终显示,而不需要远程客户端显式手动键入索引文件?

【问题讨论】:

  • 请问你这样奇怪的要求的目的是什么?这可以通过 nginx 配置中的复杂重定向规则来实现,或者(这会更好,因为它不需要额外的重定向)在生成的内容上附加一些 javascript 行。
  • 是的,我不想使用 JavaScript 参数对象来操作 URL 栏。我将使用 nginx 配置重写调整
  • 这与我在上一个问题上给你的解决方案不兼容,因为你需要覆盖默认的 nginx try_files $uri $uri/ =404 行为。
  • 啊……我明白了。嗯..我想知道为了保留所有要求,我们是否应该从索引中删除 index.php 并使用其他 403 重定向先尝试 index.php,然后再重定向到根目录....

标签: nginx indexing url-rewriting


【解决方案1】:

我试图尽可能多地保留原始行为,试试这个:

map $uri $maybe_slash {
    ~/$      '';
    default  '/';
}

server {
    ...
    location / {
        try_files $uri @check_index;
    }
    location @check_index {
        if ( -f $document_root$uri${maybe_slash}index.php ) { return 301 $uri${maybe_slash}index.php; }
        # if we also need to check for "index.html", uncomment next line
        # if ( -f $document_root$uri${maybe_slash}index.html ) { return 301 $uri${maybe_slash}index.html; }
        return 301 /index.php;
    }
    ...
}

【讨论】:

  • 非常令人印象深刻。它解决了这个问题。在我进一步添加这些要求之前,我会离开...... ;D 进一步。谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-07-05
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
相关资源
最近更新 更多