【问题标题】:How to edit 'Index of etc' pages on Nginx and - Make them responsive?如何在 Nginx 上编辑“等索引”页面并让它们响应?
【发布时间】:2015-02-05 07:41:22
【问题描述】:

我正在使用 Ubuntu Natty 12 并且我正在使用 NGINX。 (这些天考虑臃肿的 Apache 真是太疯狂了)

我想要做的是编辑默认的索引页面模板并为它们添加一些 Meta ViewPorts 和样式。我知道 Apache 有 IndexHeadInsert:

# META VIEWPORT
IndexHeadInsert "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"

示例如下:

http://nginx.org/download/

http://bluu.co.uk/fonts/

https://archive.apache.org/dist/ant/binaries/

有人知道从哪里开始使用 NGINX 吗?

【问题讨论】:

    标签: apache ubuntu nginx directory directory-listing


    【解决方案1】:

    Nginx 有 sub 模块用于替换部分答案。 http://nginx.org/en/docs/http/ngx_http_sub_module.html 它适用于所有答案,包括自动索引。只需检查您的 nginx 是否有此模块(类型为nginx -V),可能您需要nginx-full 包。

    location / {
       autoindex on;
       sub_filter_once on;
       sub_filter <head><title> "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>";
    }
    

    如果您只需要对一个位置(例如 /download/)进行此替换,只需将其放在位置块中即可。

    location = /download/ {
       autoindex on;
       sub_filter_once on;
       sub_filter <head><title> "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>";
    }
    

    = 这里只匹配 /download/,但不匹配 /download/file...

    如果您有很多位置并且想将规则保留在一个地方,您可以尝试将 403 错误重定向到指定位置

    location @subfilter {
       autoindex on;
       sub_filter <head><title> "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>";
    }
    
    location = /files/ {
         error_page 403 =200 @subfilter;
    }
    
    location = /download/ {
         error_page 403 =200 @subfilter;
    }
    

    但是子模块仍然很弱,因为您只能使用 一个 sub_filter 指令 =( 可以通过对自身的代理请求进行破解,但这是不好的解决方案。最好使用 3rd 方模块 https://github.com/yaoweibin/ngx_http_substitutions_filter_module

    html5,都一样: sub_filter "&lt;html&gt;\r\n&lt;head&gt;&lt;title&gt;" "&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;&lt;title&gt;";

    【讨论】:

    • 有什么方法可以编辑页面结构而不是附加一个hack;我想在全球范围内这样做。
    • 不,这是一个非常简单的模块,没有任何设计设置。可以查看第三方模块github.com/aperezdc/ngx-fancyindex,但不知道现代nginx是否支持,需要编译nginx才能使用。
    • 如果你的位置很少......让我编辑答案,我会告诉你
    • 谢谢你这么好的回答;最后一个问题 - 我可以将 HTML 更改为 HTML5 文档类型吗?
    • 已编辑。都一样,只需要在合适的地方加上\r\n就可以匹配了。
    猜你喜欢
    • 2021-10-21
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2019-11-01
    相关资源
    最近更新 更多