Go Nginx 502 PHP LNMP 502 Ultimate Solution perfect solution 502 with upstream and Fastcgi_next_upstream can greatly alleviate

Source: Internet
Author: User

Ext.: http://xn--ghqyhzj.com/post-21537.html

This article is for LNMP PHP version ver 5.3.6 or higher, others have not been tested.

1. Start multiple PHP-FPM master processes using different ports or Php-fpm.sock

Suppose you use a different configuration file to launch 3 PHP-FPM master processes that create sock snooping


#/usr/local/php/sbin/php-fpm--fpm-config/usr/local/php/etc/php-fpm.1.conf
#/usr/local/php/sbin/php-fpm--fpm-config/usr/local/php/etc/php-fpm.2.conf
#/usr/local/php/sbin/php-fpm--fpm-config/usr/local/php/etc/php-fpm.3.conf

Of the 3 profile *.conf, the only difference is to set the PID file and sock name, assuming Php-cgi.1.sock php-cgi.2.sock php-cgi.3.sock (You can also set a different pool name, by default [www])

Then after the boot, in the corresponding directory (typically/tmp/) appears Php-cgi.1.sock Php-cgi.2.sock php-cgi.3.sock 3 sock, I am in the default/TMP.

If you set all normal, ps auf can be seen that there are 3 main processes running.

2. Modify Nginx configuration file to execute PHP program using process pool mode

The HTTP segment in nginx.conf adds the following code according to the actual PHP-FPM process situation:


Upstream unix__tmp_php_cgi_sock{
Server Unix:/tmp/php-cgi.1.sock;
Server Unix:/tmp/php-cgi.2.sock;
Server Unix:/tmp/php-cgi.3.sock;
}
Fastcgi_next_upstream error timeout Invalid_header http_503;

This allows us to set up a pool of resources that can be polled based on health conditions and can be retried.

3. And then what?

In the original code that needs to execute the PHP program, the original code example:


Location ~ \.php$ {
Include Fastcgi_params;
Fastcgi_pass Unix:/tmp/php-cgi.sock;
}

Or it turns out that


Location ~ \.php$ {
Include Fastcgi_params;
Fastcgi_pass 127.0.0.1:9000;
}

Modify the new, as follows:


Location ~ \.php$ {
Include Fastcgi_params;
Fastcgi_pass Unix__tmp_php_cgi_sock;
}

Restart Nginx, effective.

4. Precautions

In step 1 to start the various PHP-FPM main process, as long as the dead, Nginx can normally execute PHP, that is, if some abnormal exit, the basic does not affect the site to run.
Step 2 fastcgi_next_upstream that row of parameters, do not need to add http_502, the actual you also add not up, do not support!
Step 3, the original each paragraph similar to the location ~ \.php$ {Code will need to fastcgi_pass this line according to the example of the transformation.
the. conf configuration file in step 1, the children of each main thread, is set 2~n according to the memory situation, and it is said that the more the better, the more memory is used.
After the adjustment of this program, please note that your original so-called service PHP-FPM and 502 monitoring scripts and other things, you need to re-modify, if you want to use. Of course, for the technical house, how to operate the program that is at ease, digestion and absorption.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.