Cannot install nextcloud on another path than /var/www/

This might be an nginx configuration problem, but I’m unable to get nginx to use the /usr/share/nextcloud as root.
I’m trying to use two different locations with two different roots, one for nextcloud, the other for directory listing.
This is what my nginx.conf looks like:

root@mydomain:/etc/nginx/sites-enabled# grep -E -v “^(\s*#|$)” nextcloud.conf
server {
listen 80;
server_name mydomain.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mydomain.example.com;
ssl_certificate “/etc/letsencrypt/live/mydomain.example.com/cert.pem”;
ssl_certificate_key “/etc/letsencrypt/live/mydomain.example.com/privkey.pem”;
add_header Strict-Transport-Security “max-age=15768000;
includeSubDomains; preload;”;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options “SAMEORIGIN”;
add_header X-XSS-Protection “1; mode=block”;
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
location / {
root /var/www/html;
autoindex on;
}
location /nextcloud {
root /usr/share;
include fastcgi.conf;
rewrite ^ /index.php$uri;
}
client_max_body_size 512M;
fastcgi_buffers 64 4K;
gzip off;
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:.|autotest|occ|issue|indie|db_|console) {
deny all;
}
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 /usr/share/nextcloud$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS on;
fastcgi_param modHeadersAvailable true;
fastcgi_param front_controller_active true;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
try_files $uri/ =404;
index index.php;
}
location ~
.(?:css|js)$ {
try_files $uri /index.php$uri$is_args$args;
add_header Cache-Control “public, max-age=7200”;
add_header Strict-Transport-Security “max-age=15768000;
includeSubDomains; preload;”;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options “SAMEORIGIN”;
add_header X-XSS-Protection “1; mode=block”;
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
access_log off;
}
location ~* .(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ {
try_files $uri /index.php$uri$is_args$args;
access_log off;
}
}

The problem with this configuration is that, whenever I access https://mydomain.example.com/nextcloud I get redirect to https://mydomain.example.com/app/files, which in turn generates a 404 file not found error.
/var/log/nginx/error.log says:

2018/04/23 15:29:55 [error] 3769#3769: *74 “/var/www/html/apps/files/index.html” is not found (2: No such file or directory), client: 91.206.161.137, server: mydomain.example.com, request: “GET /apps/files/ HTTP/1.1”, host: “mydomain.example.com

So it still takes /var/www/html as the root path, instead of /usr/share/nextcloud. Any ideas how I can solve this?

You need to use nest locations within the location /nextcloud block.
Have a look at the documentation for “nextcloud in a subdir of nginx”:
https://docs.nextcloud.com/server/13/admin_manual/installation/nginx.html#nextcloud-in-a-subdir-of-nginx

Thanks for the reply. The link is helpful, but I’m not sure how I can tell nginx that nextcloud is in a completely different location (on the disk). The configuration assumes that nextcloud is located under /var/www, right? I’m looking for a solution that points to /usr/share/nextcloud. The idea basically is to migrate from apache to nginx on a Centos 7, where I installed nextcloud from repositories with yum. That’s why the path is /usr/share/nextcloud, and not /var/www/nextcloud.
Would I be able to do that using root /usr/share under location ^~ /nextcloud { ?

Yes, that would be correct.