You are not logged in.

#1 2024-11-08 15:28:39

QuetschKuh
Member
Registered: 2024-05-04
Posts: 6

[SOLVED] Installing Nextcloud on NGINX alongside website in subdir

Hey there,

I want to setup nextcloud on my server but I need a relatively specific configuration that I struggle to find much information on. So here's the situation:
- I have an https website running using nginx on port 443, using just the simple domain  (e.g example.com)
- I want the address of nextcloud to be example.com/nextcloud
Now I know this is possible but I simply cannot wrap my head around how to configure this using nginx.

The official configuration for nextcloud in a subdir uses a seperate server block which doesn't work for me since I want to have nextcloud and my website on the same server name and port. I've tried copying the contents of the server block (so the location directives) and using `alias /usr/share/webapps;` (I use the default nextcloud location of /usr/share/webapps/nextcloud), but even after adding that to every single location directive I still get a 404 after getting redirected to the login page. In general I've just been tinkering around a lot with the config, making some more and some less educated guesses and but haven't gotten anywhere.

Thanks to everyone in advance! I know this may seem trivial to some and I appreciate any help!

Last edited by QuetschKuh (2024-11-15 08:02:23)

Offline

#2 2024-11-08 18:20:37

JustRegularEverydayNormal
Member
From: Spain
Registered: 2024-11-04
Posts: 12

Re: [SOLVED] Installing Nextcloud on NGINX alongside website in subdir

Hi

I think you almost got the solution, you only need to authorize the page for the 404 error
Have you tried the parameter overwritehost

More info here

Hope it helps!


<3

Offline

#3 2024-11-09 13:16:40

QuetschKuh
Member
Registered: 2024-05-04
Posts: 6

Re: [SOLVED] Installing Nextcloud on NGINX alongside website in subdir

Thank you a lot for your suggestion!
I tried what I think you meant but it doesn't seem to have worked. I'll just post my full nextcloud config.php and nginx.conf.
So here's the nginx:

user http;
worker_processes 2;             # Amount of cores to assign a worker to

events {
    multi_accept on;            # Handle multiple connections per clock
    worker_connections 1024;    # Number of allowed connections per worker process
}

http {
    # General configuration
    charset utf-8;              # Automatically sends charset meta
    sendfile on;                # Skip buffering downloaded files
    sendfile_max_chunk 4m;      # Ensure both a fast connection and not overloading the server 
    types_hash_max_size 4096;   # There was a WARN to increase this idk
    client_max_body_size 4m;    # Increase allowed size of client requests    
    
    # MIME
    include mime.types;                     # Configuration for how to handle different file types
    default_type application/octet-stream;  # Handle as binary if file type unknown

    # Include other configurations
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    # (nextcloud) Set the `immutable` cache control options only for assets with a cache busting `v` argument
    map $arg_v $asset_immutable {
        "" "";
        default ", immutable";
    }

    # BLOCKS
    server {
        listen 80;
            server_name example.com www.example.com;
            return 301 https://$host$request_uri;
    }

    server {
        listen 443 default_server ssl;
            server_name example.com www.example.com;

            root /var/www/html;

            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
            ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
        ssl_stapling on;
        ssl_stapling_verify on;

        # Prevent Nginx HTTP Server Detection
        server_tokens off;

        location = /robots.txt {
            allow all;
        }

        location ^~ /.well-known {
            location = /.well-known/carddav { return 301 /remote.php/dav/; }
            location = /.well-known/caldav  { return 301 /remote.php/dav/; }

            location /.well-known/acme-challenge    { root /var/www; try_files $uri $uri/ =404; }
            location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

            # Let Nextcloud's API for `/.well-known` URIs handle all other
            # requests by passing them to the front-end controller.
            return 301 /nextcloud/index.php$request_uri;
        }

        # PHP for Nextcloud
        location ~ \.php$ {
            alias /usr/share/webapps;
            try_files $fastcgi_script_name =404; #404
            include fastcgi_params;

            # fastcgi settings
            fastcgi_pass                        unix:/run/php-fpm-legacy/nextcloud.sock;
            fastcgi_index                       index.php;
            fastcgi_buffers                     8 16k;
            fastcgi_buffer_size         32k;

            # fastcgi params
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_param SCRIPT_FILENAME       $realpath_root$fastcgi_script_name;
            #fastcgi_param PHP_ADMIN_VALUE      "open_basedir=$base/:/usr/lib/php/:/tmp/";
        }

        location ^~ /nextcloud {
            alias /usr/share/webapps;
            # set max upload size and increase upload timeout:
            client_max_body_size 512M;
            client_body_timeout 300s;
            fastcgi_buffers 64 4K;

            # Enable gzip but do not remove ETag headers
            gzip on;
            gzip_vary on;
            gzip_comp_level 4;
            gzip_min_length 256;
            gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
            gzip_types application/atom+xml text/javascript application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/wasm application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

            # Pagespeed is not supported by Nextcloud, so if your server is built
            # with the `ngx_pagespeed` module, uncomment this line to disable it.
            #pagespeed off;

            # The settings allows you to optimize the HTTP2 bandwidth.
            # See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
            # for tuning hints
            client_body_buffer_size 512k;

            # HSTS settings
            # WARNING: Only add the preload option once you read about
            # the consequences in https://hstspreload.org/. This option
            # will add the domain to a hardcoded list that is shipped
            # in all major browsers and getting removed from this list
            # could take several months.
            #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;

            # HTTP response headers borrowed from Nextcloud `.htaccess`
            add_header Referrer-Policy                   "no-referrer"       always;
            add_header X-Content-Type-Options            "nosniff"           always;
            add_header X-Frame-Options                   "SAMEORIGIN"        always;
            add_header X-Permitted-Cross-Domain-Policies "none"              always;
            add_header X-Robots-Tag                      "noindex, nofollow" always;
            add_header X-XSS-Protection                  "1; mode=block"     always;

            # Remove X-Powered-By, which is an information leak
            fastcgi_hide_header X-Powered-By;

            # Specify how to handle directories -- specifying `/nextcloud/index.php$request_uri`
            # here as the fallback means that Nginx always exhibits the desired behaviour
            # when a client requests a path that corresponds to a directory that exists
            # on the server. In particular, if that directory contains an index.php file,
            # that file is correctly served; if it doesn't, then the request is passed to
            # the front-end controller. This consistent behaviour means that we don't need
            # to specify custom rules for certain paths (e.g. images and other assets,
            # `/updater`, `/ocs-provider`), and thus
            # `try_files $uri $uri/ /nextcloud/index.php$request_uri`
            # always provides the desired behaviour.
            index index.php index.html /nextcloud/index.php$request_uri;

            # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
            location = /nextcloud {
                alias /usr/share/webapps;
                if ( $http_user_agent ~ ^DavClnt ) {
                    return 302 /nextcloud/remote.php/webdav/$is_args$args;
                }
            }

            # Rules borrowed from `.htaccess` to hide certain paths from clients
            location ~ ^/nextcloud/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)    { return 404; }
            location ~ ^/nextcloud/(?:\.|autotest|occ|issue|indie|db_|console)                  { return 404; }

            # Ensure this block, which passes PHP files to the PHP process, is above the blocks
            # which handle static assets (as seen below). If this block is not declared first,
            # then Nginx will encounter an infinite rewriting loop when it prepends
            # `/nextcloud/index.php` to the URI, resulting in a HTTP 500 error response.
            location ~ \.php(?:$|/) {
                alias /usr/share/webapps;
                # Required for legacy support
                rewrite ^/nextcloud/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|ocs-provider\/.+|.+\/richdocumentscode(_arm64)?\/proxy) /nextcloud/index.php$request_uri;

                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                set $path_info $fastcgi_path_info;

                try_files $fastcgi_script_name =404;

                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $path_info;
                fastcgi_param HTTPS on;

                fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice
                fastcgi_param front_controller_active true;     # Enable pretty urls
                fastcgi_pass unix:/run/php-fpm-legacy/nextcloud.sock;

                fastcgi_intercept_errors on;
                fastcgi_request_buffering off;

                fastcgi_max_temp_file_size 0;
            }

            # Serve static files
            location ~ \.(?:css|js|mjs|svg|gif|ico|jpg|png|webp|wasm|tflite|map|ogg|flac)$ {
                alias /usr/share/webapps;
                try_files $uri /nextcloud/index.php$request_uri;
                # HTTP response headers borrowed from Nextcloud `.htaccess`
                add_header Cache-Control                     "public, max-age=15778463$asset_immutable";
                add_header Referrer-Policy                   "no-referrer"       always;
                add_header X-Content-Type-Options            "nosniff"           always;
                add_header X-Frame-Options                   "SAMEORIGIN"        always;
                add_header X-Permitted-Cross-Domain-Policies "none"              always;
                add_header X-Robots-Tag                      "noindex, nofollow" always;
                add_header X-XSS-Protection                  "1; mode=block"     always;
                access_log off;     # Optional: Don't log access to assets
            }

            location ~ \.(otf|woff2?)$ {
                alias /usr/share/webapps;
                try_files $uri /nextcloud/index.php$request_uri;
                expires 7d;         # Cache-Control policy borrowed from `.htaccess`
                access_log off;     # Optional: Don't log access to assets
            }

            # Rule borrowed from `.htaccess`
            location /nextcloud/remote {
                alias /usr/share/webapps;
                return 301 /nextcloud/remote.php$request_uri;
            }

            location /nextcloud {
                alias /usr/share/webapps;
                try_files $uri $uri/ /nextcloud/index.php$request_uri;
            }
        }    

    }

}

Here's the previous nextcloud config:

<?php
$CONFIG = array (
  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'example.com',
  ),
  'overwrite.cli.url' => 'https://example.com/nextcloud',
  'htaccess.RewriteBase' => '/nextcloud',
  'datadirectory' => '/var/lib/nextcloud/data',
  'logfile' => '/var/log/nextcloud/nextcloud.log',
  'maintenance_window_start' => 0,

  'apps_paths' =>
  array (
    0 =>
    array (
      'path' => '/usr/share/webapps/nextcloud/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 =>
    array (
      'path' => '/var/lib/nextcloud/apps',
      'url' => '/wapps',
      'writable' => true,
    ),
  ),
  'passwordsalt' => '***',
  'secret' => '***',
  'dbtype' => 'mysql',
  'version' => '30.0.1.2',
  'dbname' => '***',
  'dbhost' => 'localhost:/run/mysqld/mysqld.sock',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => '***',
  'dbpassword' => '***',
  'installed' => true,
  'instanceid' => '***',
);

And here's what I added to the config after looking at the docs you posted:

  'overwriteprotocol' => 'https',
  'overwritewebroot' => '/nextcloud',
  'overwritehost' => 'example.com',

Offline

#4 2024-11-15 08:01:16

QuetschKuh
Member
Registered: 2024-05-04
Posts: 6

Re: [SOLVED] Installing Nextcloud on NGINX alongside website in subdir

Resolved the issue! Not entirely sure if this is the best way so if anyone has a better way please still reply but this works for now.
So I more thoroughly read up on location, alias and root. Turns out in certain cases it's actually better to use root instead of alias inside a location.
I adapted the recommended config by putting everything below the .well-known directive into another `location /nextcloud` directive with `root /usr/share/webapps;`, which looking at the wiki page now, was kind of implied to be done, though I'm sure it could be explained better.
Anyway, thanks for the help and have a nice day everyone!

Offline

Board footer

Powered by FluxBB