在用虚拟机搭建环境的时候,出现访问php文件无法获取到。总是报错Primary script unknown
,但是静态文件是可以正常访问的。通过排查日志,检查个服务是否正常等方法终于找到解决的方法。
报错如下:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
详细报错如下:
2017/05/11 10:26:45 [error] 5475#5475: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.1.194, server: demo.demo.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
先nginx 网站配置文件*.conf中的修改下
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 修改为 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
一、检查所需的服务是否都正常。
service nginx status service php-fpm status netstat -antp
此处常规的排错,就一笔带过。
二、修改nginx网站的配置。
默认的情况下文件地址在:/etc/nginx/conf.d/default.conf
默认配置:
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
将其修改为:
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
因为默认情况下,php服务都是注释掉的,我们将其打开(去掉前面#即可),然后修改root
、fastcgi_param
这两个配置项。配置的root参数是我们网站文件放置的路径。
配置设置完成后,记得重新启动服务。
service nginx restart
通过上面的排查和配置,就可以解决nginx下php无法访问报错Primary script unknown
的问题。