Nginx,charset指令在发送301redirect时不起作用

我已经在http指令下的nginx.conf文件中将charset设置为“utf-8”。 它看起来像这样:

 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标头之后呈现字符集types。 以下是响应标题:

 $ 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服务器中设置字符集?

由于您没有为301错误定义自定义错误页面,因此nginx会将其内置的特殊页面与预定义的标题一起发送。

请参阅https://github.com/nginx/nginx/blob/master/src/http/ngx_http_special_response.c#L646