我想在我的 MAC 上配置新的 nginx,但总是出现[error] 200#0: *79 FastCGI sent in stderr: "Primary script unknown"
.. 我不知道我在配置上做错了什么。这是我的 nginx.conf 文件:
#user RobDee;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format #main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log;
error_log logs/error.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name local.mydomain.co.uk local.beer.telegraph.co.uk;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /Users/RobDee/workspace/beer/web;
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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
server {
listen 443 ;
server_name local.mydomain.co.uk local.beer.telegraph.co.uk;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate /usr/local/etc/nginx/cert.pem;
ssl_certificate_key /usr/local/etc/nginx/cert.key;
gzip_disable "msie6";
gzip_types text/plain application/xml application/x-javascript text/css application/json text/javascript;
access_log /usr/local/var/log/nginx/access.log;
error_log /usr/local/var/log/nginx/error.log;
log_not_found off;
root /Users/RobDee/workspace/beer/web;
location /.htpasswd
{
return 403;
}
location ~ \.css {
root /Users/RobDee/workspace/beer/web;
expires max;
}
location ~* \.(jpg|jpeg|png|gif|ico|js|woff|woff2|ttf)$ {
root /Users/RobDee/workspace/beer/web;
access_log off;
expires max;
}
location ~* \.(js|css)$ {
expires 1y;
log_not_found off;
}
location /
{
root /Users/RobDee/workspace/beer/web;
try_files $uri $uri/ /app_dev.php$is_args$args;
index app_dev.php;
}
location ~ \.php$ {
root /Users/RobDee/workspace/beer/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /Users/RobDee/workspace/beer/web/app_dev.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 3000;
}
}
include servers/*;
}
也许有人会在我的配置文件中看到一些错误。
答案1
“主要脚本未知”几乎总是意味着值SCRIPT_FILENAME
是错误的。
问题可能出在这三行:
root /Users/RobDee/workspace/beer/web;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
您设置$document_root
,然后将其硬连线为/scripts
。它是哪一个?
在普通nginx
服务器块中,您将在root
服务器块顶部附近设置一次,并允许所有(或大多数)location
块继承相同的值。仅在必要时覆盖。请参阅这个文件了解详情。
通常的格式SCRIPT_FILENAME
如下:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
在您的配置中,这相当于相同的值。请注意,这$document_root
是由指令的值定义的root
。
最后,包含fastcgi_params
last 可能没问题,也可能不行。在某些版本中,您明确定义的语句fastcgi_params
的定义可能会有冲突。您应该始终在明确语句之前包含该文件。fastcgi_param
fastcgi_param
例如:
server {
...
root /Users/RobDee/workspace/beer/web;
...
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 3000;
}
}
该fastcgi_index
指令在这种情况下毫无意义。