===========================================
Serving Static Files for Better Performance
===========================================

ownCloud can serve static files, which may improve performance.

.. note:: This feature can currently only be activated for local files, i.e. 
   files inside the **data/** directory and local mounts.  It also does not 
   work with the Encryption App enabled.
   Controlled file serving **does not work for generated zip files**. This is 
   due to zip files being generated and streamed back directly to the client.

Apache2 (X-Sendfile)
--------------------
It is possible to let Apache handle static file serving via `mod_xsendfile <https://tn123.org/mod_xsendfile/>`_.

Installation
~~~~~~~~~~~~
On Debian and Ubuntu systems use:

::

   apt-get install libapache2-mod-xsendfile


Configuration
~~~~~~~~~~~~~
Configuration of mod_xsendfile for ownCloud depends on its version.
For versions below 0.10 (Debian squeeze ships with 0.9)

::

    <Directory /var/www/owncloud>
        ...
        SetEnv MOD_X_SENDFILE_ENABLED 1
        XSendFile On
        XSendFileAllowAbove On
    </Directory>

For versions >=0.10 (e.g. Ubuntu 12.10)

::

    <Directory /var/www/owncloud>
        ...
        SetEnv MOD_X_SENDFILE_ENABLED 1
        XSendFile On
        XSendFilePath /home/valerio
    </Directory>

* **SetEnv MOD_X_SENDFILE_ENABLED**: tells ownCloud scripts that they should add the X-Sendfile header when serving files
* **XSendFile**: enables web server handling of X-Sendfile headers (and therefore file serving) for the specified Directory
* **XSendFileAllowAbove (<0.10)**: enables file serving through the web server on a path outside the specified Directory. This is needed for configured local mounts which may reside outside the data directory.
* **XSendFilePath (>=0.10)**: a white list of paths that the web server is allowed to serve outside of the specified Directory. Other paths which correspond to local mounts should be configured here as well. For a more in-depth documentation of this directive refer to the mod_xsendfile website linked above.

Nginx (X-Accel-Redirect)
------------------------
Nginx supports handling of static files differently from Apache. Documentation can be found in the Nginx Wiki
section `Mod X-Sendfile <http://wiki.nginx.org/XSendfile>`_ and section `X-Accell <http://wiki.nginx.org/X-accel>`_.
The header used by Nginx is X-Accel-Redirect.

Installation
~~~~~~~~~~~~
X-Accel-Redirect is supported by default in Nginx and no additional operation should be needed to install it.

Configuration - Method 1
~~~~~~~~~~~~~~~~~~~~~~~~
Method 1 is preferred because it limits what files can be served through X-Accel. ::

    location ~ \.php(?:$|/) {
        ...
        fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
    }

    location ^~ /data {
        internal;
        # Set 'alias' if not using the default 'datadirectory'
        #alias /path/to/non-default/datadirectory;

    #    LOCAL-MOUNT-NAME should match "Folder name" and 'alias' value should match "Configuration"
    #    A 'Local' External Storage Mountpoint available to a single user
    #    location /data/USER/files/LOCAL-FS-MOUNT-NAME {
    #        alias /path/to/local-mountpoint;
    #    }

    #    A 'Local' External Storage Mountpoint available to multiple users
    #    location ~ ^/data/(?:USER1|USER2)/files/LOCAL-FS-MOUNT-NAME/(.+)$ {
    #        alias /path/to/local-mountpoint/$1;
    #    }

    #    A 'Local' External Storage Mountpoint available to all users
    #    location ~ ^/data/[^/]+/files/LOCAL-FS-MOUNT-NAME/(.+)$ {
    #        alias /path/to/local-mountpoint/$1;
    #    }

    }


* **fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED** ~ Tells ownCloud scripts that they should add the X-Accel-Redirect header when serving files.
* **/data** ~ The ownCloud data directory.  Any 'Local' External Storage Mounts must also have nested locations here.

  * set alias if you are using a non-default data directory

  * **/data/USER/files/LOCAL-MOUNT-NAME** ~ a local external storage mount available to a single user

  * **~ ^/data/(?:USER1|USER2)/files/LOCAL-MOUNT-NAME/(.+)$** ~ a local external storage mount available to multiple users

  * **~ ^/data/[^/]+/files/LOCAL-MOUNT-NAME/(.+)$** ~ a local external storage mount available to all users

Configuration - Method 2
~~~~~~~~~~~~~~~~~~~~~~~~
Method 2 is simpler to setup when using local external storage mounts, especially when they are available to many, but not all users.
This method may be preferred if you are regularly adding users that should not all have access to the same local external storage mount(s). ::

    location ~ \.php(?:$|/) {
        ...
        fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
        fastcgi_param MOD_X_ACCEL_REDIRECT_PREFIX /xaccel;
    }

    location ^~ /xaccel {
        internal;
        alias /;
    }

* **fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED** ~ Tells ownCloud scripts that they should add the X-Accel-Redirect header when serving files.
* **fastcgi_param MOD_X_ACCEL_REDIRECT_PREFIX** ~ A prefix to internally serve files from, in this example "/xaccel" is used but this is configurable.
* **location ^~ /xaccel** ~ The location to internally serve files from, must match MOD_X_ACCEL_REDIRECT_PREFIX.

How to check if it's working?
-----------------------------
You are still able to download stuff via the web interface and single, local file downloads can be paused and resumed.
