Question

Receiving "502 Bad Gateway" Nginx

Posted March 29, 2021 1.3k views
Nginx

I’m hosting a selfmade php and mysql panel in a VPS using nginx. The problem is that I’m receiving “502 Bad” Gateway when I access the website.

I’ve searched for some solutions here on DO but I couldn’t find anything that help me. I tried tons of tutorials so I’m here to ask for help.

When I do nginx -t I get:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

The logs:

2021/03/29 09:19:11 [crit] 23478#23478: *20 SSL_do_handshake() failed (SSL: error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol) while SSL handshaking, client: 35.203.251.60, server: 0.0.0.0:443
2021/03/29 09:19:12 [crit] 23478#23478: *24 SSL_do_handshake() failed (SSL: error:141CF06C:SSL routines:tls_parse_ctos_key_share:bad key share) while SSL handshaking, client: 35.203.251.60, server: 0.0.0.0:443
2021/03/29 09:24:13 [crit] 23478#23478: *36 SSL_do_handshake() failed (SSL: error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol) while SSL handshaking, client: 107.178.232.178, server: 0.0.0.0:443
2021/03/29 09:24:14 [crit] 23478#23478: *40 SSL_do_handshake() failed (SSL: error:141CF06C:SSL routines:tls_parse_ctos_key_share:bad key share) while SSL handshaking, client: 107.178.232.178, server: 0.0.0.0:443
2021/03/29 10:13:42 [crit] 23478#23478: *53 SSL_do_handshake() failed (SSL: error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol) while SSL handshaking, client: 192.241.225.107, server: 0.0.0.0:443
2021/03/29 10:13:43 [crit] 23478#23478: *57 SSL_do_handshake() failed (SSL: error:141CF06C:SSL routines:tls_parse_ctos_key_share:bad key share) while SSL handshaking, client: 192.241.225.107, server: 0.0.0.0:443
2021/03/29 11:41:35 [error] 23478#23478: *81 directory index of "/var/www/html/" is forbidden, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", host: "painel.unturned.root@mysql:~# sudo tail -30 /var/log/nginx/error.log
sudo: unable to resolve host mysql
2021/03/29 09:16:57 [notice] 23466#23466: signal process started
2021/03/29 09:17:00 [notice] 23468#23468: signal process started
2021/03/29 09:17:02 [notice] 23475#23475: signal process started
2021/03/29 09:17:03 [notice] 23477#23477: signal process started
2021/03/29 09:19:11 [crit] 23478#23478: *20 SSL_do_handshake() failed (SSL: error:14209102:SSL routiroot@mysql:~# sudo tail -30 /var/log/nginx/error.log
sudo: unable to resolve host mysql
2021/03/29 09:16:57 [notice] 23466#23466: signal process started
2021/03/29 09:17:00 [notice] 23468#23468: signal process started
2021/03/29 09:17:02 [notice] 23475#23475: signal process started
2021/03/29 09:17:03 [notice] 23477#23477: signal process started
2021/03/29 09:19:11 [crit] 23478#23478: *20 SSL_do_handshake() failed (SSL: error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol) while SSL handshaking, client: 35.203.251.60, server: 0.0.0.0:443
2021/03/29 09:19:12 [crit] 23478#23478: *24 SSL_do_handshake() failed (SSL: error:141CF06C:SSL routines:tls_parse_ctos_key_share:bad key share) while SSL handshaking, client: 35.203.251.60, server: 0.0.0.0:443
2021/03/29 09:24:13 [crit] 23478#23478: *36 SSL_do_handshake() failed (SSL: error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol) while SSL handshaking, client: 107.178.232.178, server: 0.0.0.0:443
2021/03/29 09:24:14 [crit] 23478#23478: *40 SSL_do_handshake() failed (SSL: error:141CF06C:SSL routines:tls_parse_ctos_key_share:bad key share) while SSL handshaking, client: 107.178.232.178, server: 0.0.0.0:443
2021/03/29 10:13:42 [crit] 23478#23478: *53 SSL_do_handshake() failed (SSL: error:14209102:SSL routines:tls_early_post_process_client_hello:unsupported protocol) while SSL handshaking, client: 192.241.225.107, server: 0.0.0.0:443
2021/03/29 10:13:43 [crit] 23478#23478: *57 SSL_do_handshake() failed (SSL: error:141CF06C:SSL routines:tls_parse_ctos_key_share:bad key share) while SSL handshaking, client: 192.241.225.107, server: 0.0.0.0:443
2021/03/29 11:41:35 [error] 23478#23478: *81 directory index of "/var/www/html/" is forbidden, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", host: "painel.unturned.com.br"
2021/03/29 11:43:07 [error] 23478#23478: *93 directory index of "/var/www/html/" is forbidden, client: 34.72.86.44, server: painel.unturned.com.br, request: "GET / HTTP/1.1", host: "painel.unturned.com.br"
2021/03/29 11:45:17 [error] 23478#23478: *95 directory index of "/var/www/html/" is forbidden, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", host: "painel.unturned.com.br"
2021/03/29 11:50:38 [error] 23478#23478: *96 directory index of "/var/www/html/" is forbidden, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", host: "painel.unturned.com.br"
2021/03/29 11:54:12 [error] 23478#23478: *100 directory index of "/var/www/html/" is forbidden, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", host: "painel.unturned.com.br"
2021/03/29 11:57:01 [emerg] 24879#24879: "listen" directive is not allowed here in /etc/nginx/sites-enabled/default:70
2021/03/29 11:57:51 [emerg] 24890#24890: "location" directive is not allowed here in /etc/nginx/sites-enabled/default:54
2021/03/29 11:59:19 [alert] 24904#24904: *2 open socket #15 left in connection 6
2021/03/29 11:59:19 [alert] 24904#24904: aborting
2021/03/29 11:59:19 [emerg] 24922#24922: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/default:108
2021/03/29 11:59:58 [emerg] 24931#24931: "fastcgi_pass" directive is duplicate in /etc/nginx/sites-enabled/default:62
2021/03/29 12:01:02 [emerg] 24945#24945: "fastcgi_pass" directive is duplicate in /etc/nginx/sites-enabled/default:44
2021/03/29 12:03:58 [crit] 24991#24991: *1 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "painel.unturned.com.br"
2021/03/29 12:05:48 [crit] 31136#31136: *1 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 179.159.164.1, server: painel.unturned.com.br, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "painel.unturned.com.br"

My default.cnf:

server {

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name painel.unturned.com.br;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/painel.unturned.com.br/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/painel.unturned.com.br/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {

    if ($host = painel.unturned.com.br) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name painel.unturned.com.br;
    return 404; # managed by Certbot
}

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hello there,

The reason may be that you are using PHP5, not PHP7. To check what version your PHP is, just run the following command:

ls /var/run | grep php

In Nginx, you have to put the correct path to the correct PHP socket file.

Recently I ran into the same problem. I had no idea what to do, but then I found this video and it really helped me out. Here is the link to the video:

https://www.youtube.com/watch?v=obRxID54H14&t=111s&ab_channel=BobbyIliev

Hopefully, it will as helpful to you as it was to me.

Regards,
Boyan