0

I'm trying to use nginx as a webserver for my owncloud installation. According to this tutorial, I created the following config file:

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;
        }
}

But when I visit the website http://mydomain.de:59476 I only get the error Message "File not found.". Since I set the debug level to "debug", I get the following extensive log file http://pastebin.com/uy7jHQQs. I think the most important line is

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"

Which, as I understood nginx, means that the file that nginx tries to execute does not exist. But the log also said:

2013/08/03 17:23:21 [debug] 29508#0: *3 fastcgi param: "SCRIPT_FILENAME: /home/owncloud/www/index.php"

and the file /home/owncloud/www/index.php does exist.

To check if nginx alone works correctly, I set up another website without owncloud and everything works perfectly (including PHP support) with the following configuration:

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;
        }
}

and the the following 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;
born
  • 101
  • 1
  • 5
  • I totally forgot to append an error from the nginx logfile: FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 37.24.145.156, server: mydomain.de, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "mydomain.de:59476" – born Aug 15 '13 at 15:03

2 Answers2

0

Does nginx user have te right to read from "/home/owncloud/www/"? And does it have the right rights to /var/run/php5-fpm.sock?

to check try:

sudo -u nginx ls /var/run/php5-fpm.sock

and

sudo -u nginx ls /home/owncloud/www/index.php

and use ps aux | grep nginx to check if it really runs as "nginx"

Marco
  • 287
  • 1
  • 2
  • 12
  • ls -l /var/run/php5-fpm.sock yields srw-rw-rw- 1 root root 0 Jul 21 19:08 /var/run/php5-fpm.sock so everyone has write permission for the socket. – born Aug 04 '13 at 05:41
  • ls -l /home/owncloud/www yields lrwxrwxrwx 1 owncloud owncloud 14 Jul 21 18:44 /home/owncloud/www -> owncloud-5.0.9 so owncloud has read permission for the www-directory. – born Aug 04 '13 at 05:42
  • and finally ps aux | grep nginx says that nginx runs as the user owncloud (at least the worker processes. I configured this too. Should the master process also run as owncloud? That whould be weird or not? – born Aug 04 '13 at 05:43
  • service iptables stop && setenforce 0 ----- then retry – Marco Aug 04 '13 at 16:51
  • -- edit: also include fastcgi_params; seems to be different in your working example, try swapping it – Marco Aug 04 '13 at 17:02
  • Could you accept my answer? Thank you too. – Marco Aug 15 '13 at 12:41
  • I followed your advice and swapped the location part concerning php-files and still doesn't work. – born Aug 15 '13 at 15:02
  • Maybe my comment on Aug 11 was a bit unclear: It does not work yet. – born Aug 15 '13 at 18:28
0

After a lot of digging it turned out that neither nginx nor owncloud where the cause of the "File not found". I found this by stopping the php5-fpm service which changed the message to a 502 Bad Gateway from nginx.

The corresponding child processes of nginx ran as the user owncloud (not as www-data) but the corresponding child processes of php5-fpm ran as www-data. Adding a new pool with a new socket that uses the user owncloud solved the problem. The message was "File not found" instead of access denied because the user was not allowed to read the directory.

If you encounter the same reason for this problem, make sure that nginx accesses the right socket.

@Marco: Thank you for all your time though.

born
  • 101
  • 1
  • 5