PrestaShop + nginx: Fix 502 Error When Logging In

How to fix 502 errors when logging into PrestaShop stores using nginx web servers. Add 2 lines to your PrestaShop store's nginx config file.

If you're running PrestaShop on an nginx-powered web server, you may encounter '502 errors' when you or your customers try to log into your store.

This can be fixed by adding a couple of lines of configuration to your nginx server settings.

Quick solution for advanced users

If you know what you're doing, add these 2 lines to the PHP FastCGI brackets of your PrestaShop store's config, or within the http {} of your nginx.conf to apply the fix to every website on your web server.

fastcgi_buffers 8 16k; fastcgi_buffer_size 32k;

Reload your nginx config using

$ /etc/init.d/nginx reload

You should now be able to log into your PrestaShop store without any 502 errors during authentication.

If that made no sense, please look below for the detailed explanation.

Explained solution

You'll need SSH/SFTP access to your server, or some other means of accessing your nginx config file. If you are unsure if you have access to this, please consult your web host's support.

Please note: Server configurations and settings differ greatly from site to site. Your website's configuration may not look exactly like the examples shown here, but there should be noticeable similarities. Please adjust our examples to match your configuration as appropriate.

1. Open your PrestaShop store's configuration file

Navigate to /etc/nginx/sites-available/ and open your PrestaShop store's configuration file. We will use the domain example.com, change this to your PrestaShop store's domain where appropriate.

For example, you would open /etc/nginx/sites-available/example.com.

You can use vi via SSH (learn how to use VI), or use FileZilla to connect via SFTP and edit the remote file in a text editor.

2. Add the fix

Once editing your website's configuration file, you'll need to add the following within PHP's brackets, this should appear as, or similar to:

location ~ \.php {

Add the following within the brackets alongside your other PHP settings

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;

For example, your PrestaShop store's nginx PHP config should look similar to this:

location ~ \.php {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/example.com/public_html$fastcgi_script_name;
}

Example website configuration

An example of a full working website configuration file would be as follows:

server {
    listen 80;
    server_name example.com;
    access_log /var/www/example.com/logs/access.log;
    error_log /var/www/example.com/logs/error.log;
    root /var/www/example.com/public_html;

    location / {
        index index.php;

        # serve static files directly
        if (-f $request_filename) {
            access_log off;
            expires 30d; break;
        }

        ## Add trailing slash
        rewrite ^(.*[^/])$ $1/ permanent;
    }

    location ~ \.php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/public_html$fastcgi_script_name;
    }
}

Your config file should look somewhat similar to this. As mentioned, you may have additional settings depending on your web server's setup, but this should be bare some resemblance to your configuration.

3. Reload your nginx configuration

For the changes to take effect, we need to reload nginx's server configuration. This can be done by running the following command in your SSH terminal:

$ /etc/init.d/nginx reload

If you don't have access to SSH, you should have some method to do this via CPanel/Plesk/Similar dashboard area providing by your web hosting company.

You should now be able to log in to your front office of your PrestaShop store!