Skip to content

Service discover doesn't work behind reverse proxy #577

Closed

Description

@funkypenguin

Hey guys,

I'm using NextCloud docker image behind a Traefik SSL-terminating reverse proxy. I found I was unable to use service discovery to configure CalDAV/CardDAV on my iOS device, because the 301 redirect served by https://MYHOST/.well-known/caldav is an http URL (http://MYHOST/remote.php/dav), since the container has no native HTTPS setup.

I've temporarily worked around this by changing the following in .htaccess:

RewriteRule ^\.well-known/carddav /remote.php/dav/ [R=301,L]
RewriteRule ^\.well-known/caldav /remote.php/dav/ [R=301,L]

To:

RewriteRule ^\.well-known/carddav https://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
RewriteRule ^\.well-known/caldav https://%{SERVER_NAME}/remote.php/dav/ [R=301,L]

But I imagine this is not an ideal fix, since .htaccess could be automatically updated during upgrades / app installs.

Can we have a config.php option, tied to the occ maintenance:update:htaccess process, which would automatically insert the necessary https elements to ensure service discovery works? Perhaps something like overwrite.redirect.url = https://MYHOST?

Notes:

  1. Issue cal|carddav not setup properly (.htaccess) #528 is related
  2. I've written a more detailed description of the fix here: https://geek-cookbook.funkypenguin.co.nz/recipes/nextcloud/#use-service-discovery

Thanks!
D

Activity

  1. kesselb commented on Dec 12, 2018

    @kesselb
    Contributor

    I guess you have to adjust some of these values? but i'm not sure if these are considered for the well-known redirect.

    https://docs.nextcloud.com/server/15/admin_manual/configuration_server/reverse_proxy_configuration.html

  2. J0WI commented on Dec 12, 2018

    @J0WI
    Contributor

    I think this could be fixed by #527

  3. kesselb commented on Dec 12, 2018

    @kesselb
    Contributor

    I dont use traefik but if i would go for this solution. Let traefik return the right url sounds like a better way to do it.

    traefik.frontend.redirect.permanent: 'true'
    traefik.frontend.redirect.regex: https://(.*)/.well-known/(card|cal)dav
    traefik.frontend.redirect.replacement: https://$$1/remote.php/dav/
    

    nextcloud/server#11850 (comment) all credits to @pauvos

  4. zroug commented on Dec 15, 2018

    @zroug

    #527 won't fix the issue. (Maybe it is a Nextcloud bug, that it doesn't?)

    I think the correct way to fix the issue without a change to Nextcloud is to add these lines

    ServerName https://cloud.example.com
    ServerAlias *
    UseCanonicalName On
    

    inside of <VirtualHost> in /etc/apache2/sites-available/000-default.conf. cloud.example.com is your host name and https is your protocol.

    UseCanonicalName On tells Apache to use ServerName for rewrites. ServerAlias * is not strictly necessary, but it makes the intention clear, that this virtual host should accept all connections.

    This will work for all rewrites that Nextcloud adds in the future. One could make it configurable using the same variables used in #527.

  5. SysEngDan commented on Jan 29, 2019

    @SysEngDan

    The suggested fix by the OP worked for me. I'm running NC 15.02 container behind an Nginx reverse proxy.

  6. marceljd commented on Feb 9, 2019

    @marceljd
    Contributor

    See #644, it is possible to fix this with changes in the proxy settings, but running Nextcloud Apache on https is the better way.

  7. zroug commented on Feb 9, 2019

    @zroug

    While #644 can solve the issue, it requires to use an additional layer of encryption in a perfectly trusted network. My solution #577 (comment) can tell Apache from where it is accessed on the public network, without the need to access it the same way on the internal network. This solution also doesn't require any changes to the proxy settings or Nextcloud itself.

  8. marceljd commented on Feb 9, 2019

    @marceljd
    Contributor

    @zroug I did not see your solution untill now. I am not an Apache expert, but your solution would mean that all traffic is still unencrypted behind a proxy or not (since the port used is still 80 and the Apache SSL engine is off)? I agree that adding the servername with the real (external) hostname does add some security. The best solution would be to give the proxy the certificates of the Apache instance and let Apache only accept the connections to the servername that should be set to the external address. Or do I miss something?

    The second question I have: would it still be possible to access the server by IP address for users who only need intranet access and do not have internal DNS set up?

    @J0WI @tilosp @SnowMB what do you think? I'd be happy to build this into the PR (#644 ) if everyone thinks that this will be the better solution (or perhaps use both and add the server-name as an environment variable?).

    edit: though the internal network in Docker or Kubernetes is fairly trustable, someone could break out of another container and sniff around. I would not consider an internal network as perfectly trusted. Adding SSL for the internal traffic is not a bad thing to my opinion.

  9. zroug commented on Feb 9, 2019

    @zroug

    Yes that would mean that Apache is still on port 80 and Nextcloud is still accessible from internal ips. It has only an effect on some links, like share links or the dav redirect. Therefore I suggest to integrate that with #527 because that would make it explicit that it is the behavior that you want.

    I certainly think that #644 is a good thing and some additional security dosen't hurt, but it solves a different issue. Sometimes you want to solve this issue independently.

    For example: Traefik still has some issues with self signed certificates. That can make it difficult to use self signed certificates. You suggested to set insecureSkipVerify: true but that would mean that not much security is added because the certificate is not validated and more importantly it means you also skip verification for backends where you want to verify the certificate.

  10. marceljd commented on Feb 10, 2019

    @marceljd
    Contributor

    OK, thnx for the explanation. I do not think both use cases can be solved at once. Besides that it is impossible to solve several upstream issues like all different Traefik discussions about encrypting traffic to the backends. I am aware of those but in general I think it is good to stick to standardized solutions as much as possible for mainstream applications and patching on different locations often leads to other problems in the future.

    Based on this discussion I think it would be wise to implement a second environment variable in #644 that handles the redirect from http to https. The main goal should be to let the client container generate certificates and run on SSL with a default self signed certificate, but redirection is a seperate decision to make, or should at least have an option to disable it. Activating SSL will solve most issues of the security checks but it has certain restrictions. If you do not want or cannot enable SSL, you do not set the environment variable and nothing will change.

    For #527 and #577 the goal should be to patch the server for various issues without activating SSL for those who need it, but I think the general advice should be to activate SSL where possible and let Nextcloud do the configuration-magic it normally does.

  11. SysEngDan commented on Feb 10, 2019

    @SysEngDan

    I think the proper resolution is to treat this issue, #577, as an improvement to NextCloud for reverse proxy support. I believe that most instances where this is being used for a professional use, it is located behind a reverse proxy, whether that's Trafik or Nginx, and the protocol between Trafik and the Apache hosted container is HTTP.

  12. ffried commented on Mar 4, 2019

    @ffried
    Contributor

    For those that have control over the reverse proxy (e.g. an apache 2), the following config lines can fix the issue inside the proxy (as a workaround):

    RewriteEngine On
    RewriteRule ^/\.well-known/carddav https://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
    RewriteRule ^/\.well-known/caldav https://%{SERVER_NAME}/remote.php/dav/ [R=301,L]
    
  13. GuyPaddock commented on Mar 5, 2019

    @GuyPaddock

    I just posted a comment about another option for those using NextCloud on NGinx (even if you are not using NGinx for the proxy): #527 (comment)

  14. kesselb commented on Mar 10, 2019

    @kesselb
    Contributor

    I prepared a pull request for the documentation. Could you share your solutions? nextcloud/documentation#1297

  15. SnowMB commented on May 28, 2019

    @SnowMB
    Contributor

    Thanks @kesselb for pointing that out. #528 and #635 had the same problem.

  16. cdauth commented on Aug 27, 2024

    @cdauth

    Do I understand it right that this issue was not actually fixed, but a workaround has been added to the official documentation?

    Would it be possible to fix the issue, or is that technically not possible? Possible fixes could be:

    • Handle the X-Forwarded-* headers right inside Apache rather than in PHP so that the htaccess rewrite rules use the right protocol (not sure if Apache can do that)
    • or generate the .htaccess file dynamically and make it use the OVERWRITE* environment variables
    • or implement the redirect in PHP rather than in a htaccess rule.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions