docker icon indicating copy to clipboard operation
docker copied to clipboard

Run Nextcloud from Subdirectory

Open studio315b opened this issue 6 years ago • 40 comments

How do you tell nextcloud that it's in a sub-directory? Currently each service on my machine is in its own docker instance, and I use Traefik to expose them to different sub-directories on my server:

mydomain/service1 mydomain/service2

Current Behaviour

all links are "/index.php/*"

Desired Behavior

all links are "/nextcloud/index.php/*"

studio315b avatar Jul 27 '18 20:07 studio315b

if I understood you correctly - you have to adjust your nextcloud configuration:

'overwritewebroot'

Look this:

https://docs.nextcloud.com/server/13/admin_manual/configuration_server/reverse_proxy_configuration.html#overwrite-parameters

regards

datenfahrt avatar Jul 29 '18 08:07 datenfahrt

This is what I was looking for. Is there a way to set this setting via docker, or do I need to modify my version of the image?

studio315b avatar Jul 30 '18 00:07 studio315b

Depending on how you run the docker container. If you mount your filesystem in /var/www/html, for example, you don't need to rebuild the container. You can find the config file in the mounted directory.

udl avatar Aug 06 '18 15:08 udl

can you show us some example, I tried everything I could( 'overwritehost' => '', htaccess.RewriteBase' => , etc.), still failed.

lilarcor avatar Aug 29 '18 06:08 lilarcor

I had a similar problem and I have created a fix for this issue (Pull request : #527) that contains the an ENV variable NEXTCOULD_OVERWRITEWEBROOT to help define "subdirectories".

Hope it helps,

naramski avatar Oct 27 '18 17:10 naramski

Hi, I am sorry maybe i didn’t get something but in 16.0.3 version , I still have troubles to access to nextcloud with subdirectories as http://myserver/nextcloud/login

anything a do and every variable i use ( like overwrite.cli.url 'htaccess.RewriteBase' ...)

i get a redirection to http://myserver/login

except if a use the variable 'overwritewebroot' => '/nextcloud’

but in this case i have ERR_TOO_MANY_REDIRECTS error

Is there something i missed ?

Thank you Best regards

omontigny avatar Jul 31 '19 06:07 omontigny

Hi all! I have the same issue. Migrating to using docker and need nextcloud to use the URL /nextcloud as I have multiple web services all using different folders.

I too get the ERR_TOO_MANY_REDIRECTS error

I've also tried fiddling with the 'htaccess.RewriteBase' => '/nextcloud', setting as well as RewriteBase / in .htaccess no success. (On some occasions all files were deleted when docker container was started)

PR #527 seems to have morphed into something different so not sure what the fix is for this as I can't get this to work either. What am I missing?

John

johnjore avatar Aug 13 '19 10:08 johnjore

I've excatly the same issue and looking for a solution.

wolftobias avatar Aug 13 '19 11:08 wolftobias

Solution, for me, was to have all connections go via proxy when using 'overwritewebroot' => '/nextcloud’

If URL is used via web browser (local network) without reverse proxy => ERR_TOO_MANY_REDIRECTS With proxy (from internet/external), logon screen appears.

Need to do more validation and testing, but looks good so far. config.php

  'overwritewebroot' => '/nextcloud',
  'trusted_proxies' =>
  array (
    0 => 'IP_of_my_proxy',
  ),
  'forwarded_for_headers' =>
  array (
    0 => 'HTTP_X_FORWARDED_FOR',
  ),

nginx

    location /nextcloud/ {
        proxy_headers_hash_max_size 512;
        proxy_headers_hash_bucket_size 64;
        add_header Front-End-Https on;
        proxy_pass http://server/;
    }

ymmv...


Update: After moving data and re-using previous DB, Android client re-connected and resumed without any reconfiguration when connecting via proxy. Without going through the proxy, it does not work (at all), lots of errors and timeouts.

I would suspect that 'overwritewebroot' not working when connecting directly is a bug, as per a number of posts here, and hopefully it gets fixed at some point.

johnjore avatar Aug 13 '19 12:08 johnjore

I tried everything, not working, by the way I use nextcloud docker image

wolftobias avatar Aug 13 '19 14:08 wolftobias

My configuration is almost the same. Nextcloud running in a docker container und nginx as webserver / proxy. The overwritewebroot command didn't work for me either, what is working is the nginx config:

location /nextcloud {
        proxy_pass http://localhost:8080;
        proxy_set_header X-Forwarded-Host $host:$server_port;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location = nextcloud/.well-known/carddav {
      return 301 $scheme://$host/remote.php/dav;
    }
    location = nextcloud/.well-known/caldav {
      return 301 $scheme://$host/remote.php/dav;
    }

Inside the Docker container, I moved the nextcloud files from /var/www/html to /var/www/html/nextcloud Then I edited the .htaccess file in the /var/www/html/nextloud almost at the bottom line from RewriteBase / to RewriteBase /nextcloud

Hope that helps, it seems that the command overwritewebroot does not affect the apache config.

The problem with the container restart doesn't affect this config. After a restart there are new files in the /var/www/html folder of a "fresh" nextcloud installation copied.

Here is are some lines of my config.php

<?php
$CONFIG = array (
  'htaccess.RewriteBase' => '/nextcloud/',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'apps_paths' =>
  array (
    0 =>
    array (
      'path' => '/var/www/html/nextcloud/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 =>
    array (
      'path' => '/var/www/html/nextcloud/custom_apps',
      'url' => '/custom_apps',
      'writable' => true,
    ),
  ),
  'instanceid' => '
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' =>
  array (
    0 => 'localhost:PORT',
  ),
  'datadirectory' => '/var/www/html/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '16.0.1.1',
  'overwrite.cli.url' => 'http://localhost:PORT/nextcloud',
  'dbname' => '',
  'dbhost' => '',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => '',
  'dbpassword' => ,
  'installed' => true,
  'overwritehost' => 'URL',
  'overwriteprotocol' => 'https',
  'trusted_proxies' =>
  array (
    0 => 'https://***.de',
    1 => 'https://***.de',
  ),
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'ssl',
...
  'theme' => '',
  'loglevel' => 0,
  'maintenance' => false,
);

Energy23 avatar Sep 03 '19 15:09 Energy23

After almost giving up i did a final test and found the following to be working. i created nextcloud with the following command: podman run --name nextcloud-my -p 8080:80 -v <local_mount_point>:/var/www/html:Z -e -d docker.io/library/nextcloud:18.0.1-apache vi <local_mount_point>/html/.htaccess add or change "RewriteBase /" to "RewriteBase /your-sub-context" vi <local_mount_point>/html/config/config.php add 'overwritewebroot' => '/your-sub-context', cd <local_mount_point>/html/ ln -s ../html/ your-sub-context

the symlink is important because otherwise somehow the app does not look at the right path. Most likely configurations within the apache would be required, but i didnt want to modify these as i would have to mount those to my host system then. Anyways, hope that this helps for some of you!

StokeHead avatar Mar 04 '20 14:03 StokeHead

@StokeHead Many thanks! It seems htaccess.RewriteBase in config.php does not do its job.

bakcsa83 avatar Mar 27 '20 21:03 bakcsa83

@StokeHead @bakcsa83 thanks to you both but as i documented here i still cannot get Nextcloud served as root but from a subdirectory. it indeed looks like htaccess.RewriteBase does not have an effect but the symlink did not work for me in a non-Docker installation. thanks again.

waynedpj avatar Mar 30 '20 17:03 waynedpj

I found out later that there is a command that updates the .htaccess file: sudo -u www-data php occ maintenance:update:htaccess (did not try it though) @waynedpj I can only confirm that the solution described by @StokeHead worked for me.

However, the self-update feature did not work (in brand new image) so I just went back to lxc.

bakcsa83 avatar Apr 02 '20 19:04 bakcsa83

thanks @bakcsa83 however i forgot to mention that i had already tried running the occ maintenance:update:htaccess command as well, still no luck. i had also followed @StokeHead but could not get it working. regardless thanks again.

waynedpj avatar Apr 04 '20 12:04 waynedpj

Same problem as in nextcloud/server#20338.

We need help here, fumbling around inside the container is not the way to go.

DoctorDrunk avatar Apr 07 '20 21:04 DoctorDrunk

careful bump

DoctorDrunk avatar Apr 10 '20 16:04 DoctorDrunk

careless bump

nicbou avatar May 05 '20 14:05 nicbou

For anyone who "just wants it to work", i've been able to get it to work with a modified docker image:

FROM nextcloud

RUN apt-get update && apt-get install sudo

RUN echo "sudo -u www-data php occ maintenance:update:htaccess && /usr/local/bin/apache2-foreground">/usr/local/sbin/apache2-foreground && chmod +x /usr/local/sbin/apache2-foreground

CMD ln -srf /var/www/html /var/www/html/nextcloud && /entrypoint.sh apache2-foreground

then just set overwritewebroot and htaccess.RewriteBase to /nextcloud, and http://localhost:<port>/nextcloud should work! (make sure to restart the container)

Now what would be great is a env var to set the RewriteBase property

EDIT: If the above doesn't work, open /var/www/html/.htaccess scroll to line 112, and change RewriteBase to /nextcloud

EDIT2: Or you can just use the linuxserver/nextcloud image, it uses nginx and you can pretty much effortlessly do this (example from their letsencrypt nginx reverse proxy image, linuxserver/letsencrypt, soon to be renamed linuxserver/swag)

# Assuming this container is called "letsencrypt", edit your nextcloud container's config
# located at /config/www/nextcloud/config/config.php and add the following lines before the ");":
#  'trusted_proxies' => ['letsencrypt'],
#  'overwritewebroot' => '/nextcloud',
#  'overwrite.cli.url' => 'https://your-domain.com/nextcloud',
#
# Also don't forget to add your domain name to the trusted domains array. It should look somewhat like this:
#  array (
#    0 => '192.168.0.1:444', # This line may look different on your setup, don't modify it.
#    1 => 'your-domain.com',
#  ),

# Redirects for DAV clients
location = /.well-known/carddav {
    return 301 $scheme://$host/nextcloud/remote.php/dav;
}

location = /.well-known/caldav {
    return 301 $scheme://$host/nextcloud/remote.php/dav;
}

location /nextcloud {
    return 301 $scheme://$host/nextcloud/;
}

location ^~ /nextcloud/ {
    include /config/nginx/proxy.conf;
    resolver 127.0.0.11 valid=30s;
    set $upstream_app nextcloud;
    set $upstream_port 443;
    set $upstream_proto https;
    proxy_pass $upstream_proto://$upstream_app:$upstream_port;

    rewrite /nextcloud(.*) $1 break;
    proxy_max_temp_file_size 2048m;

    proxy_set_header Range $http_range;
    proxy_set_header If-Range $http_if_range;
    proxy_set_header Connection $http_connection;
    proxy_redirect off;
    proxy_ssl_session_reuse off;
}

(not gonna attach proxy.conf here, just have a look at their image)

m00nwtchr avatar Jul 28 '20 11:07 m00nwtchr

Thank you guys!

I could get it working with my setup which also involves a traefik container as reverse proxy. Though I had a minor hickup on the way, namely that the web browser login form and the desktop client's authentication form froze but that can be fixed too as described in the following:

These are the steps on how I got it working:

1.)

Pull the image via a docker-compose.yml:

...
  service_nextcloud:
    image: nextcloud:latest
    volumes:
      - ./volumes/nc_image/html:/var/www/html
...

2.)

Start the containers, then in the nextcloud container the dircetory /var/www/html gets pouplated with code. Within the container I then did:

ln -s /var/www/html/ /var/www/html/intern
chown www-data:root -h /var/www/html/intern

3.)

Then open nextcloud in a browser on your domain, register an admin user. Then config files got auto generated of which the next ones to modify are /var/www/html/.htaccess and /var/www/html/config/config.php

4.)

modify RewriteBase in /var/www/html/.htaccess where there was the following auto-generated code-block.

This step seems important as without it I get an ERR_TOO_MANY_REDIRECTS error.

#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####

ErrorDocument 403 /intern/
ErrorDocument 404 /intern/
<IfModule mod_rewrite.c>
  Options -MultiViews
  RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]
  ....
  RewriteBase /intern # <-- changed from auto generated `RewriteBase /`
  ....
</IfModule>

5.)

Now it would work mostly already, except for the fact that the web login and client authentication form hang. This issue is discussed here: https://github.com/nextcloud/server/issues/19091 and to fix it, add the following to /var/www/html/config/config.php:

...
'overwriteprotocol' => 'https' 
...

Now it's all working fine.

My current nextcloud version is 19 and traefik is v2.2

SteffRhes avatar Aug 17 '20 11:08 SteffRhes

Try this: https://github.com/nextcloud/nextcloud-snap/wiki/Putting-the-snap-behind-a-reverse-proxy#nginx-optional-custom-path-location-for-reverse-proxy It worked for me.

ettingshausen avatar Aug 21 '20 09:08 ettingshausen

It worked for me using the following lines:

docker-compose.yml:

  cloud_server:
    environment:
      - OVERWRITEWEBROOT=/cloud

and config.php:

  'htaccess.RewriteBase' => '/cloud',

...and running docker-compose exec -uwww-data cloud_server php occ maintenance:update:htaccess (after updating config.php)

Would be nice to have an env variable for htaccess.RewriteBase to have a clean setup without calling occ manually. Something like OVERWRITEHTACCESSBASE (or something more readable :D)?

PS.: Also @ettingshausen's link was very useful by adding rewrite ^/cloud(.*) $1 break; to my proxy's config 😊

timonf avatar Nov 07 '20 00:11 timonf

@timonf Hi! I tried the 3 steps from your post

  • OVERWRITEWEBROOT variable in compose file
  • change config.php
  • run the command

but the "docker-compose exec " command is not working for me.

Nextcloud is not installed - only a limited number of commands are available There are no commands defined in the "maintenance:update" namespace. Did you mean this? maintenance

Any idea?

cheers michael

Michael-Hennemann avatar Apr 11 '21 14:04 Michael-Hennemann

Try this: https://github.com/nextcloud/nextcloud-snap/wiki/Putting-the-snap-behind-a-reverse-proxy#nginx-optional-custom-path-location-for-reverse-proxy It worked for me.

This was a big help to me, I was experiencing redirect loops (HTTP 302) as soon as I added overwritewebroot in nextcloud's config.php and had my reverse proxy redirect to nextcloud for location /nextcloud instead of /. My setup: nextcloud runs in a docker container on one host and a Nginx reverse proxy forwards traffic for location /nextcloud.

To enable the reverse proxy I follow the nextcloud docs: https://docs.nextcloud.com/server/19/admin_manual/configuration_server/reverse_proxy_configuration.html I added these lines to nextclouds config.php, as described in the example at the bottom:

  'trusted_proxies'   => ['10.0.0.1'],
  'overwritehost'     => 'ssl-proxy.tld',
  'overwriteprotocol' => 'https',
  'overwritewebroot'  => '/domain.tld/nextcloud',
  'overwritecondaddr' => '^10\.0\.0\.1$',

Then setup nginx as a reverse proxy as follows: https://github.com/nextcloud/nextcloud-snap/wiki/Putting-the-snap-behind-a-reverse-proxy#nginx-optional-custom-path-location-for-reverse-proxy

location /domain.tld/nextcloud {
     return 301 $scheme://$server_name/domain.tld/nextcloud/;
}
location /domain.tld/nextcloud/ {
   rewrite ^/domain.tld/nextcloud(.*) $1 break;
   proxy_pass http://host_with_docker;
}

The rewrite line was key here, I scowered a lot of guides & forums with similar setups, but this was the first time the rewrite was mentioned. You also need to forward /domain.tld/nextcloud to /domain.tld/nextcloud/ to make it a bit more user friendly. Putting everything under location /domain.tld/nextcloud instead of the forwarding breakes the rewrite in the edge case of visiting /domain.tld/nextcloud, because $1 is emtpy and the proxy throws an error: the rewritten URI has a zero length

SigiSang avatar Apr 28 '21 20:04 SigiSang

Anyone care to help me out? I get constant 302s after creating my admin account, no matter what I do. Here's a post I was going to post to the forums, but it kept rejecting my post :/ Google proved to me the forums are useless anyway, this topic is on there constantly with zero responses.

I am trying to setup nextcloud on a subdirectory (example.com/nextcloud). I can get to the initial setup screen (where I create an admin account), but after pressing enter after entering my new password, I can only get a 302 message on example.com/nextcloud.

Here is my docker-compose.yml for nextcloud and its db, modified slightly from https://github.com/nextcloud/docker#base-version---fpm

 version: '2'
 
 services: 
   db:
     image: mariadb
     restart: always
     command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
     volumes:
       - ./db:/var/lib/mysql
 
     environment:
       - MYSQL_ROOT_PASSWORD=ROOTPASS 
       - MYSQL_PASSWORD=PASS
       - MYSQL_DATABASE=nextcloud
       - MYSQL_USER=nextcloud
 
   app:
     image: nextcloud:production-fpm-alpine
     restart: always
     links:
       - db
     ports:
       - 9000:9000
 
     volumes:
       - ./app:/var/www/html
 
     environment:
       - MYSQL_PASSWORD=PASS
       - MYSQL_DATABASE=nextcloud
       - MYSQL_USER=nextcloud
       - MYSQL_HOST=db
       - OVERWRITEWEBROOT=/nextcloud

I expose port 9000 since my nginx instance is in a different docker-compose.yml. It mounts the same .app directory at /var/www/html/nextcloud.

Here is my config for nginx, modified from https://docs.nextcloud.com/server/stable/admin_manual/installation/nginx.html#nextcloud-in-a-subdir-of-the-nginx-webroot

server {
    listen DOCKERHOST:443 ssl http2;
    # listen [::]:443 ssl http2;

    # snippet sets certs
    include snippets/domain_example.com.conf;

    server_name example.com www.example.com *.example.com;

    root /var/www;

    # snippets set ssl configs, "custom" errors, security.txt
    include snippets/https.conf;
    include snippets/errors.conf;
    include snippets/security.txt.conf;

    location /.well-known {
        # The following 6 rules are borrowed from `.htaccess`

        rewrite ^/\.well-known/host-meta\.json  /nextcloud/public.php?service=host-meta-json    last;
        rewrite ^/\.well-known/host-meta        /nextcloud/public.php?service=host-meta         last;
        rewrite ^/\.well-known/webfinger        /nextcloud/public.php?service=webfinger         last;
        rewrite ^/\.well-known/nodeinfo         /nextcloud/public.php?service=nodeinfo          last;

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

        try_files $uri $uri/ =404;
    }

    location ^~ /nextcloud {
        # set max upload size
        client_max_body_size 512M;
        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 application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject 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;

        # 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-Download-Options                   "noopen"        always;
        add_header X-Frame-Options                      "SAMEORIGIN"    always;
        add_header X-Permitted-Cross-Domain-Policies    "none"          always;
        add_header X-Robots-Tag                         "none"          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`, `/ocm-provider`, `/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 {
            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(?:$|/) {
            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 DOCKERHOST:9000;

            fastcgi_intercept_errors on;
            fastcgi_request_buffering off;
        }

        location ~ \.(?:css|js|svg|gif)$ {
            try_files $uri /nextcloud/index.php$request_uri;
            expires 6M;         # Cache-Control policy borrowed from `.htaccess`
            access_log off;     # Optional: Don't log access to assets
        }

        location ~ \.woff2?$ {
            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
        }

        location /nextcloud {
            try_files $uri $uri/ /nextcloud/index.php$request_uri;
        }
    }
}

At this point, I've done what the docs say to do, and get a 404, presented to the client and also logged on the Nextcloud container:

"GET /nextcloud/index.php" 404

Then, I try https://github.com/nextcloud/docker/issues/401#issuecomment-674834095.

ln -s /var/www/html/ /var/www/nextcloud
chown www-data:root -h /var/www/nextcloud

After the above, (I used /var/www/nextcloud instead of /var/www/html/nextcloud because the nextcloud site docs used /var/www as the root instead of /var/www. Doing it any other way continues 404s).

So, I get a one time success! I am presented with a screen to create an admin (this shows I'm getting the styles/assets from my proxy). I create an account, and then configuration files are generated (Showing my proxy is reaching the nextcloud server correctly). But... Now I just constantly get 302s! Performing the rest of the steps in the github link doesn't change the constant 302s (that also log on nextcloud).

After step 3 of the linked comment, my config.php that was generated looks like this:

<?php
$CONFIG = array (
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'apps_paths' => 
  array (
    0 => 
    array (
      'path' => '/var/www/html/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 => 
    array (
      'path' => '/var/www/html/custom_apps',
      'url' => '/custom_apps',
      'writable' => true,
    ),
  ),
  'overwritewebroot' => '/nextcloud',
  'instanceid' => 'REDACTED',
  'passwordsalt' => 'REDACTED',
  'secret' => 'REDACTED',
  'trusted_domains' => 
  array (
    0 => 'example.com',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'mysql',
  'version' => '20.0.9.1',
  'overwrite.cli.url' => 'https://example.com/nextcloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'db',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => 'PASS',
  'installed' => true,
);

My .htaccess (after manually adding the rewritebase line)

<IfModule mod_headers.c>
  <IfModule mod_setenvif.c>
    <IfModule mod_fcgid.c>
       SetEnvIfNoCase ^Authorization$ "(.+)" XAUTHORIZATION=$1
       RequestHeader set XAuthorization %{XAUTHORIZATION}e env=XAUTHORIZATION
    </IfModule>
    <IfModule mod_proxy_fcgi.c>
       SetEnvIfNoCase Authorization "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>
  </IfModule>

  <IfModule mod_env.c>
    # Add security and privacy related headers

    # Avoid doubled headers by unsetting headers in "onsuccess" table,
    # then add headers to "always" table: https://github.com/nextcloud/server/pull/19002
    Header onsuccess unset Referrer-Policy
    Header always set Referrer-Policy "no-referrer"

    Header onsuccess unset X-Content-Type-Options
    Header always set X-Content-Type-Options "nosniff"

    Header onsuccess unset X-Download-Options
    Header always set X-Download-Options "noopen"

    Header onsuccess unset X-Frame-Options
    Header always set X-Frame-Options "SAMEORIGIN"

    Header onsuccess unset X-Permitted-Cross-Domain-Policies
    Header always set X-Permitted-Cross-Domain-Policies "none"

    Header onsuccess unset X-Robots-Tag
    Header always set X-Robots-Tag "none"

    Header onsuccess unset X-XSS-Protection
    Header always set X-XSS-Protection "1; mode=block"

    SetEnv modHeadersAvailable true
  </IfModule>

  # Add cache control for static resources
  <FilesMatch "\.(css|js|svg|gif)$">
    Header set Cache-Control "max-age=15778463"
  </FilesMatch>

  # Let browsers cache WOFF files for a week
  <FilesMatch "\.woff2?$">
    Header set Cache-Control "max-age=604800"
  </FilesMatch>
</IfModule>
<IfModule mod_php7.c>
  php_value mbstring.func_overload 0
  php_value default_charset 'UTF-8'
  php_value output_buffering 0
  <IfModule mod_env.c>
    SetEnv htaccessWorking true
  </IfModule>
</IfModule>
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_USER_AGENT} DavClnt
  RewriteBase /nextcloud
  RewriteRule ^$ /remote.php/webdav/ [L,R=302]
  RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  RewriteRule ^\.well-known/host-meta /public.php?service=host-meta [QSA,L]
  RewriteRule ^\.well-known/host-meta\.json /public.php?service=host-meta-json [QSA,L]
  RewriteRule ^\.well-known/webfinger /public.php?service=webfinger [QSA,L]
  RewriteRule ^\.well-known/nodeinfo /public.php?service=nodeinfo [QSA,L]
  RewriteRule ^\.well-known/carddav /remote.php/dav/ [R=301,L]
  RewriteRule ^\.well-known/caldav /remote.php/dav/ [R=301,L]
  RewriteRule ^remote/(.*) remote.php [QSA,L]
  RewriteRule ^(?:build|tests|config|lib|3rdparty|templates)/.* - [R=404,L]
  RewriteCond %{REQUEST_URI} !^/\.well-known/(acme-challenge|pki-validation)/.*
  RewriteRule ^(?:\.|autotest|occ|issue|indie|db_|console).* - [R=404,L]
</IfModule>
<IfModule mod_mime.c>
  AddType image/svg+xml svg svgz
  AddEncoding gzip svgz
</IfModule>
<IfModule mod_dir.c>
  DirectoryIndex index.php index.html
</IfModule>
AddDefaultCharset utf-8
Options -Indexes
<IfModule pagespeed_module>
  ModPagespeed Off
</IfModule>
#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####

ErrorDocument 403 /nextcloud/
ErrorDocument 404 /nextcloud/

brendan-mccoy avatar May 04 '21 00:05 brendan-mccoy

So if I understand correctly you have an Nginx reverse proxy in one docker container and nextcloud in another? In that case, I notice you don't have any nextcloud configuration for the reverse proxy (see my previous post, the one before yours). I think at least 'trusted_proxies' is required. I'm guessing the IP there needs to be the IP of your host machine, as the docker-containers come from separate docker-compose file ergo are in different docker networks.

I have set up an Nginx reverse proxy on one machine, pointing /nextcloud to a docker container on another. I didn't need to change .htaccess or create a symlink, only change the nextcloud config for reverse proxy per the docs and correctly rewrite the url with the proxy configuration in Nginx (reverse proxy, not nextclouds own Nginx instance).

P.S. Rereading your comment I'm starting to think you're trying to run nextcloud through Nginx in a different container (judging from your volume mounts), which would be very weird as the nextcloud container hosts its own Nginx instance. Is this the case?

SigiSang avatar May 04 '21 07:05 SigiSang

I am using the fpm image, which does not have its own nginx instance. Adding trusted proxies docker environment variable (which adds it to the config) doesn't change the behavior.

brendan-mccoy avatar May 04 '21 13:05 brendan-mccoy

Right, if you're just running the single nginx instance you don't need the reverse proxy config. I was misguided because you mentioned 'proxy' in your comment, but in your case it's just a single nginx hosting the files from within the nextcloud container.

Have you checked the logs of nginx to find any clue as to why throws 302?

SigiSang avatar May 04 '21 13:05 SigiSang

Nginx logs: "GET /nextcloud HTTP/2.0" 301 "GET /nextcloud/ HTTP/2.0" 302

Nextcloud logs: "GET /nextcloud/index.php" 302

I don't really see anything else unfortunately. I will try to see if maybe there's a higher level of logging for nextcloud.

brendan-mccoy avatar May 04 '21 15:05 brendan-mccoy