nginx 配置
root /var/www/php; // 新版本 root 要放在这里次啊能起作用
location ~\.php$ {
root /var/www/php;
index index.php index.htmlindex.htm;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
服务器 安装 php-fpm
然后 nginx 通过unix:/run/php/php7.4-fpm.sock 转发请求到php-fpm上
tips : fastcgi_params 添加以下配置(若访问首页空白)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
//从nginx变量 传到 cgi变量(即php变量)
fastcgi_param 参数详解 就是从nginx传过去的变量 可以通过_SERVER 获取
关键变量是
DOCUMENT_ROOT php 项目文件所在文件夹
SCRIPT_NAME php 项目文件
SCRIPT_FILENAME $document_root$fastcgi_script_name; //php 文件全路径
nginx变量 都是$开头小写下划线组合
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
以上参数可以通过 php $_SERVER[‘SERVER_NAME’] 获取变量值