我正在尝试使用 nginx 作为我的 owncloud 安装的 Web 服务器。根据本教程,我创建了以下配置文件:
server {
listen 59476;
server_name mydomain.de;
root /home/owncloud/www/;
access_log /home/owncloud/log/nginx.access.log;
error_log /home/owncloud/log/nginx.error.log debug;
client_max_body_size 10G; # set max upload size
fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php;
error_page 403 = /core/templates/403.php;
error_page 404 = /core/templates/404.php;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
location / {
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ index.php;
}
location ~ ^(.+?\.php)(/.*)?$ {
try_files $1 = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
fastcgi_param PATH_INFO $2;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# Optional: set long EXPIRES header on static assets
location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
# Optional: Don't log access to assets
access_log off;
}
}
但当我访问该网站时,http://mydomain.de:59476
我只收到错误消息“文件未找到。”。由于我将调试级别设置为“调试”,因此我得到了以下详尽的日志文件http://pastebin.com/uy7jHQQs我觉得最重要的一句话是
2013/08/03 17:23:21 [error] 29508#0: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 37.24.146.15, server: mydomain.de, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "mydomain.de:59476"
根据我对 nginx 的理解,这意味着 nginx 尝试执行的文件不存在。但日志还说:
2013/08/03 17:23:21 [debug] 29508#0: *3 fastcgi param: "SCRIPT_FILENAME: /home/owncloud/www/index.php"
并且该文件/home/owncloud/www/index.php
确实存在。
为了检查 nginx 单独是否正常工作,我建立了另一个没有 owncloud 的网站,一切都运行正常(包括 PHP 支持),配置如下:
server {
listen 59477;
server_name mydomain.net;
root /home/born/web/nginx;
index index.php;
# Logging --
access_log /home/myuser/logs/nginx.access.log;
error_log /home/myuser/logs/nginx.error.log notice;
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
以及以下 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_FILENAME $request_filename;
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 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;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
答案1
nginx 用户是否有权从“/home/owncloud/www/”读取?它是否有权读取/var/run/php5-fpm.sock?
检查尝试:
sudo -u nginx ls /var/run/php5-fpm.sock
和
sudo -u nginx ls /home/owncloud/www/index.php
并使用 ps aux | grep nginx 检查它是否真的以“nginx”身份运行
答案2
经过大量挖掘,结果发现 nginx 和 owncloud 都不是导致“文件未找到”的原因。我通过停止 php5-fpm 服务找到了这个问题,该服务将消息更改为来自 nginx 的 502 Bad Gateway。
nginx 的相应子进程以用户 owncloud 的身份运行(而不是以 www-data 的身份运行),但 php5-fpm 的相应子进程以 www-data 的身份运行。添加一个使用用户 owncloud 的新套接字的新池解决了该问题。消息是“文件未找到”,而不是拒绝访问,因为不允许用户读取目录。
如果您遇到同样原因导致的此问题,请确保 nginx 访问正确的套接字。
@Marco:感谢您抽出时间。