Can't serve static files through nginx reverse proxy with Nextcloud docker image

I asked the question on ServerFault but didn’t get any sensible advice.

I have multiple web apps running in Docker containers behing a Nginx reverse-proxy (in its own container).

I’m trying to install Nextcloud 25 with the standard image (actually, I want to upgrade from owncloud to nextcloud but I want to have a running instance of Nextcloud first ; that’s why I’m starting with nextcloud:25-fpm image). I chose not to use Nextcloud-AIO because I already have a postgresql container.

Since I have several web apps served by nginx, I mount the files for each app in the nginx container in a subdir of /var/www/html ; Nextcloud files are in /var/www/html/nextcloud

Nextcloud version : 25.0.13.2
Operating system and version : Debian 12
Apache or nginx version : nginx 1.23.3
PHP version : 8.1.26

The issue I am facing:
nginx does proxy the request to index.php fine, but the static files are not served. So for eaxmple, I can’t access the web interface (which relies on js).
I was able to install with occ maintenance:install

This is because I have two different document_root:

  • in the nginx container, the Nextcloud files are mounted in /var/www/html/nextcloud
  • in the nextcloud container (built on php-fpm), they are in /var/www/html

I ran into this problem before with owncloud so I had made a custom image with a script that would move owncloud files to /var/www/html/owncloud in the fpm container upon install, so that document_root was the same for both containers.
But it seems more complicated with the Nextcloud image. I haven’t been able to figure out a script to put in docker-entrypoint-hooks.d/post-installation and I assume I should also do something upon upgrades…

Logs
Here’s the relevant parts of my docker-compose.yml file:

 nginx:
        image: nginx:latest
        restart: unless-stopped
        volumes:
          - /home/etc/nginx/ssl:/etc/nginx/ssl:ro
          - /home/etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
          - /home/etc/nginx/conf.d:/etc/nginx/conf.d:ro
          - other_vol:/var/www/html/other
          - nextcloud_vol:/var/www/html/owncloud
        ports:
          - "80:80"
          - "443:443"
        networks:
          - web_net

    nextcloud:
        image: nextcloud:25-fpm
        restart: unless-stopped
        volumes:
          - nextcloud_vol:/var/www/html
        networks:
          - web_net

NB - the networks and volumes are defined and I have running postgresql and redis containers

/etc/nginx/conf.f/nextcloud.conf

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    listen [::]:80;            # comment to disable IPv6

    if ($scheme = "http") {
        return 302 https://$host$request_uri;
    }

    listen 443 ssl http2;      # for nginx versions below v1.25.1
    listen [::]:443 ssl http2; # for nginx versions below v1.25.1 - comment to disable IPv6

    # listen 443 ssl;      # for nginx v1.25.1+
    # listen [::]:443 ssl; # for nginx v1.25.1+ - keep comment to disable IPv6
    
    # http2 on;                                 # uncomment to enable HTTP/2        - supported on nginx v1.25.1+
    # http3 on;                                 # uncomment to enable HTTP/3 / QUIC - supported on nginx v1.25.0+
    # quic_retry on;                            # uncomment to enable HTTP/3 / QUIC - supported on nginx v1.25.0+
    # add_header Alt-Svc 'h3=":443"; ma=86400'; # uncomment to enable HTTP/3 / QUIC - supported on nginx v1.25.0+
    # listen 443 quic reuseport;       # uncomment to enable HTTP/3 / QUIC - supported on nginx v1.25.0+ - please remove "reuseport" if there is already another quic listener on port 443 with enabled reuseport
    # listen [::]:443 quic reuseport;  # uncomment to enable HTTP/3 / QUIC - supported on nginx v1.25.0+ - please remove "reuseport" if there is already another quic listener on port 443 with enabled reuseport - keep comment to disable IPv6

    server_name cloud.example.com;

    include /etc/nginx/conf.d/ssl.conf;

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

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

    # Path to the root of your installation
    root /var/www/html/owncloud; # where the nextcloud files are located in the nginx container

    # Specify how to handle directories -- specifying `/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/ /index.php$request_uri`
    # always provides the desired behaviour.
    index index.php index.html /index.php$request_uri;

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

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

    # Make a regex exception for `/.well-known` so that clients can still
    # access it despite the existence of the regex rule
    # `location ~ /(\.|autotest|...)` which would otherwise handle requests
    # for `/.well-known`.
    location ^~ /.well-known {
        # The rules in this block are an adaptation of the rules
        # in `.htaccess` that concern `/.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    { 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 /index.php$request_uri;
    }

    # Rules borrowed from `.htaccess` to hide certain paths from clients
    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
    location ~ ^/(?:\.|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 `/index.php`
    # to the URI, resulting in a HTTP 500 error response.
    location ~ \.php(?:$|/) {
        # Required for legacy support
        rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;

        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        set $path_info $fastcgi_path_info;
        # trick from https://serverfault.com/questions/1125329/nextcloud-fpm-docker-image-behind-an-nginx-docker-container-as-proxy-reverse
        set $fpm_root /var/www/html/;

#        try_files $fastcgi_script_name =404;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $fpm_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 nextcloud:9000;

        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ \.(?:css|js|svg|gif)$ {
        try_files $uri /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 /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 /remote {
        return 301 /remote.php$request_uri;
    }

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

NB - I used a trick from this ServerFault post to proxy requests to index.php in the location block that passes requests to the proxy:

    location ~ \.php(?:$|/) {
(...)
        set $fpm_root /var/www/html/;
(...)
        fastcgi_param SCRIPT_FILENAME $fpm_root$fastcgi_script_name;
(...)
    }

I suppose there should be something in the block that handles static files to do the same (but I’m no nginx guru :slightly_frowning_face:)

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

logs from the nginx container

  • When I browse cloud.example.com → I get a 404
nginx_1      | xxx.xxx.xxx.xxx - - [07/Dec/2023:13:20:57 +0100] "GET / HTTP/2.0" 404 174 "-" "UserAgent" "-"(/var/www/html/owncloud/ -> GET / HTTP/2.0)[ p
athinfo :  / uri : / ]
  • when I browse cloud.example.com/index.php, I get redirected to cloud.example.com/login (not functional because no js is loaded)
nginx_1      | xxx.xxx.xxx.xxx - - [07/Dec/2023:13:20:57 +0100] "GET /login HTTP/2.0" 200 6020 "https://cloud.example.c
om/" "UserAgent" "-"(/var/www/
html/owncloud/index.php -> GET /login HTTP/2.0)[ pathinfo : /login / uri : /index.php/login ]
  • the static files are logged as :
nginx_1      | 176.145.89.35 - - [07/Dec/2023:13:20:57 +0100] "GET /core/css/server.css?v=61bb1c8f-0 HTTP/2.0" 404 20 "htt
ps://cloud.example.com/login" "UserAgent" "-"(/var/www/html/owncloud/index.php -> GET /core/css/server.css?v=61bb1c8f-0 HTTP/2.0)[ pathinfo : /core
/css/server.css / uri : /index.php/core/css/server.css ]
(etc for other css or js assets)

logs from the nextcloud container

nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 200
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404
nextcloud_1  | 172.29.0.12 -  07/Dec/2023:13:20:57 +0100 "GET /index.php" 404

Only the first request (index.php/login) is served, the rest (static files) are 404.

My guess is that requests such as /index.php/core/css/server.css are proxied to the nextcloud container, but it can’t find them because of a confusion with document_root, looking for them in /var/www/html/owncloud/core/css (nginx mount point) instead of /var/www/html/core/css (nextcloud mount point)

What can I do ? Thanks in advance for your help.

Solved, I’m just an idiot :pensive:, I was not pointing my document_root correctly :
it was /var/ww/html/owncloud instead of /var/www/html/nextcloud

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.