# Deploy Nextcloud Container

## Configure System Settings for Redis

Redis depends on a few kernel/system parameters being set appropriately to run well. Since these are kernel parameters, they can only be set on the host (not the container), and must be configured as root.

Firstly, the `overcommit_memory` system setting being set to zero may cause Redis to fail to background save. Make sure it is set to 1 by running:
```
sysctl vm.overcommit_memory=1
```
Additionally, Redis has fairly serious latency issues with THP (Transparent Huge Pages) enabled, so it is best to disable it. Disable them by running:
```
echo never > /sys/kernel/mm/transparent_hugepage/enabled
```
You can make these settings persistent accross reboots by adding them to the container start script in `/etc/containers/startup.sh`.

## Configure HAproxy

Add the following directives to your HAproxy configuration:
```
frontend www
        bind :80
        mode http

        # Reroute caldav/carddav auto-discover to appropriate address (i.e. Nextcloud)
        # Put "code 301" after "redirect" to make it a 301 redirect
        http-request redirect location /nextcloud/remote.php/dav/ if { path /.well-known/caldav /.well-known/carddav }

        # Send to nextcloud if path begins with /nextcloud
        use_backend nextcloud if { path_beg /nextcloud }

backend nextcloud
        mode http
        option forwardfor
        http-request set-header X-Forwarded-Proto https if { ssl_fc }
        # Remove the "/nextcloud" part from the path
        # WARNING: due to limitations in the config parser, the characters ")","]", or ","
        # are NOT allowed in any regular expression
        http-request set-path "%[path,regsub(^/nextcloud,)]"
        server nc-1 127.0.0.1:9080
```
And make sure to reload the configuration by running:
```
service haproxy reload
```