Skip to content

Magento in NGINX 502 Bad Gateway

I’m running a Kubernetes Ubuntu pod and have installed all the dependencies and packages inside the pod. I have also installed PHP-FPM-8.1 and NGINX. And then I ran the below commands.

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.5

composer install

php bin/magento setup:install --base-url=https://my-magento-url --db-host=mysql-service --db-name=magento_db --db-user=magento_user --db-password=abc12345678 --admin-firstname=admin --admin-lastname=admin [email protected] --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/New_York --use-rewrites=1 --elasticsearch-host=elasticsearch-service --elasticsearch-port=9200

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

php bin/magento cache:flush

chmod -R 777 pub/static

chmod -R 777 var

chmod -R 777 generated

The nginx.conf is in /etc/nginx/sites-enabled/ path and below is the config file.

server {
    listen 80;
    server_name my-magento-url; # Replace with your actual domain
    
    set $MAGE_ROOT /var/www/html/;    

    root /var/www/html/pub;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Static files configuration
    location /static/ {
        # Uncomment the following line in production mode
        # expires max;

        # Remove signature of the static files that is used to overcome the browser cache
        location ~ ^/static/version {
            rewrite ^/static/(versiond*/)?(.*)$ /static/$2 last;
        }

        location ~* .(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
            add_header Cache-Control "public";
            add_header X-Frame-Options "SAMEORIGIN";
            expires +1y;

            if (!-f $request_filename) {
                rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
            }
        }

        location ~* .(zip|gz|gzip|bz2|csv|xml)$ {
            add_header Cache-Control "no-store";
            add_header X-Frame-Options "SAMEORIGIN";
            expires    off;

            if (!-f $request_filename) {
                rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
            }
        }

        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
        add_header X-Frame-Options "SAMEORIGIN";
    }

    location ~ .php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param MAGE_RUN_CODE default;
        fastcgi_param MAGE_RUN_TYPE store;

        # Increase buffer sizes for large headers
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
    }

    location ~* .(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
        expires max;
        log_not_found off;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

So when I access https://my-magento-url the home page loads successfully, but when I try to access the admin page (https://my-magento-url/admin_adasd) it shows 502 Bad Gateway. Also if I click on anything on the homepage (For example, Login button) it shows 502 Bad Gateway.

But the homepage has no issues. And NGINX logs doesn’t show any errors. Any idea why it shows 502 Bad Gateway?