Nginx and php-fpm: cannot get rid of 502 and 504 errors Nginx and php-fpm: cannot get rid of 502 and 504 errors nginx nginx

Nginx and php-fpm: cannot get rid of 502 and 504 errors


A number of issues. Still worth to fix them with such a busy site. MySQL may be the root cause for now. But longer term you need to do more work.

Caching

I see one of your error msg showing a get request to the php upstream. This doesn't look good with such a high traffic site (2000 r/s as you mentioned). This page (/readarticle/121430) seems a perfectly cacheable page. For one, you can use nginx for caching such pages. Check out fastcgi cache

GET /readarticle/121430

php-fpm

pm.max_requests = 100

The value means that a process will be killed by php-fpm master after serving 100 requests. php-fpm uses that value to fight against 3rd party memory leaks. Your site is very busy, with 2000r/s. Your max child processes is 130, each can only serve at most 100 requests. That means after 13000/2000 = 6.5 seconds all of them are going to be recycled. This is way too much (20 processes killed every second). You should at least start with a value of 1000 and increase that number as long as you don't see memory leak. Someone uses 10,000 in production.

nginx.conf

  • Issue 1:

        if (!-e $request_filename) {        rewrite ^(.*)$ /index.php?path=$1 last;    }

    should be replaced by more efficient try_files:

        try_files $uri /index.php?path=$uri;

You save an extra if location block and a regex rewrite rule match.

  • Issue 2: using unix socket will save you more time than using ip (around 10-20% from my experience). That's why php-fpm is using it as default.

  • Issue 3: You may be interested in setting up keepalive connections between nginx and php-fpm. An example is given here in nginx official site.


I need to see your php.ini settings and I don't think this is related to MySQL since you're getting socket errors it looks like. Also, is this something that begins to start happening after a period of time or does it immediately happen when the server restarts?

Try restarting the php5-fpm daemon and see what happens while tailing your error log.

Check your php.ini file and also all your fastcgi_params typically located in /etc/nginx/fastcgi_params. There are a ton of examples for what you're trying to do.

Also, do you have the apc php caching extension enabled?

It will look like this in your php.ini file if your on a lamp stack:

extension=apc.so
....
apc.enabled=0

Probably wouldn't hurt to do some mysql connection load testing from the command line as well and see what the results are.


Setting up nginx microcache would help as well.Which will serve the same response for a few seconds.

http://seravo.fi/2013/optimizing-web-server-performance-with-nginx-and-phphas some good info on nginx performance.Personally followed that and i'm quite happy.