我在指令下的 nginx.conf 文件中将字符集设置为“utf-8” http
。它看起来像这样:
http {
charset utf-8;
}
我有两个服务器指令。一个用于 example.com,另一个用于 www.example.com。它们看起来像这样。
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
root /usr/share/nginx/html;
index index.html;
location /{
try_files $uri $uri/ =404;
}
}
但是,初始请求似乎example.com
没有在Content-Type
HTTP 标头后显示字符集类型。以下是响应标头:
$ curl example.com -I --progress-bar
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 10 Jul 2014 05:10:42 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.example.com/
$ curl www.example.com -I --progress-bar
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 10 Jul 2014 05:13:30 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 123
Last-Modified: Tue, 08 Jul 2014 00:55:38 GMT
Connection: keep-alive
ETag: "53bb418a-7b"
Accept-Ranges: bytes
为什么非 www 服务器中没有设置字符集?
答案1
由于您没有为 301 错误定义自定义错误页面,因此 nginx 将发送带有预定义标头的内置特殊页面。
看https://github.com/nginx/nginx/blob/master/src/http/ngx_http_special_response.c#L646