nextcloud / cms_pico

🗃 Integrate Pico CMS and let your users manage their own websites
https://apps.nextcloud.com/apps/cms_pico
GNU Affero General Public License v3.0
137 stars 43 forks source link

Short URLs return Timeout after some time #226

Closed Greek64 closed 1 year ago

Greek64 commented 1 year ago

Disclaimer: I have opened this as an issue although I have found and fixed my issue in order to provide feedback and maybe improve the default nginx settings, or at least improve the documentation.

Problem

So, I had this problem that after some unspecified amount of time, all short URLs were returning 504 timeout, while the long URLs were working fine. After bringing the nextcloud instance down and back up again they worked again for an amount of time. I use nginx's proxy_pass method to get the short URLs.

I finally got some time to find the problem. Turns out that the domains specified in the proxy_pass directive are only resolved during nginx start (or reload) and are cached, meaning that if the upstream IP address changes, nginx tries to proxy to a non existent server. And since I use DDNS domains for my public facing IP, this happens quite often.

Solution

You can "force" nginx to do a lookup on the domain by using variables, but this also changes the default "rewrite" behavior, which means that we have to also manually rewrite the url. So I changed the suggested location block from:

location ^~ /sites/ {
    proxy_set_header X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Server $host;
    proxy_pass https://example.com/index.php/apps/cms_pico/pico_proxy/;
    proxy_ssl_server_name on;
}

to:

location ^~ /sites/ {
    resolver 8.8.8.8;
    proxy_ssl_server_name on;
    proxy_set_header X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Server $host;
    set $proxy_pass_url https://example.com;
    rewrite ^/sites/(.*)$ /index.php/apps/cms_pico/pico/$1 break;
    proxy_pass $proxy_pass_url;
}

The resolver directive is the address of the DNS server which will resolve the domain name.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1: