Nginx + PHP-FPM Randomly Gives 502

My site is working fine with Nginx + PHP-FPM but randomly it gives 502 error. Environment Details

OS - CetnOS 6 Nginx PHP-FPM (php 5.4) APC (Code Cache APC 3.1.13 beta) Memcache (data cache)

In php-fpm

pm.max_children = 200 pm.start_servers = 40 pm.min_spare_servers = 30 pm.max_spare_servers = 50 pm.max_requests = 500

Also i am using TCP connection not socket.

If any body has any input please update me.


-------------Problems Reply------------

Firstly reduce pm.max_children = 200 to pm.max_children = 50.

You will have to firstly increase the file limit of the system thereby allowing nginx and php-fpm to open a larger number of files. File limit has to be increased as in linux everything in the end is a file. So the more connections you open the more number of files will be required. In ubuntu the file limit configuration are done in /etc/security/limits.conf. You will need to locate this for CentOS.

Then try to increase the internal port range which can be used by php-fpm. Along with this tcp ports are generally associated with a timeout value before they are reused, reduce this timeout so that more ports are freed when their job is done.

Find detailed info here.


In case if the error still persists try increasing the number of php-fpm worker processes to 100. Although it is not recommended to set the value so high as they consume addition memory.

pm.max_children = 100
pm.start_servers = 90
pm.min_spare_servers = 70
pm.max_spare_servers = 100

You can try out various values to get the optimum suited for your purpose.

The basic reason for a 502 is when nginx cannot forward or fails to forward the request to php-fpm. Increasing the number of php-fpm worker processes can be one of the way, thereby giving nginx more processes to forward the requests to.

Category:php Views:28 Time:2018-12-14
Tags: php apc nginx

Related post

  • Nginx + PHP-FPM 502 Bad Gateway 2010-09-01

    I am getting a 502 Bad Gateway from Nginx on a line of PHP code that is working fine in other places of my program ($this->provider = new OAuthProvider();), and that have worked fine before. This is the message I get in the Nginx error log for eac

  • Curl to HTTPS stop working with Nginx + php-fpm 2014-08-20

    When I try to do a curl with https in nginx+php-fpm I get the next error in the php-fpm.log WARNING: [pool www] child 661 exited on signal 11 (SIGSEGV) after 64.104500 seconds from start [20-Aug-2014 00:09:43] NOTICE: [pool www] child 670 started Als

  • How to setup nginx + php-fpm + wordpress for only certain URLs? 2009-08-08

    I'm trying to get nginx + php-fpm +wordpress to serve some pages from my site while acting as a proxy to other pages. ie: host/foo -> proxy to another www server host/bar -> wordpress (via fpm) the proxy part works fine, but the nginx -> wor

  • Scaling Nginx, PHP-FPM and MongoDB 2011-05-31

    I am looking for the best way to scale a PHP application under Nginx using PHP-FPM. I am looking at a concurrency of about 1200. Currently, anything over 400 starts to get slow response times. Response size is generally very small, but a few may be f

  • Optimize Nginx + PHP-FPM for faster response times (for Openx adserving) 2010-02-16

    I'm currently running Nginx + PHP-FPM for serving ads on OpenX. Currently my response times are horrible, even during times of low load. However, my CPU and Memory resources are fine, so I can't seem to figure out what the bottleneck is. My current c

  • With nginx/php-fpm, location header sometimes ignored by browser. Why? 2010-11-11

    I'm in the process of moving a working apache/mod-php website to nginx/php-fpm. Under Apache/mod-php, I could use header("Location: $url"); to redirect the browser to a different page, such as after a login attempt. After switching to nginx/php-fpm,

  • Setting $_ENV (fka $HTTP_ENV_VARS) with nginx/php-fpm 2011-12-18

    What is the equivalent of setenv in a apache environment? With apache I am able to for example set the env "SOMEENV" and access it in php via $_ENV['SOMEENV'] - but I have no idea how to do it with nginx+php-fpm. I initially thought that I just have

  • Nginx + Php-fpm + Zend Framework 1.11 strange rewrite error 2012-01-04

    I have strange rewrite problem with nginx & php5-fpm on an *nix platform. I'm using a Router.php class as app resource and there are lot of routes in this file which generates user-friendly uri's for corresponding modules & controllers. Now,

  • 502 Gateway Errors under High Load (nginx/php-fpm) 2012-01-07

    I work for a rather busy internet site that is often gets very large spikes of traffic. During these spikes hundreds of pages per second are requested and this produces random 502 gateway errors. Now we run Nginx (1.0.10) and PHP-FPM on a machine wit

  • Codeigniter + Nginx + php-fpm = 502 error 2011-11-19

    i’ve tried several configuration instructions and forum posts but nothing worked for me, I still get 502-bad gateway errors when I try to access my website. $config[“index_page”] = “”; ... $config[“uri_protocol”] = “REQUEST_URI”; // also tried AUTO M

  • How to set up SVN with nginx+php-fpm? 2011-10-19

    I have a working installation of Nginx and PHP-FPM, which its default index directory is /usr/share/nginx/html/ Also, I have a working installation of SVN, it seems the default directory of this is /var/www/ I tried to change in nginx.conf the defaul

  • CodeIgniter + Nginx + PHP-FPM on Mac OS X Lion 2012-02-13

    So I have installed Nginx and PHP-FPM via Homebrew, but to NO avail have I been able to actually get my local system up and running. I keep receiving the Nginx 502 Bad Gateway error. Here is my Nginx.conf user nobody; worker_processes 1; error_log /v

  • Experimental set-up with nginx, php-fpm and wordpress in arch linux - getting 403 2012-03-03

    Here's my nginx configuration:- user http; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $bod

  • Temporary value (local value) with nginx php FPM 2011-07-17

    I have search a lot but not answers found. i won't to have a lot of pool of PHP FPM, i want it to be like mod_php, and in nginx i wrote : fastcgi_param PHP_VALUE "open_basedir = /var/xxx/"; It works, but it declare the open_basedir for entiere life o

  • ApacheBench Results from another system on the network for nginx, php-fpm 2012-03-08

    I have a rather weird problem and given that I'm pretty new to all this, it might just be a duh moment. Looking for suggestions on what I might be doing wrong - Background - I am using nginx and php-fpm for serving my Zend/Yaf application. I wrote a

  • EC2, Nginx, PHP FPM : Down after 10 minutes up 2015-01-11

    I'm running a ec2 server (micro instance) with 2 wordpress on it. They were running well months long but since 2 days (after a yum update) PHP-FPM goes down after 10-15 minutes each time I restart it. Here are my config files /etc/php-fpm.d/www.conf

  • nginx + php-fpm ignores no-cache headers 2010-09-21

    I'm using the following header on a php page. // Prevent page caching. header('Expires: Tue, 20 Oct 1981 05:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header('

  • Two interfering cakePHP-Installations on nginx/php-fpm server 2011-12-03

    I needed to setup two completely separated cakePHP-Environments on my nginx-server (nginx 1.0.5, php5-fpm via fastcgi). I created the following folder-structure: /var/www /**virtual1**/ /app /lib/Cake/.. /**virtual2**/ /app /lib/Cake/.. Now, when I c

  • remove php file extension with nginx/php-fpm 2012-01-23

    maybe someone can help me with this problem. I want to remove the php extension. (i want to use grabaperch with nginx). This is the apache rewrite rule: RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php RewriteRule (.

  • problems with kohana and nginx / php-fpm 2011-09-07

    I'm having issues getting kohana to come up at all. I'm somewhat new to nginx and it's configurations. It's a CentOS 5.6 box. The configuration file: I can get phpinfo() to return correctly or other static files. It appears

  • Getting nginx/php-fpm to fail gracefully if uploaded file exceeds limits 2011-05-05

    At the moment, if I try to upload a file that takes too long or exceeds the maximum file size, the browser just reports that the connection has timed out. How do I get this to fail gracefully without relying on JavaScript, preferably by passing the e

  • PHP-FPM and Nginx: 502 Bad Gateway 2012-04-04

    Configuration Ubuntu Server 11.10 64 bit Amazon AWS, Ec2, hosted on the cloud t1.micro instance Before I write anything else, I'd like to state that I've checked both nginx 502 bad gateway and Nginx + PHP-FPM 502 Bad Gatway threads, which unfortunate

  • NGINX compile and gzip config (with rails 3 + php-fpm): "can't unzip" by torrent client from php tracker 2011-02-09

    Hi there: I have got a weird question about a new server installed with Nginx+php-fpm+passenger. Everything is the latest version. I have got everything configured well, all the web pages are running, however when the users tried to access the php tr

  • Nginx load balance with dedicated php-fpm server 2011-03-16

    I got server setup with nginx+php-fpm and mysql. I have another server with only installed php-fpm, so wanted to use as load balance. But when I am using this dedacted server with php-fpm as load balancer, I got error when opening page: "Access denie

  • Using git post-receive hook with nginx and php-fpm environment 2012-03-01

    I have a CentOS 6 server set up with git, gitosis, nginx, and php-fpm. With our setup, nginx executes php scripts via php-fpm, which is configured on a per-site basis to run as a specific user for security purposes (i.e. not all under nginx:nginx) si

Copyright (C), All Rights Reserved.

processed in 0.291 (s). 11 q(s)