Nginx – Bad Gateway 502 (nginx + php7) on Digital Ocean

gatewaynginxPHP

Every time when I try to publish my site, I get after a couple of minutes 502 Bad Gateway error, and in my default-error.log file I get this error over and over (file size was 360MB..):

2016/02/03 14:57:28 [error] 1044#1044: *7248 connect() to unix:/var/run/php/php7.0-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 123.456.78.90, server: default, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "domain.com"

Server Configuration

  • OS: Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-71-generic x86_64)
  • Nginx Version: 1.9.10
  • PHP Version PHP 7.0.2-4+deb.sury.org~trusty+1 (cli) ( NTS )
  • (My server is hosted in Digital Ocean and it has 4GB RAM).

nginx.conf:

server {
    listen 80 default_server;
    server_name default;
    root /home/forge/default/public;

    # FORGE SSL (DO NOT REMOVE!)
    include logjam_fix;
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log on;
    error_log  /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Is there any data I'm missing?

Best Answer

Try increasing the max number of open files:

root@server $ sysctl fs.file-max=3000000000

Edit:

You can usually have these changes be made permanent by adding the line to /etc/sysctl.conf:

fs.file-max=3000000000