=Start=
缘由:
有时为了方便自己,会在VPS的博客根目录下放置一些文本文件(TXT或HTML),比如:http://ixyzero.com/blog/awk_sed.txt 和 http://ixyzero.com/blog/regex2.html ,但是在用浏览器访问这些页面的时候,会出现乱码,特别是TXT文件,对于HTML文件还可以通过设置<head>标签中的meta信息来指定浏览器打开时的编码方式:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 或 <meta charset="UTF-8">
但对于TXT文件来说我就不知道该怎么办了,文件内容本身就已经是UTF-8编码了(看到有一篇文章说对于UTF-8 BOM格式的文本文件,部分浏览器会将其识别为UTF-8编码格式,但是感觉这样治标不治本,没有很好的解决问题),在经过一番搜索之后解决了问题,在此记录一下,方便以后参考。
参考解答:
在Nginx配置文件中添加/修改下面的指令到 http 或 server 或 location 段中,即可为对应范围下的请求自动在HTTP头部中添加 charset 字段:
charset UTF-8;
修改Nginx的配置文件之后记得测试一下写的是否正确,然后重新加载一下即可:
$ curl -I http://ixyzero.com/blog/awk_sed.txt HTTP/1.1 200 OK Server: nginx Date: Wed, 21 Sep 2016 11:36:05 GMT Content-Type: text/plain Content-Length: 21406 Last-Modified: Tue, 20 Sep 2016 13:16:12 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "57e1369c-53e5" Accept-Ranges: bytes # nginx -t # nginx -s reload $ curl -I http://ixyzero.com/blog/awk_sed.txt HTTP/1.1 200 OK Server: nginx Date: Wed, 21 Sep 2016 11:44:04 GMT Content-Type: text/plain; charset=utf-8 Content-Length: 21406 Last-Modified: Wed, 21 Sep 2016 11:40:16 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "57e271a0-539e" Accept-Ranges: bytes
在Apache配置文件中对应的指令是:
AddDefaultCharset utf-8
参考链接:
- http://www.cyberciti.biz/faq/nginx-set-http-content-type-response-header-to-charset-utf8/
- http://serverfault.com/questions/312177/how-to-enable-correct-charset-http-header-in-nginx
- http://nginx.org/en/docs/http/ngx_http_charset_module.html#charset
- http://stackoverflow.com/questions/19772955/utf-8-not-working-nginx
- http://www.linuxyan.com/web-server/348.html
- =
- https://httpd.apache.org/docs/2.4/mod/core.html
- http://stackoverflow.com/questions/913869/how-to-change-the-default-encoding-to-utf-8-for-server
=END=
《 “由Web服务器指定文件的编码” 》 有 7 条评论
HTML meta标签总结与属性使用介绍
http://lxxyx.win/2016/01/09/%E5%AF%92%E5%81%87%E5%89%8D%E7%AB%AF%E5%AD%A6%E4%B9%A0(1)%E2%80%94HTML%20meta%E6%A0%87%E7%AD%BE/
一些安全相关的HTTP响应头
https://imququ.com/post/web-security-and-response-header.html
HTTP 协议中的 Content-Encoding
https://imququ.com/post/content-encoding-header-in-http.html
HTTP 协议中的 Transfer-Encoding
https://imququ.com/post/transfer-encoding-header-in-http.html
HTTP 协议中 Vary 的一些研究
https://imququ.com/post/vary-header-in-http.html
HTTP 请求头中的 X-Forwarded-For
https://imququ.com/post/x-forwarded-for-header-in-http.html
HTTP 请求头中的 Proxy-Connection
https://imququ.com/post/the-proxy-connection-header-in-http-request.html
nginx-upload-module模块实现文件断点续传
http://blog.csdn.net/weiwangchao_/article/details/76039480
Nginx 整数溢出(CVE-2017-7529)漏洞分析
http://paper.seebug.org/353/
http://galaxylab.org/cve-2017-7529-nginx%E6%95%B4%E6%95%B0%E6%BA%A2%E5%87%BA%E6%BC%8F%E6%B4%9E%E5%88%86%E6%9E%90/
小心Nginx的add_header指令
https://tlanyan.me/be-careful-with-nginx-add_header-directive/
https://tlanyan.me/tag/nginx/
Common MIME types
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
MIME types (IANA media types)
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
Media Types
https://www.iana.org/assignments/media-types/media-types.xhtml
What is the meaning of “vnd” in MIME types?
https://stackoverflow.com/questions/5351093/what-is-the-meaning-of-vnd-in-mime-types
`
vnd indicates vendor-specific MIME types, which means they are MIME types that were introduced by corporate bodies rather than e.g. an Internet consortium.
vnd表示特定供应商的MIME类型,这意味着它们是由法人团体引入的MIME类型,而不是例如Internet联盟。
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.ico image/vnd.microsoft.icon
.mpkg application/vnd.apple.installer+xml #Apple Installer Package
`
Do I need Content-Type: application/octet-stream for file download?
https://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download
`
No.
在 RFC 2046 中,application/octet-stream 被定义为 “任意二进制数据”,它与那些唯一目的是保存到磁盘上的实体有一定的重叠,因此不属于任何 “网络 “内容。或者从另一个角度看,应用/八进制流的唯一安全做法就是将其保存到文件中,并希望别人知道它的用途。
`
Which JSON content type do I use?
https://stackoverflow.com/questions/477816/which-json-content-type-do-i-use
`
For JSON text:
application/json
For JSONP (runnable JavaScript) with callback:
application/javascript
`
Content-Type
“`
种类太多,前期先用白名单机制(只关注特定的Content-Type),后面可以采用黑明单机制(只关注新的Content-Type)。
# json字符串
application/json;charset=utf-8
application/json
# 任意二进制数据
application/octet-stream
# 由制表符分隔的 TSV 文件格式与 CSV 文件格式非常相似,但 CSV 文件中存储的数据字段是用逗号而不是制表符空格分隔的。两者都是一种分隔符分隔值格式。
text/tab-separated-values;charset=utf-8
# js/css
text/javascript;charset=utf-8
text/css;charset=utf-8
# 图片
image/gif
image/jpeg
image/png
image/webp
# 视频
video/mp4
video/mp2t
# 字体
font/woff2
font/woff
font/ttf
application/font-woff
application/font-woff2
# 文本
text/plain
text/json #非IANA官方推荐的json字符串类型,但浏览器也能识别和支持
text/html
text/xml
……
“`
Which JSON content type do I use?
https://stackoverflow.com/questions/477816/which-json-content-type-do-i-use
`
For JSON text:
application/json
For JSONP (runnable JavaScript) with callback:
application/javascript
==
IANA has registered the official MIME Type for JSON as application/json.
When asked about why not text/json, Crockford seems to have said JSON is not really JavaScript nor text and also IANA was more likely to hand out application/* than text/*.
A lot of stuff got put into the text/* section in the early days that would probably be put into the application/* section these days.
The most widely supported non-standard media types are text/json or text/javascript. But some big names even use text/plain.
==
IANA 已将 JSON 的官方 MIME 类型注册为 application/json。
当被问及为何不注册 text/json 时,Crockford 似乎说 JSON 并非真正的 JavaScript,也不是文本,而且 IANA 更倾向于注册 application/* 而非 text/*。
早期有很多东西被放到 text/* 部分,而现在这些东西可能会被放到 application/* 部分。
最广泛支持的非标准媒体类型是 text/json 或 text/javascript。但有些大公司甚至使用 text/plain。
`