Hi Tomas.
That’s the good part about docker and containers in general, they are working isolated from each other and Nginx config for onlyoffice won’t interfere with your config for nextcloud. I suggest you take a look at portainer, that’s a great web UI for managing docker containers and it will make things A LOT easier if you are learning.
Back to your question, I am using a local DNS server and self-signed certificates but you can just make valid certs with letsencrypt without using docker.
So this is my docker-compose file (stacks in portainer), just change usernames and passwords
version: '3'
services:
app:
container_name: app-server
image: nextcloud:fpm
restart: always
expose:
- '80'
- '9000'
volumes:
- /home/docker/nextcloud/app_data:/var/www/html
- /home/docker/nextcloud/config/php.ini:/usr/local/etc/php/conf.d/zzz-custom.ini
environment:
- MYSQL_PASSWORD=your_db_password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
- MYSQL_HOST=db
- REDIS_HOST=redis
- OVERWRITEPROTOCOL=https
networks:
- frontend
- backend
db:
image: mariadb
container_name: nextcloud_db
restart: always
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --innodb-file-per-table=1 --skip-innodb-read-only-compressed
volumes:
- /home/docker/nextcloud/db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=mysql_root_password
- MYSQL_PASSWORD=your_db_password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
networks:
- backend
onlyoffice-document-server:
container_name: onlyoffice-document-server
image: onlyoffice/documentserver:latest
restart: always
expose:
- '80'
- '443'
volumes:
- /home/docker/onlyoffice/document_data:/var/www/onlyoffice/Data
- /home/docker/onlyoffice/document_log:/var/log/onlyoffice
environment:
- USE_UNAUTHORIZED_STORAGE=true
networks:
- frontend
nginx:
container_name: nginx-server
image: nginx
restart: always
ports:
- 80:80
- 443:443
volumes:
- /home/docker/nextcloud/config/nginx-ssl.conf:/etc/nginx/nginx.conf:ro
- /home/docker/nextcloud/config/certs:/etc/nginx/certs:ro
- /home/docker/nextcloud/config/common.conf:/etc/nginx/common.conf:ro
- /home/docker/nextcloud/app_data:/var/www/html
networks:
- frontend
- backend
redis:
image: redis:latest
container_name: nextcloud_redis
restart: always
networks:
- backend
volumes:
- /home/docker/nextcloud/redis:/var/lib/redis
volumes:
document_data:
document_log:
app_data:
mysql_data:
networks:
frontend:
backend:
To understand the basics of docker I would strongly recommend you youtube channel Digital Life and read the official documentation.
So basically this docker-compose file will deploy five containers. The nextcloud fpm, mariadb, onlyoffice, nginx web server, and redis.
As you can see all my volumes are stored at /home/docker/app_name/service_name and I do it like that just because I feel like it, you can store it wherever you want.
I mapped php.ini from my host system to the NC container to overwrite some settings that allow me to upload bigger files and allow PHP to use more ram.
php.ini:
upload_max_filesize=1G
post_max_size=1G
memory_limit=2G
Next, you can see a few folders/files are mapped to nginx container.
File nginx-ssl.conf is mapped to nginx container as nginx.conf and it contains the following:
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream backend {
server app-server:9000;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
map $http_host $this_host {
"" $host;
default $http_host;
}
map $http_x_forwarded_proto $the_scheme {
default $http_x_forwarded_proto;
"" $scheme;
}
map $http_x_forwarded_host $the_host {
default $http_x_forwarded_host;
"" $this_host;
}
server {
listen 80;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 80;
server_name nginx-server;
include /etc/nginx/common.conf;
}
server {
listen 443 ssl;
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
include /etc/nginx/common.conf;
}
}
As you noticed, SSL certificates are stored at /etc/nginx/certs/ inside the container, but at the host system they will be at /home/docker/nextcloud/config/certs
And the last one, nginx-ssl.conf is pointing to /etc/nginx/common.conf, which is also mapped to container. It contents:
# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
root /var/www/html;
client_max_body_size 10G; # 0=unlimited - set max upload size
fastcgi_buffers 64 4K;
gzip off;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
rewrite ^/.well-known/carddav /remote.php/dav/ permanent;
rewrite ^/.well-known/caldav /remote.php/dav/ permanent;
#rewrite ^/.well-known/webfinger /index.php/.well-known/webfinger;
#rewrite ^/.well-known/nodeinfo /index.php/.well-known/nodeinfo
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location / {
rewrite ^/remote/(.*) /remote.php last;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ =404;
}
location ~* ^/ds-vpath/ {
rewrite /ds-vpath/(.*) /$1 break;
proxy_pass http://onlyoffice-document-server;
proxy_redirect off;
client_max_body_size 100m;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $the_host/ds-vpath;
proxy_set_header X-Forwarded-Proto $the_scheme;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS off;
fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
fastcgi_pass backend;
fastcgi_intercept_errors on;
}
# Adding the cache control header for js and css files
# Make sure it is BELOW the location ~ \.php(?:$|/) { block
location ~* \.(?:css|js)$ {
add_header Cache-Control "public, max-age=7200";
# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
# Optional: Don't log access to assets
access_log off;
}
# Optional: Don't log access to other assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
access_log off;
}
After deploying all containers you can run set_configuration.sh from the git repository mentioned above. Note that bash script requires container names to be like it docker-compose, so if you would like to rename your containers you should also make changes to the scrips.
And that’s it, you should be able to use your services after that.