大网数据广告活动
服务器运维问答

Have files with querystring included in their name. Need nginx to ignore querystring for the content header

· 52CE 我爱测

Have a directory with lots of files. These are all static files from backups. The issue is that some files that are named like the following: mystyle.css?version=3.3.1 When serving the files, nginx is not detecting the file is a …

问题原文

原帖链接 · Mamsaac · CC BY-SA 4.0

Have a directory with lots of files. These are all static files from backups.

The issue is that some files that are named like the following:

mystyle.css?version=3.3.1

When serving the files, nginx is not detecting the file is a css, and thus doesn't send the Content-Type: text/css header and it is giving me some trouble. It is very troublesome to rename the files, since it would require renaming a ton of files, while also renaming them inside of html, javascript, etc. Could it be possible to assist nginx into ignoring the ? and the rest of the query string and only check mystyle.css for the determination of the kind of mimetype to serve?

This is particularly troublesome for things like SVG images, since the browsers don't detect them as such unless you serve them as image/svg+xml, but this also causes trouble in other situations.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

Nginx looks at the final filename and concludes that the extension is css?version=3.3.1. It looks this up in the types block and finds no match, so the value of the default_type is used instead.

If you have all CSS type files under one folder and all SVG type files under another - or if the URI path for different types is unique, you can set the mime-type for all files within a particular location:

location /css {
    types { }
    default_type text/css;
}

From the first line of your question, you indicate that there is one directory with lots of files, presumably with different mime-types.

You can use a map directive with a regular expression to identify the mime-type of the files. You will need to add a line for every type of file contained in the directory. My example contains just a few. The file /etc/nginx/mime.types contains the types Nginx is usually configured to recognise, but you will only need to add the types of files you currently archive to this directory.

map $request_uri $type {
    default         application/octet-stream;
    ~*\.css         text/css;
    ~*\.js          application/javascript;
}

server {
    ...
    location /some/path {
        default_type "";
        types { }
        add_header Content-Type $type;
    }
}

DISCLAIMER: This is probably a hack, but appears to work.

Setting the default_path to an empty string together with an empty types block, prevents Nginx from adding its own Content-Type header.

See this document for details of the map directive.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。