php 报错 无法访问,nginx下php无法访问报错Primary script unknown的解决方法

在用虚拟机搭建环境的时候,出现访问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的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值