Nginx Call Remote PHP-FPM

Source: Internet
Author: User
Tags fpm nginx server

In the case of Nginx server, when we enter http://localhost:8080/index.php carriage return

The browser will send the request to Nginx,nginx based on the PHP files we have configured to end in. php to FastCGI

  Location ~ \.php {            root           /nginx;            Fastcgi_pass   127.0. 0.1:9000;                      fastcgi_index  index.php    ;       ^ (. +\.php) (. *) $;     Fastcgi_param path_info $fastcgi _path_info;                      fastcgi_param  script_filename  /scripts$fastcgi_script_name;          /usr/local/etc/nginx/fastcgi.conf;                }

The request is forwarded to fastcgi, namely PHP-FPM, through the Fastcgi_pass command;

PHP-FPM is an implementation of fastcgi.

Generally we will forward the received request to local, that is, the local port that php-fpm listens to 127.0.0.1:9000

If we want to implement distributed, forward our request to the remote PHP-FPM to deal with how to implement it?

1. Modify the local nginx fastcgi_pass monitor IP: Port

Fastcgi_pass 127.0.0.1:9000 modified to x.x.x.x:9000

Where x.x.x.x represents the IP address of the remote host

2. Modify the remote PHP-FPM's listening IP: port

The remote PHP-FPM is listening to the IP: port, changed from 127.0.0.1:9000 to: 0.0.0.0:9000

   

127.0.0.1 represents the Machine

0.0.0.0 represents all local addresses, which is equivalent to a collection: all unclear hosts and destination networks. The "unclear" here means that there is no specific entry in the local routing table to indicate how to get there.

If we do not modify the remote PHP-FPM listening IP: port is 0.0.0.0:9000

Local direct access to http://localhost:8081/index.php Nginx will error appears (where x.x.x.x represents my remote host address)

In general we php-fpm not start the time will throw this error, but our remote PHP-FPM is already open, so can not connect to the remote PHP-FPM listening address.

* Kevent () reported that connect () failed (a: Connection refused)
while 127.0. 0.1 " get/index.php http/1.1 "
" fastcgi://x.x.x.x:9000 " " localhost:8081 "

Next we will php-fpm listening IP: port to: 0.0.0.0:9000; reboot php-fpm, then we continue to visit http://localhost:8081/index.php

This time the browser appears

Can't find the file? Why is it? I then analyzed the Nginx configuration (where x.x.x.x represents my remote host address)

  Location ~ \.php {            root           /nginx;            Fastcgi_pass   x. x. x:9000;                     Fastcgi_index  index.php;       ^ (. +\.php) (. *) $;     Fastcgi_param path_info $fastcgi _path_info;                Fastcgi_param  Script_filename      /scripts$fastcgi_script_name ;            /usr/local/etc/nginx/fastcgi.conf;                }

Pay particular attention to the "Fastcgi_script_name" line (in bold) and php-cgi specifically need this line of information to determine the location of the PHP file. That is, when we forward the request to our fastcgi (i.e. PHP-FPM), the exact path of the requested file is also sent to fastcgi (i.e. PHP-FPM);

If my nginx local server root directory is/nginx, then the absolute path of the index.php I requested is/nginx/ Index.php,nginx when the request is forwarded to the remote PHP-FPM, the path will also be sent to the remote PHP-FPM, the remote PHP-FPM will be based on this path/nginx/ index.php find index.php and execute, return the result to the local nginx server, which means that our remote PHP-FPM host must exist the path is/nginx/ index.php file is only OK, otherwise it will report 404 error, the file does not exist (that is, we see the files not found.);

Next, create the Nginx in the root directory and create a new index.php inside the nginx.

CD//nginx build nginxcd nginxtouch index.php Create file
Vim index.php Edit input PHP code

When we edit the vim index.php, we enter the following code:

<? PHP         Echo ' Remote php-fpm ';? >

Visit http://localhost:8081/index.php again

The following results will appear:

Successful access.

Summary:

1: Remote PHP-FPM monitoring IP: Port changed to 0.0.0.0:9000 (port can be modified appropriately, default 9000)

2: Note the requested file path address to the root directory of the local server always, otherwise it will be reported 404

Nginx Call Remote PHP-FPM

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.