nginx之如何使用 nginx 不区分大小写地提供静态文件

qlqwjy 阅读:21 2024-10-25 08:56:14 评论:0

我正在尝试使用 nginx 提供静态文件,并且我已经尝试将 ~* 添加到 location 选项,但这似乎不适用于我正在尝试的做:提供不区分大小写的文件。现在,如果 URL 的大小写与文件系统中的文件名不匹配,我会收到 404。

例如,我有一个文件 /usr/share/nginx/psimages/scripts/ajaxprogress.js,我可以通过访问 /scripts/ajaxprogress.js 来访问它 URL,但我还需要通过转到 /scripts/AjaxProgress.js 来访问它,目前在我尝试加载它时出现 404 错误。

这是我的配置:

server { 
    error_log /var/log/nginx/ngerror.log debug; 
    listen 80 default_server; 
    listen [::]:80 default_server; 
 
    root /usr/share/nginx/psimages; 
    index index.html index.htm; 
 
    # Make site accessible from http://localhost/ 
    server_name localhost; 
 
    location ~* / { 
        # First attempt to serve request as file, then 
        # as directory, then fall back to displaying a 404. 
        try_files $uri $uri/ =404; 
        # Uncomment to enable naxsi on this location 
        # include /etc/nginx/naxsi.rules 
 
            # Wide-open CORS config for nginx 
        if ($request_method = 'OPTIONS') { 
        add_header 'Access-Control-Allow-Origin' '*'; 
        # 
        # Om nom nom cookies 
        # 
        add_header 'Access-Control-Allow-Credentials' 'true'; 
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
        # 
        # Custom headers and headers various browsers *should* be OK with but aren't 
        # 
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
        # 
        # Tell client that this pre-flight info is valid for 20 days 
        # 
        add_header 'Access-Control-Max-Age' 1728000; 
        add_header 'Content-Type' 'text/plain charset=UTF-8'; 
        add_header 'Content-Length' 0; 
        return 204; 
         } 
         if ($request_method = 'POST') { 
        add_header 'Access-Control-Allow-Origin' '*'; 
        add_header 'Access-Control-Allow-Credentials' 'true'; 
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
         } 
         if ($request_method = 'GET') { 
        add_header 'Access-Control-Allow-Origin' '*'; 
        add_header 'Access-Control-Allow-Credentials' 'true'; 
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
         } 
    } 
} 

请您参考如下方法:

来自权威World Wide Web Consortium specifications对于网址:

URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn't matter, but identifying these may not be easy. Users should always consider that URLs are case-sensitive.

机器名被免除,因为Domain Name System在 §2.3.1 中早就定义了不区分大小写的名称。

如果没有非常令人信服的理由,您不应该违反 w3.org 规范,因为您永远不知道它会产生什么副作用。

例如,使用区分大小写的 URL 可能会混淆可能无法区分 ThisFile.htmlthisfile.html(或其 144 个大小写变体)的缓存代理姓名)。因为,根据定义,您永远无法知道您是否命中了缓存代理,或者命中了多少缓存代理,因此产生错误文档的可能性很高。


标签:nginx
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号