Nginx putting /nextcloud after my url recursively

I've been wrecking my head trying to get my 90& self-hosted life online and it's going great so far, except for Nextcloud.

I've set up several passes in nginx before, all working perfectly fine after a little tweaking, but despite trying out multiple ways to set this up I get 1 of 2 errors every time.

1. I get a 502 bad gateway most often.

2. I get an internal server error and the url looking like this https://cloud.domain.com/nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud

Here is the relevant part of my nginx file:

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

server {
server_name cloud.domain.com;
listen 80 default_server;
location ^~ /.well-known/acme-challenge {
proxy_pass http://127.0.0.1:81;
proxy_set_header Host $host;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
server_name cloud.domain.com;
listen 443 ssl http2 default_server;
root /var/www/nextcloud/;
access_log /var/log/nginx/nextcloud.access.log;
error_log /var/log/nginx/nextcloud.error.log;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location = /.well-known/carddav {
return 301 $scheme://$host/remote.php/dav;
}
location = /.well-known/caldav {
return 301 $scheme://$host/remote.php/dav;
}
#SOCIAL app enabled? Please uncomment the following three rows
#rewrite ^/.well-known/webfinger /public.php?service=webfinger last;
#rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
#rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
client_max_body_size 10240M;
location / {
rewrite ^ /index.php$request_uri;
}
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location ~ \.(?:flv|mp4|mov|m4a)$ {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
include php_optimization.conf;
fastcgi_pass php-handler;
fastcgi_param HTTPS on;
}
location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
include php_optimization.conf;
fastcgi_pass php-handler;
fastcgi_param HTTPS on;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
try_files $uri/ =404;
index index.php;
}
location ~ \.(?:css|js|woff2?|svg|gif|png|html|ttf|ico|jpg|jpeg)$ {
try_files $uri /index.php$request_uri;
access_log off;
expires 360d;
}


	ssl on;
        ssl_certificate /etc/letsencrypt/live/cloud.domain.com-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/cloud.domain.com-0001/privkey.pem; # managed by Certbot

}


And my most recent log file after attempting to set it up (mostly useless, due to the obvious URL issue):

2019/02/11 19:28:07 [error] 14794#14794: *2 open() "/var/www/nextcloud/index.php/nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud" failed (20: Not a directory), client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud HTTP/2.0", host: "cloud.domain.com"
2019/02/11 19:28:27 [error] 14794#14794: *36 directory index of "/var/www/" is forbidden, client: 212.237.135.59, server: cloud.domain.com, request: "GET / HTTP/2.0", host: "deluge.domain.com"
2019/02/11 19:28:27 [error] 14794#14794: *36 open() "/var/www/favicon.ico" failed (2: No such file or directory), client: 212.237.135.59, server: cloud.domain.com, request: "GET /favicon.ico HTTP/2.0", host: "deluge.domain.com"
2019/02/11 20:36:18 [error] 2985#2985: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "cloud.domain.com"
2019/02/11 20:36:33 [error] 2985#2985: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "cloud.domain.com"
2019/02/11 20:36:44 [error] 2985#2985: *7 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloud HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "domain.com"
2019/02/11 20:36:44 [error] 2985#2985: *7 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /favicon.ico HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "domain.com"
2019/02/11 20:36:52 [error] 2985#2985: *10 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 34.216.155.175, server: cloud.domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "95.216.117.49"
2019/02/11 20:41:32 [error] 2985#2985: *12 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloud HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "domain.com"
2019/02/11 20:53:28 [error] 2985#2985: *17 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "cloud.domain.com"
2019/02/11 20:55:45 [error] 4169#4169: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Class 'Memcached' not found in /var/www/nextcloud/config/config.php:1197
Stack trace:
#0 /var/www/nextcloud/lib/private/Config.php(214): include()
#1 /var/www/nextcloud/lib/private/Config.php(63): OC\Config->readData()
#2 /var/www/nextcloud/lib/base.php(140): OC\Config->__construct('/var/www/nextcl...')
#3 /var/www/nextcloud/lib/base.php(591): OC::initPaths()
#4 /var/www/nextcloud/lib/base.php(1068): OC::init()
#5 /var/www/nextcloud/index.php(40): require_once('/var/www/nextcl...')
#6 {main}
  thrown in /var/www/nextcloud/config/config.php on line 1197" while reading upstream, client: 212.237.135.59, server: cloud.domain.com, request: "GET /nextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloudnextcloud HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "cloud.domain.com"

here https://www.c-rieger.de/ you find a working config for nextcloud and nginx. you may compare your nginx config with Carstens.

and here https://github.com/ReinerNippes/nextcloud you find a working ansible playbook to setup this config. saves you some typing. :wink:

I’ve attempted to use that one, and it does correctly solve the recursive url issue. But it does have the issue with 502 bad gateway that I’ve been unsuccessful in solving as well :frowning:

Thanks for your reply and time btw!

it looks like you didn’t configure memcache/redis.

and i think it’s better to use the unix socket connection to connect to php. the path and name of php7.x-fpm.sock socket depends on your linux/installation.

I’ve attempted to follow your guide, and everything checks out. Except that it stills recurses the url to nextcloudnextcloudnextcloud… and so on.

Any idea what might cause that specific issue?

sorry. no idea.

if you try to setup nextcloud on a vps you may want to try one of my playbooks. they will setup everything for you.