Answer a question

I'm trying to serve static files with nginx, and I've already tried adding ~* to the location option, but that doesn't seem to work with what I'm trying to do: serve files with case-insensitivity. Right now, I get a 404 if the case of the URL doesn't match the file's name in the file system.

For example, I have a file /usr/share/nginx/psimages/scripts/ajaxprogress.js, and I can access it by going to the /scripts/ajaxprogress.js URL, but I also need to access it by going to /scripts/AjaxProgress.js, which is currently giving me a 404 error when I try to load it.

Here's my config:

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';
         }
    }
}

Answers

From the authoritative World Wide Web Consortium specifications for URLs:

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.

The machine name is exempted because the Domain Name System had long prior defined names to be case-insensitive in §2.3.1.

Absent extremely compelling justification, you SHOULD NOT violate w3.org specifications as you never know what side effects it can generate.

As an example, using case-sensitive URLs can confuse caching proxies that may not distinguish between ThisFile.html and thisfile.html (or the 144 case variants of that name). Because, by definition, you ought never be able to know if you've hit a caching proxy, or how many of them, chances are high that this will yield the wrong document.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐