Nginx:Nginx1.12.2php
PHP:PHP 7.2mysql
系統:CentOS 7.4nginx
Nginx與PHP都是yum安裝的,具體步驟:web
rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
~]# yum install -y nginx
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y yum install php72w-common php72w-fpm php72w-opcache php72w-gd php72w-mysqlnd php72w-mbstring php72w-pecl-redis php72w-pecl-memcached php72w-devel
location ~ .php$ { #匹配規則 fastcgi_pass 127.0.0.1:9000; #交由fastcgi(php-fpm)處理 fastcgi_index index.php; include fastcgi_params; }
在加完這個配置後,自動Nginx和php-fpm發現都沒有報錯,在網頁根目錄建立index.php文件redis
<?php phpinfo(); ?>
訪問時確出現了問題,狀態碼爲200,但沒有頁面顯示。sql
將Nginx配置修改後:php7
location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #加了這一項 include fastcgi_params; }
重啓Nginx和php-fpm後便能正常顯示php頁面了。後來將 include fastcgi_params; 這一項刪掉保留上一項,發現php網頁也顯示空白頁。memcached
在查閱資料後大體緣由是:在Nginx+php-fpm的組合中,Nginx將php交由php-fpm處理時須要使用 fastcgi_params 中的內建參數來解析處理請求。php-fpm
安裝完成Nginx後會自動生成 fastcgi_params 文件。網站
查看Nginx中 fastcgi_params 的具體參數:
[root@localhost ~]# cat /etc/nginx/fastcgi_params 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;
原來在yum安裝Nginx後默認配置中並無 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 這一項,而這一項的意思是指腳本文件請求的路徑,也就是說當訪問127.0.0.1/index.php的時候,須要讀取網站根目錄下面的index.php文件,若是沒有配置這一配置項時,nginx不回去網站根目錄下訪問.php文件,因此返回空白
因此若默認 fastcgi_params 中如有 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 參數,則在Nginx配置中只須要加 include fastcgi_params; 這一項便可;若默認配置中沒有則能夠單獨加上。