0

Most of my page works, but when I try to access part of my site I get a 502 Bad Gateway error.

I am running the latest version of Laravel, nginx and php5-fpm. My server is an AWS Ubuntu 14.04 instance.

I check the nginx log and get the following error

2016/07/01 19:06:29 [error] 1101#0: *8 recv() failed (104: Connection reset     by peer) while reading response header from upstream, client: [client IP here], server: [aws server IP here], request: "GET /get/request/here/build?active=talent HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "[server IP here]", referrer: "http://[server IP here]/admin?all=yes"

Here is my fpm/pool.d/www.conf file (everything that is not coded out more or less)

; Pool name
[www]

listen.owner = www-data
listen.group = www-data
;listen.mode = 0660

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
;pm.process_idle_timeout = 10s;
; pm.max_requests = 500

chdir = /
catch_workers_output = yes

Here is my nginx/sites-available/default file:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/laravelproject/public;
    index index.php index.html index.htm;

    server_name [server-ip-here];

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

    location ~ \.php$ {
        try_files $uri /index.php =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;
    }
}

How can I fix this?

ruby_noobie
  • 205
  • 4
  • 16

2 Answers2

2

Verify where your fastcgi_params seats

location ~ \.php$ {
    set $php_root /var/www/laravelproject/public;
    fastcgi_pass unix:/var/run/php5-fpm.sock; // switch back when verified 
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params; // adjust to your absolute path
}
dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • So when I swapped to the port it didn't work at all, got the bad gateway from a page that was working before. – ruby_noobie Jul 01 '16 at 19:53
  • 1
    Looks like it needs your fastcgi_params - to be set in the existing location, now you can check the log and see. – dmitryro Jul 01 '16 at 19:56
  • 1
    Also, try commenting out line listen [::]:80 default_server ipv6only=on; to and make sure only one server uses port 80 - you can see from ps -ef or ps aux what's going on with processes – dmitryro Jul 01 '16 at 19:58
  • Here is the new error message `2016/07/01 19:55:29 [error] 1672#0: *10 FastCGI sent in stderr: "PHP message: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1024 bytes) in /var/www/laravelproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 303" while reading response header from upstream, client: [client-ip-here], server: [server-ip], request$` – ruby_noobie Jul 01 '16 at 20:03
  • Commented out the second server line and still no luck. Thanks for the suggestions. – ruby_noobie Jul 01 '16 at 20:07
  • 1
    So now we can modify php.ini and set ini_set('memory_limit', '-1') – dmitryro Jul 01 '16 at 20:26
  • 1
    which is either /etc/php/5.6/apache2/php.ini or /etc/php/5.6/cli/php.ini - just run find / -name php.ini – dmitryro Jul 01 '16 at 20:27
  • I was hopeful about this one, but it was a no go as well. Still getting the gateway error. Going to leave that memory_limit as -1 just in case though. – ruby_noobie Jul 01 '16 at 20:36
  • https://serversforhackers.com/video/php-fpm-configuration-the-listen-directive - this may give some more insight. – dmitryro Jul 01 '16 at 20:45
  • try to enable swap. I had such issues on small droplet on digitalocean. memory limit did not helped, cuz really server had no free memory, so enabled swap to have some unnecessary memory blocks to be swapped out of ram. – num8er Jul 01 '16 at 21:03
1

I don't see issue in nginx config.

But about www.conf I see that You've defined listener but not defined listener socket.

So try this:

[www]

user = www-data
group = www-data

listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0666

pm = ondemand
pm.max_children = 4
pm.process_idle_timeout = 10s
pm.max_requests = 32
chdir = /

php_admin_flag[display_errors] = on
php_admin_flag[log_errors] = off
php_admin_value[memory_limit] = 512M
php_admin_value[post_max_size] = 128M
php_admin_value[upload_max_filesize] = 128M
num8er
  • 18,604
  • 3
  • 43
  • 57