Precautions for migrating mod_php to PHP-FPM

Source: Internet
Author: User

Due to its unique advantages, PHP-FPM has gradually become the first choice for large-load websites at this stage. Recently, I was asked by my friends to migrate an older website from Apache + mod_php to nginx + PHP-FPM. In addition to the compatibility problems caused by PHP version upgrades, many compatibility problems come from the features of PHP-FPM. Here is a simple list of the problems encountered for your reference.

The first concern is the $ _ server ["http_referer"] environment variable. This problem often occurs on the page to jump to, and is displayed as jump to the whiteboard.

Many codes use $ _ server ["http_referer"] to obtain the URL of the last accessed page. For mod_php, because the method of embedding the HTTP server directly, PHP can easily obtain the http_referer variable. However, for PHP-FPM in CGI Mode, this environment variable cannot be obtained. My personal solution is to get it by refreshing a cookie. It looks clumsy, but the amount of code changes is very small. You may say this is easy to tamper with, but in fact, even the environment variables obtained by mod_php are easy to tamper.

Next is $ _ server ["HTTPS"]. This issue often occurs during redirection. This issue is most likely caused by exceptions in all redirection URLs.

In mod_php, the $ _ server ["HTTPS"] variable only appears after HTTPS is enabled. Otherwise, empty ($ _ server ["HTTPS"]) = true, the value of PHP-FPM always exists, but $ _ server ["HTTPS"] = NULL at http. Although $ _ server ["HTTPS"] = NULL is equivalent to empty ($ _ server ["HTTPS"]) = true, it is not true under the "thirds" condition.

The corresponding $ _ server variable issues include X-forward * and so on.

Then there is the rewrite rule, which is actually a problem of compatibility between APACHE and nginx and has nothing to do with PHP.

I personally think that the jump rules of Apache and nginx are basically common, even if you use NotePad to replace the full text. For example:

RewriteRule ^(.*.(css|js))$ min/index.php?f=$1 [L]

Convert to nginx

rewrite "^(.*.(css|js))$"              /min/index.php?f=$1     last;

Of course, nginx does not support. htaccess and needs to be written to the configuration file.

The execution time is incorrect. Most of the errors are 502 errors. To tell the truth, I don't have a good solution. I have to plan it in advance.

Unlike mod_php, When you execute a script for a long time, once the threshold value is exceeded (10 seconds by default), PHP-FPM automatically closes the execution and returns 502 to the front-end. I personally think this is a protective setting relative to mod_php, which is an advantage and should not be disabled.

Process control functions are rarely used.

Php-FPM uses a non-blocking thread pool. This type of function should not appear and should be used with caution.

TIPS: $ _ env variable

Compared with mod_php, the $ _ env variable is an advantage for enterprise development, especially for enterprises separated from development and O & M, this makes it easy to develop environment-independent code-O & M changes $ _ env variable. developers can read this variable without too much communication between them. Corresponds to the Env [temp] =/tmp or 5.3 <value name = "environment"> <value name = "tmp">/tmp </value> label in the 5.2 FPM configuration file

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.