Nginx – Using NGINX reverse proxy to rewrite path on OwnCloud Docker image half-working

dockernginxowncloudreverse-proxy

So I'm trying to have an instance of OwnCloud running via a docker image behind an nginx https reverse proxy and need to re-write the path via the reverse proxy so that instead of going to https://my-server/ to get to OwnCloud, you use https://my-server/cloud to get to it.

My current reverse-proxy location block looks like this:

location  /cloud {
  rewrite /cloud/(.*) /$1  break;
  proxy_pass         http://localhost:8080;
  proxy_redirect     off;
  proxy_set_header   Host $host;
}

But this only half-works. When I navigate to htts://my-server/cloud, I get an OwnCloud generated 404 error and then if I click 'take me back to OwnCloud', it redirects me to https://my-server/index.php which results in an nginx level 404 because the redirect should go to https://my-server/cloud/index.php.

I'm thinking I may be hosed and need to modify the OwnCloud configurations so it returns links with the /cloud appended but I have no clue how to do that via a docker image. Appreciate any help on this.

Best Answer

I would probably do something like this:

location  /cloud/ {
  proxy_pass         http://localhost:8080/;
}

Note the trailing slash / at the end of location and proxy_pass directives[1]. This is similar to how rsync works, as in, if the trailing slash exists, it will replace everything after. In your case: http://proxy/cloud will request to the upstream http://upstream:8080/ and whatever subdirectory OwnCloud is using, like example, would be requested as: http://proxy/cloud/example and requested as http://upstream:8080/example

You should not need the rewrite if you are catching the /cloud directory before proxying to the upstream.

And remove the other settings, as they are not needed for what you want, unless you are using the header for something, I don't think the redirect is making any difference in your case.

[1] Source: https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/