Skip to content

Instantly share code, notes, and snippets.

@petarpetrovic
Created March 14, 2013 17:56
Show Gist options
  • Save petarpetrovic/5163565 to your computer and use it in GitHub Desktop.
Save petarpetrovic/5163565 to your computer and use it in GitHub Desktop.
This is the proper config file for nginx and ownCloud version 5. Please note that slight variations might need to be applied in order for your particular instance to work. This one works for me, hence this Gist. Everyone is free to fork it and make proper modifications for different server configurations.
# This is the complete example of nginx configuration file for ownCloud 5
# This config file configures proper rewrite rules for the new release of ownCloud
# Also, this config file configures nginx to listen on both IPv4 and IPv6 addresses
# If you want it to listen to IPv4 address only, use listen 80; instead of listen [::]:80
# First, we configure redirection to HTTPS (substitue owncloud.example.com with the proper address of your OC instance)
server {
listen [::]:80;
server_name owncloud.example.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
# Now comes the main configuration for ownCloud 5
server {
listen [::]:443 ssl; # Make it listen on port 443 for SSL, on both IPv4 and IPv6 interfaces
server_name owncloud.example.com;
root /var/www/owncloud; # Make sure to insert proper path for your ownCloud root directory
index index.php;
# Now we configure SSL certificates. Make sure you enter correct path for your SSL cert files
ssl_certificate /etc/nginx/certs/owncloud.example.com.crt;
ssl_certificate_key /etc/nginx/certs/owncloud.example.com.key;
client_max_body_size 1024M; # This is the first parameter which configures max size of upload, more to come later
fastcgi_buffers 64 4K;
# Configure access & error logs
access_log /var/log/nginx/owncloud.example.com.access_log main;
error_log /var/log/nginx/owncloud.example.com.error_log info;
# Configure proper error pages
error_page 403 = /core/templates/403.php;
error_page 404 = /core/templates/404.php;
# Some rewrite rules, more to come later
rewrite ^/caldav((/|$).*)$ /remote.php/caldav$1 last;
rewrite ^/carddav((/|$).*)$ /remote.php/carddav$1 last;
rewrite ^/webdav((/|$).*)$ /remote.php/webdav$1 last;
# Protecting sensitive files from the evil outside world
location ~ ^/(data|config|\.ht|db_structure.xml|README) {
deny all;
}
# Configure the root location with proper rewrite rule
location / {
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 ^/apps/calendar/caldav.php /remote.php/caldav/ last;
rewrite ^/apps/contacts/carddav.php /remote.php/carddav/ last;
rewrite ^/apps/([^/]*)/(.*\.(css|php))$ /index.php?app=$1&getfile=$2 last;
rewrite ^(/core/doc[^\/]+/)$ $1/index.html;
index index.php; # This one might be redundant, but it doesn't hurt to leave it here
try_files $uri $uri/ index.php;
}
# Configure PHP-FPM stuff
location ~ ^(<script_name>.+?\.php)(?<path_info>/.*)?$ {
try_files $script_name = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock; # Be sure to check proper socket location for php-fpm, might be different on your system
fastcgi_param PATH_INFO $path_info;
fastcgi_param HTTPS on;
# This one is a little bit tricky, you need to pass all parameters in a single line, separating them with newline (\n)
fastcgi_param PHP_VALUE "upload_max_filesize = 1024M \n post_max_size = 1024M"; # This finishes the max upload size settings
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # On some systems OC will work without this setting, but it doesn't hurt to leave it here
include fastcgi_params;
}
location ~* ^.+.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
}
@Yacodo
Copy link

Yacodo commented Mar 20, 2013

You forget the ? before script_name on l.67, take me few minutes to figure it out.

@androos
Copy link

androos commented Mar 21, 2013

Why do you have no dav_methods in it?

@dattah
Copy link

dattah commented Mar 26, 2013

New to nginx and struggling to get OwnCloud 5.0 and nginx working. I have followed your posts and appear to be close to getting the configuration correct. Running on Ubuntu 12.04 LTS with PHP 5.4.12-2.

With the configuration below I have configured and can start Owncloud but get message this content failed to load.

server {
listen 80; ## listen for ipv4; this line is default and implied
server_name owncloud.zxzxzx.net;
index index.html index.htm index.php;
root /var/www/owncloud;
client_max_body_size 1000M; # set maximum upload size

    location ~ ^/owncloud/remote.php(/.*)$ {
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            }

              # deny direct access
              location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
                deny all;
              }

              # default try order
              location / {
                try_files $uri $uri/ @webdav;
              }

              # owncloud WebDAV
              location @webdav {
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include fastcgi_params;
              }

       location ~ .*\.(php|php5)?$
               {
                       try_files $uri =404;
                       fastcgi_pass unix:/var/run/php5-fpm.sock;
                       fastcgi_index index.php;
                       include fastcgi_params;
               }
        }

I updated the above configuration file to add the missing rewrite sections and then nginx will not restart. As I do not understand the rewrite rules could you please advise as to what is wrong with the following config.

Updated
server {
listen 80; ## listen for ipv4; this line is default and implied
server_name owncloud.zxzxzx.net;
index index.html index.htm index.php;
root /var/www/owncloud;
client_max_body_size 1000M; # set maximum upload size

    location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
                   deny all;
           }

           rewrite ^/caldav((/|$).*)$ /remote.php/caldav$1 last;
           rewrite ^/carddav((/|$).*)$ /remote.php/carddav$1 last;
           rewrite ^/webdav((/|$).*)$ /remote.php/webdav$1 last;

       location / {
            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 ~ ^/owncloud/remote.php(/.*)$ {
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            }

              # deny direct access
              location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
                deny all;
              }

              # default try order
              location / {
                try_files $uri $uri/ @webdav;
              }

              # owncloud WebDAV
              location @webdav {
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include fastcgi_params;
              }

       location ~ .*\.(php|php5)?$
               {
                       try_files $uri =404;
                       fastcgi_pass unix:/var/run/php5-fpm.sock;
                       fastcgi_index index.php;
                       include fastcgi_params;
               }
        }

Thanks for any assistance.

@Zer0t3ch
Copy link

I copied the config provided at this doc page which also doesn't have dav_methods in it, and I'm wondering how it works without having that there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment