Search code examples
nginxutf-8iso-8859-1php-7.2

Setting Charset on Nginx PHP virtual host not working


I tried to set the Charset of a virtual host like this:

server {
        root /var/www/mywebsite.com;

...

        charset iso-8859-1;
        override_charset on;

...

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                charset iso-8859-1;
                override_charset on;

        }
}

I have specified charset and overried_charset on both server and location and yet, it was still sending headers in UTF-8.

I had to modify the php.ini file from /etc/php/7.2/fpm/php.ini to specify in it default_charset = "iso-8859-1".

But I would like to let my php set to UTF-8 and specify on Nginx only for only one virtual host iso-8859-1.

On Apache we can do:

<VirtualHost mywebsite.com:80>
...
    Header set Content-Type "text/html; charset=iso-8859-1"
</VirtualHost>

How to do the same on Nginx?

Thanks.


Solution

  • It's a PHP issue not Nginx. The default PHP charset config is set to "utf-8" and you want to let it as it. So to overwrite it, add on the beginning of your script:

    ini_set('default_charset', 'iso-8859-1');