Create an SSL certificate

There are two options [1] here: to create a self-signed certificate o to obtain a Let’s Encrypt certificate. Both options are documented in other articles, so choose either of them and let’s go.

I will assume you have a valid SSL certificate for henceforth.

[1] There is another option: to pay for a ‘cheap’ certificate. But I encourage you to use Let’s Encrypt before going that way.

Check WebDAV support

_$: nginx -V  2>&1 >/dev/null | grep "with-http_dav_module"

Configure nginx

Let’s start with a very basic configuration

# HTTP: Redirect to HTTPS
server {
    listen 80;
    return 301 https://$server_name$request_uri;

server {
    listen 443 ssl http2;

    ssl_certificate     /etc/nginx/ssl/example.pem;
    ssl_certificate_key /etc/nginx/ssl/example.key;

    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;

    # Logs
    include conf.d/nextcloud-example-com/logs;

    # Path to the root of your installation
    root /var/www/nextcloud/;

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;

    # Max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Disable gzip to avoid the removal of the ETag header
    gzip off;

    #location / {
    #   rewrite ^ /index.php$uri;

    location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34])\.php(?:$|/) {
       include fastcgi_params;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param PATH_INFO $fastcgi_path_info;

       # Avoid sending the security headers twice
       fastcgi_param modHeadersAvailable true;

       fastcgi_param front_controller_active true;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
       fastcgi_intercept_errors on;
       fastcgi_request_buffering off;

Test the configuration and reload nginx:

_$: nginx -t && systemctl reload nginx.service

Now create two files: index.html and index.php in /var/www/nextcloud.

<!DOCTYPE html>
<title>Welcome to nextcloud on Ubuntu!</title>
<h1>Welcome to nextcloud on Ubuntu!</h1>
<p>If you see this page, the nextcloud is on its way.</p>
<?php echo("PHP looks good!"); ?>

And make sure that both of them are working: