1

I have set the charset to 'utf-8' in the nginx.conf file under the http directive. It looks like this:

http {
  charset utf-8;
}

I have two server directives. One for example.com, and one for www.example.com. They look like this.

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

However, an initial request to example.com does not seem to present the charset type after the Content-Type HTTP header. Here are the response headers:

$ 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

Why isn't the charset being set in the non-www server?

1 Answer 1

2

Since you did not define custom error page for 301 error, nginx sends it's built-in special page with predefined headers.

See https://github.com/nginx/nginx/blob/master/src/http/ngx_http_special_response.c#L646

4
  • 2
    And it really doesn't matter since almost nobody will ever see that bit of HTML anyway. Jul 10, 2014 at 12:30
  • 301 is not an error. I'm sorry but your answer does not make sense. I do not why you got an up-vote.
    – Ryan
    Jul 10, 2014 at 13:25
  • It's not an error, but you could define custom page for 301 code with error_page directive. By default nginx sends its own page with this exact headers.
    – Alexey Ten
    Jul 10, 2014 at 13:41
  • And why do you ever bother this?
    – Alexey Ten
    Jul 10, 2014 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.