容器关系如下。
如请求 http://mydomain.com/test.php
Nginx 收到该请求的流程步骤如下
-
请求到达 Nginx:请求首先到达 Nginx 服务。
-
Nginx 处理静态文件:对于静态文件(如 HTML、CSS、JS 和图片等),Nginx 会在自己的 容器内的
/var/www/html
目录中查找并直接返回给用户。 -
Nginx 转发 PHP 请求:对于 PHP 文件,Nginx 将根据配置中的
location ~ \.php$
块,使用fastcgi_pass
指令将请求转发给 PHP-FPM 服务。 -
Nginx 设置 FastCGI 参数:在转发请求之前,Nginx 设置了一系列 FastCGI 参数,包括:
SCRIPT_FILENAME
:设置为 Nginx 的$document_root$fastcgi_script_name
,即/var/www/html/test.php
。PATH_INFO
:如果请求包含额外的路径信息,如http://mydomain.com/test.php/some/path
,则$fastcgi_path_info
将是/some/path
。
-
PHP-FPM 执行脚本:PHP-FPM 接收到 Nginx 转发的请求后,会根据提供的
SCRIPT_FILENAME
执行相应的 PHP 脚本。 -
PHP-FPM 返回结果给 Nginx:执行完成后,PHP-FPM 将输出结果返回给 Nginx。
-
Nginx 返回结果给用户:Nginx 接收到 PHP-FPM 返回的结果后,将其发送给用户。
贴上nginx的配置
server {
listen 80;
server_name mydomain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
}