Nginx配置php-fpm 逐行解读

实验环境:

  • ubuntu18.04
  • nginx/1.14.0(Ubuntu)
  • php7.2-fpm

我的配置示例

涉及到fastcgi的配置主要在localhost上下文

location ~ \.php$ {
	fastcgi_pass 127.0.0.1:9000;
	# fastcgi_pass unix:/run/php/php7.2-fpm.sock; #如果php-fpm监测的是unix套接字,则需要设置为套接字。
	fastcgi_index index.php;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	# 引入配置文件
	include fastcgi_params;
}

逐行解读

  1. location ~ \.php$ {
    • ~:区分大小写匹配
    • 整行意思:匹配以‘.php’结尾的请求,区分大小写
  2. fastcgi_pass 127.0.0.1:9000;
    • fastcgi_pass:设置 FastCGI 服务器的地址。
  3. fastcgi_index index.php;
    • fastcgi_index:设置一个文件名,该文件名将附加在以斜杠结尾的 URI 之后,在$fastcgi_script_name变量的值中。(例:当url=‘http:127.0.0.1/public/’时,在其后自动补充index.php。会影响$fastcgi_script_name变量)
  4. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    url访问例子:http://127.0.0.1/biosmanage/public/index.php/index/index/index
    • fastcgi_param:向fastcgi(php7.2-fpm)传递请求参数,php的$_SERVER中的参数与此相关。
    • SCRIPT_FILENAME:当前执行脚本的绝对路径。(即要执行的php文件的绝对路径)
    • $document_root
      This variable is equal to the value of directive root for the current request;
      此变量等于当前请求的指令根的值;
      如上url例时 $document_root=biosmanage/public/index.php,所以此行也可以替换成:
# 我的root为/var/www/html/
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
  • $fastcgi_script_name
    request URI or, if a URI ends with a slash, request URI with an index file name configured by the fastcgi_index directive appended to it. This variable can be used to set the SCRIPT_FILENAME and PATH_TRANSLATED parameters that determine the script name in PHP.
    一句话:相对服务器根目录的想要执行的php文件的路径。
    如上url例时 :$fastcgi_script_name=biosmanage/public/index.php
  1. include fastcgi_params;
    引入fastcgi_params内的配置
    在ubuntu下Nginx存在一个文件叫‘fastcgi_params’,内有默认配置,如下:
fastcgi_param  QUERY_STRING       $query_string; # query string(查询字符串),如果有的话,通过它进行页面访问。参数SCRIPT_FILENAME 是PHP 用来确定执行脚本的名字,而参数QUERY_STRING 是它的一个子参数。也就是url后面的?name=xiaoqiang的参数这部分。
fastcgi_param  REQUEST_METHOD     $request_method; # 请求类型(get、post等)
fastcgi_param  CONTENT_TYPE       $content_type; # 请求头中的Content-Type
fastcgi_param  CONTENT_LENGTH     $content_length; # 请求头中的Content-length

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name; # 相对服务器根目录的想要执行的php文件的路径(具体参考第4步中的解释)
fastcgi_param  REQUEST_URI        $request_uri; # 此变量等于完整的初始URI和参数
fastcgi_param  DOCUMENT_URI       $document_uri; # 此变量等于请求中的当前URI,它可以与初始URI不同,例如通过内部重定向,或者通过使用索引,它是带有内部重定向的文件
fastcgi_param  DOCUMENT_ROOT      $document_root; # 此变量等于当前请求的指令根的值(即网站root路径)
fastcgi_param  SERVER_PROTOCOL    $server_protocol; # 这个变量等于请求的协议(HTTP/1.0或HTTP/1.1)
fastcgi_param  REQUEST_SCHEME     $scheme; # HTTP方案(即HTTP、https)
fastcgi_param  HTTPS              $https if_not_empty; # 只有当$https不为空时才传递该值。
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1; # 服务器使用的 CGI 规范的版本
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version; # 服务器标识字符串

fastcgi_param  REMOTE_ADDR        $remote_addr;#客户端IP
fastcgi_param  REMOTE_PORT        $remote_port;#客户端端口
fastcgi_param  SERVER_ADDR        $server_addr;#服务器IP地址
fastcgi_param  SERVER_PORT        $server_port;#服务器端口
fastcgi_param  SERVER_NAME        $server_name;# 服务器名。在server上下文内设置的server_name的值

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200; # 重定向状态

参考文档:
$_SERVER
模块 ngx_http_fastcgi_module
http核心模块

有钱的打个赏,没钱的尽量打个赏。。。吧

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值