我的家庭服务器上已成功运行 nginx。我没有指定 .com,因为这只是一次试运行,直到我弄清楚所有问题。我可以导航到服务器 IP 地址,index.html 可以正常使用。我尝试导航到 test.php,它告诉我“未指定输入文件”
这是我的 nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
server { # php/fastcgi
listen 80;
server_name localhost;
root /var/www;
location = /favicon.ico {
empty_gif;
# return 204;
}
location ~ \.php$ {
root /var/www;
fastcgi_pass 127.0.0.1:9000;
}
}
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
upstream php5 {
server unix:/tmp/php-fastcgi.sock;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# include /var/www/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
答案1
我通常的做法是创建一个“php.inc”文件,并将其包含在虚拟主机中。它包含所有必要的设置,可以告诉 Nginx 如何处理 PHP 文件(包括针对 Wordpress 等的 URL 重写)。
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
只需将其放入/etc/nginx
(您需要使用 sudo 通过命令行执行此操作)。然后,编辑您的配置以在您的块中添加include php.inc;
(它会自动知道在哪里查找) 。重新加载( )或重新启动()Nginx,您的 PHP 文件应该可以正常工作。server{}
sudo service nginx reload
sudo service nginx restart
编辑:以下是我在您的配置中注意到的其他几件事,以及我所做不同的事情。
我做的一件不同的事情是保留主 nginx.conf 文件,而是在 /etc/nginx/sites-available(带有指向 sites-enabled 的符号链接)或 /etc/nginx/conf.d 中声明虚拟主机。我发现这可以避免所有问题,但是,我通常必须随时管理多个虚拟主机域,并从一开始就考虑到这一点。
root /var/www;
我注意到您的配置中还有一件事,那就是块内有一个块location ~ \.php$ {}
。您不需要这样做(并且,根据我发现的这篇文章,可能会导致您的问题),因为服务器块内的位置块将根据需要继承。
结合这三种策略(将 vhost 配置放在 nginx.conf 之外,而是放在 conf.d 中,使用包含文件包含 PHP 内容,并允许位置块根据需要继承)应该可以实现干净的配置设置,而没有太多麻烦。
因此,您的 nginx.conf 文件应该如下所示(或者默认文件,如果您已备份的话):
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
然后,在 /etc/nginx/conf.d/default.conf 中添加一个条目,如下所示:
server {
listen 80;
server_name localhost;
root /var/www;
include php.inc;
}
答案2
我搞定了。我将 fastcgi_pass 127.0.0.1:9000 更改为 fastcgi_pass localhost:9000。我还更改了 php{} 块中注释掉的 root。我在 http{} 块中声明了 root。对于 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 已添加。
php.inc 可能有所帮助。我对它了解不多,无法给出任何结论。
谢谢 Shauna!!!!