404 on install in subfolder

Hi all,
currently i am trying to install a new nextcloud instance using docker (for mac).

I followed the example on github which is running fine from the webserver root. But i want to make nextcloud accessibly from a subfolder like https://domain/cloud so i can have other services on the same domain and ports.

As a first try i changed all the paths inside nginx.conf to forward /cloud requests to the nextcloud container. I double checked the changes according to the nextcloud docu. But i always get a 404 page and a “Primary script unknown” error. On the console 404 says it cannot find /cloud/index.php.

Even if i copy the copy the config of the nextcloud docu, it does not work but with a different error. This time i will get a redirect error without additional prompts in any console.

Nginx logs are also always empty.

This is my docker-compose for the 404 error:

version: '3'

services:
  db:
    image: mariadb
    container_name: nextcloud-db
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=xxx
    env_file:
      - db.env

  app:
    image: nextcloud:fpm-alpine
    container_name: nextcloud-app
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/nextcloud:/var/www/html
    environment:
      - MYSQL_HOST=db
      - OVERWRITEWEBROOT=/cloud
      - NEXTCLOUD_TRUSTED_DOMAINS=domain
    env_file:
      - db.env
    depends_on:
      - db

  web:
    build: ./web
    container_name: nginx
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/nextcloud:/var/www/html/cloud:ro
    environment:
      - VIRTUAL_HOST=domain
      - LETSENCRYPT_HOST=domain
      - LETSENCRYPT_EMAIL=mail
    depends_on:
      - app
    networks:
      - proxy-tier
      - default

  proxy:
    build: ./proxy
    container_name: nginx-proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - /Users/surfer/Documents/docker/nginx/certs:/etc/nginx/certs:ro
      - /Users/surfer/Documents/docker/nginx/vhost:/etc/nginx/vhost.d
      - /Users/surfer/Documents/docker/nginx/html:/usr/share/nginx/html
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - proxy-tier

  letsencrypt-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: letsencrypt
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/nginx/certs:/etc/nginx/certs
      - /Users/surfer/Documents/docker/nginx/vhost:/etc/nginx/vhost.d
      - /Users/surfer/Documents/docker/nginx/html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
      - NGINX_PROXY_CONTAINER=nginx-proxy
      - NGINX_DOCKER_GEN_CONTAINER=nginx-proxy
    networks:
      - proxy-tier
    depends_on:
      - proxy

networks:
  proxy-tier:

and its nginx.conf:

worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    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  120;

    # hide server info on error pages
    server_tokens off;

    set_real_ip_from  10.0.0.0/8;
    set_real_ip_from  172.16.0.0/12;
    set_real_ip_from  192.168.0.0/16;
    real_ip_header    X-Real-IP;

    #gzip  on;

    upstream php-handler {
        server app:9000;
    }

    server {
        listen 80;

        # Add headers to serve security related headers
        # Before enabling Strict-Transport-Security headers please read into this
        # topic first.
        #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
        #
        # 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 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;

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

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

        # The following 2 rules are only needed for the user_webfinger app.
        # Uncomment it if you're planning to use this app.
        #rewrite ^/.well-known/host-meta /cloud/public.php?service=host-meta last;
        #rewrite ^/.well-known/host-meta.json /cloud/public.php?service=host-meta-json last;

        # The following rule is only needed for the Social app.
        # Uncomment it if you're planning to use this app.
        #rewrite ^/.well-known/webfinger /cloud/public.php?service=webfinger last;

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

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

        location ^~ /cloud {

			# set max upload size
			client_max_body_size 10G;
			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;
	
			# Uncomment if your server is build with the ngx_pagespeed module
			# This module is currently not supported.
			#pagespeed off;
	
			location /cloud {
				rewrite ^ /cloud/index.php;
			}
	
			location ~ ^\/cloud\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
				deny all;
			}
			location ~ ^\/cloud\/(?:\.|autotest|occ|issue|indie|db_|console) {
				deny all;
			}
	
			location ~ ^\/cloud\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.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;
	
				# Avoid sending the security headers twice
				fastcgi_param modHeadersAvailable true;
	
				# Enable pretty urls
				fastcgi_param front_controller_active true;
				fastcgi_pass php-handler;
				fastcgi_intercept_errors on;

                #raise timeouts especially for installation process
                fastcgi_read_timeout 600;
                fastcgi_send_timeout 600;
                fastcgi_connect_timeout 600;

				fastcgi_request_buffering off;
			}
	
			location ~ ^\/cloud\/(?:updater|oc[ms]-provider)(?:$|\/) {
				try_files $uri/ =404;
				index index.php;
			}
	
			# Adding the cache control header for js, css and map files
			# Make sure it is BELOW the PHP block
			location ~ ^\/cloud\/.+[^\/]\.(?:css|js|woff2?|svg|gif|map)$ {
				try_files $uri /cloud/index.php$request_uri;
				add_header Cache-Control "public, max-age=15778463";
				# Add headers to serve security related headers (It is intended to
				# have those duplicated to the ones above)
				# Before enabling Strict-Transport-Security headers please read into
				# this topic first.
				#add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
				#
				# 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 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;
	
				# Optional: Don't log access to assets
				access_log off;
			}
	
			location ~ ^\/cloud\/.+[^\/]\.(?:png|html|ttf|ico|jpg|jpeg|bcmap|mp4|webm)$ {
				try_files $uri /cloud/index.php$request_uri;
				# Optional: Don't log access to other assets
				access_log off;
			}
        }
    }
}

Docker-compose for the redirect error:

version: '3'

services:
  db:
    image: mariadb
    container_name: nextcloud-db
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=xxx
    env_file:
      - db.env

  app:
    image: nextcloud:fpm-alpine
    container_name: nextcloud-app
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/nextcloud:/var/www/html
    environment:
      - MYSQL_HOST=db
      - OVERWRITEWEBROOT=/cloud
      - NEXTCLOUD_TRUSTED_DOMAINS=domain
      - TRUSTED_PROXIES=domain
    env_file:
      - db.env
    depends_on:
      - db

  web:
    build: ./web
    container_name: nginx
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/nextcloud:/var/www/html/cloud:ro
      - /Users/surfer/Documents/docker/nginx/certs:/etc/nginx/certs:ro
    environment:
      - VIRTUAL_HOST=domain
      - LETSENCRYPT_HOST=domain
      - LETSENCRYPT_EMAIL=mail
    depends_on:
      - app
    networks:
      - proxy-tier
      - default

  proxy:
    build: ./proxy
    container_name: nginx-proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    volumes:
      - /Users/surfer/Documents/docker/nginx/certs:/etc/nginx/certs:ro
      - /Users/surfer/Documents/docker/nginx/vhost:/etc/nginx/vhost.d
      - /Users/surfer/Documents/docker/nginx/html:/usr/share/nginx/html
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - proxy-tier

  letsencrypt-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: letsencrypt
    restart: always
    volumes:
      - /Users/surfer/Documents/docker/nginx/certs:/etc/nginx/certs
      - /Users/surfer/Documents/docker/nginx/vhost:/etc/nginx/vhost.d
      - /Users/surfer/Documents/docker/nginx/html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
      - NGINX_PROXY_CONTAINER=nginx-proxy
      - NGINX_DOCKER_GEN_CONTAINER=nginx-proxy
    networks:
      - proxy-tier
    depends_on:
      - proxy

networks:
  proxy-tier:

Its nginx.conf:

worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    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  120;

    # hide server info on error pages
    server_tokens off;

    set_real_ip_from  10.0.0.0/8;
    set_real_ip_from  172.16.0.0/12;
    set_real_ip_from  192.168.0.0/16;
    real_ip_header    X-Real-IP;

    include /etc/nginx/conf.d/*.conf; 
}

And its nextcloud.conf:

upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php/php7.2-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;
    server_name domain;
    # enforce https
    return 301 https://$server_name:443$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name domain;

    # Use Mozilla's guidelines for SSL/TLS settings
    # https://mozilla.github.io/server-side-tls/ssl-config-generator/
    # NOTE: some settings below might be redundant
    ssl_certificate /etc/nginx/certs/dogcloud.ddns.net.crt;
    ssl_certificate_key /etc/nginx/certs/dogcloud.ddns.net.key;

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
    #
    # 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 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;

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

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

    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /cloud/public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /cloud/public.php?service=host-meta-json last;

    # The following rule is only needed for the Social app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/webfinger /cloud/public.php?service=webfinger last;

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

    location /.well-known/acme-challenge { }

    location ^~ /cloud {

        # 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;

        # Uncomment if your server is build with the ngx_pagespeed module
        # This module is currently not supported.
        #pagespeed off;

        location /cloud {
            rewrite ^ /cloud/index.php;
        }

        location ~ ^\/cloud\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
            deny all;
        }
        location ~ ^\/cloud\/(?:\.|autotest|occ|issue|indie|db_|console) {
            deny all;
        }

        location ~ ^\/cloud\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.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 SCRIPT_NAME /cloud/$fastcgi_script_name;
            # Avoid sending the security headers twice
            fastcgi_param modHeadersAvailable true;
            # Enable pretty urls
            fastcgi_param front_controller_active true;
            fastcgi_pass php-handler;
            fastcgi_intercept_errors on;

            #raise timeouts especially for installation process
            fastcgi_read_timeout 600;
            fastcgi_send_timeout 600;
            fastcgi_connect_timeout 600;

            fastcgi_request_buffering off;
        }

        location ~ ^\/cloud\/(?:updater|oc[ms]-provider)(?:$|\/) {
            try_files $uri/ =404;
            index index.php;
        }

        # Adding the cache control header for js, css and map files
        # Make sure it is BELOW the PHP block
        location ~ ^\/cloud\/.+[^\/]\.(?:css|js|woff2?|svg|gif|map)$ {
            try_files $uri /cloud/index.php$request_uri;
            add_header Cache-Control "public, max-age=15778463";
            # Add headers to serve security related headers  (It is intended
            # to have those duplicated to the ones above)
            # Before enabling Strict-Transport-Security headers please read
            # into this topic first.
            #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
            #
            # 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 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;

            # Optional: Don't log access to assets
            access_log off;
        }

        location ~ ^\/cloud\/.+[^\/]\.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
            try_files $uri /cloud/index.php$request_uri;
            # Optional: Don't log access to other assets
            access_log off;
        }
    }
}

Does anybody have an idea how to solve this?

Thanks in advance

Did you set overwritewebroot and overwrite.cli.url?

https://docs.nextcloud.com/server/18/admin_manual/configuration_server/config_sample_php_parameters.html#proxy-configurations

Hey,
the config.php does not yet exist. The error occurs when i open the installation page, so i cannot run the install process.
But i added overwritewebroot as environment to the docker-compose which does not change anything. Is it possible to add overwrite.cli.url as environment as well? I did not find any docu about that.