Nginx documentation (Unofficial (Community) )

Performance Tuning

1. HTTP/2

To increase the performance of your Nginx installation, we recommend using either the SPDY or HTTP_V2 modules, depending on your installed Nginx version.

To use HTTP_V2 for Nginx you have to check two things:

  1. Be aware that this module may not built in by default, due to a dependency to the OpenSSL version used on your system. It will be enabled with the --with-http_v2_module configuration parameter during compilation. The dependencies should be checked automatically. You can check the presence of ngx_http_v2_module by using the command: nginx -V 2>&1 | grep http_v2 -o. A description of how to compile Nginx to include modules can be found in Compiling Modules.
  2. When changing from SPDY to HTTP v2, the Nginx config has to be changed from listen 443 ssl spdy; to listen 443 ssl http2;

2. Caching Metadata

The open_file_cache directive can help you to cache file metadata information. This can increase performance on high loads respectively when using eg NFS as backend. That cache can store:

  • Open file descriptors, their sizes and modification times;
  • Information on existence of directories;
  • File lookup errors, such as “file not found”, “no read permission”, and so on.

To configure metadata caching, add following directives either in your http, server or location block:

open_file_cache                 max=10000 inactive=5m;
open_file_cache_valid           1m;
open_file_cache_min_uses        1;
open_file_cache_errors          on;

Configure Nginx to use caching for ownCloud internal images and thumbnails

This mechanism speeds up presentation as it shifts requests to Nginx and minimizes PHP invocations, which otherwise would take place for every thumbnail or internal image presented every time.

1. Preparation

  • Create a directory where Nginx will save the cached thumbnails or internal images. Use any path that fits to your environment. Replace /opt/cachezone in this example with your path created:
sudo mkdir -p /opt/cachezone
sudo chown www-data:www-data /opt/cachezone

2. Configuration

a. Define when to skip the cache:

  • Option 1: map

This is the preferred method. In the http{} block, but outside the server{} block:

# skip_cache, default skip
map $request_uri $skip_cache {
     default              1;
     ~*\/thumbnail.php    0;
     ~*\/apps\/gallery\/  0;
     ~*\/core\/img\/      0;
}
  • Option 2: if

In the server{} block, above the location block mentioned below:

set $skip_cache 1;
if ($request_uri ~* "thumbnail.php")      { set $skip_cache 0; }
if ($request_uri ~* "/apps/gallery/")     { set $skip_cache 0; }
if ($request_uri ~* "/core/img/")         { set $skip_cache 0; }

a. General Config:

In case you want to have multiple cache paths with different cache keys, follow the Nginx documentation where to place the directives. For the sake of simplicity, we both add them to the http{} block.

  • Add inside the http{} block:
fastcgi_cache_path /opt/cache levels=1:2 keys_zone=cachezone:100m
                   max_size=500m inactive=60m use_temp_path=off;
fastcgi_cache_key $http_cookie$request_method$host$request_uri;
  • Add inside the server{} block the following FastCGI caching directives, as an example of a configuration:
location ~ \.php(?:$/) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    # ...
    ## Begin - FastCGI caching
    fastcgi_ignore_headers  "Cache-Control"
                            "Expires"
                            "Set-Cookie";
    fastcgi_cache_use_stale error
                            timeout
                            updating
                            http_429
                            http_500
                            http_503;
    fastcgi_cache_background_update on;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_cache cachezone;
    fastcgi_cache_valid  60m;
    fastcgi_cache_methods GET HEAD;
    ## End - FastCGI caching
}

3 Test the configuration

sudo nginx -t
sudo service nginx reload
  • Open your browser and clear your cache.
  • Logon to your ownCloud instance, open the gallery app, move thru your folders and watch while the thumbnails are generated for the first time.
  • You may also watch with eg. htop your system load while the thumbnails are processed.
  • Go to another app or logout and relogon.
  • Open the gallery app again and browse to the folders you accessed before. Your thumbnails should appear more or less immediately.
  • htop will not show up additional load while processing, compared to the high load before.
1 Like