UP-NextPush / server-app

UnifiedPush provider for Nextcloud - server application - Moved to https://codeberg.org/NextPush/uppush
GNU Affero General Public License v3.0
67 stars 8 forks source link

Compatibility with internal rewrites. #45

Closed Sandelinos closed 1 year ago

Sandelinos commented 1 year ago

The documentation assumes that your Nextcloud server is behind a reverse proxy, which isn't the case for many people. To set up the matrix gateway on a Nextcloud server running on nginx / php-fpm I think something like this configuration should be needed:

location /_matrix/push/v1/notify {
    rewrite ^.*$ /index.php/apps/uppush/gateway/matrix last;
}

This however doesn't work and /_matrix/push/v1/notify just returns a 302 to /login. I dug through nginx debug logs and it seems that for some reason the fastcgi REQUEST_URI parameter (which normally isn't changed by internal rewrites) is being used for determining the path instead of the PATH_INFO parameter so I needed to add a ugly workaround like this:

set $custom_request_uri $request_uri;
location /_matrix/push/v1/notify {
    set $custom_request_uri /index.php/apps/uppush/gateway/matrix;
    rewrite ^.*$ /index.php/apps/uppush/gateway/matrix last;
}

location ~ \.php(?:$|/) {
    ...
    include fastcgi_params;
    fastcgi_param REQUEST_URI $custom_request_uri;
    ...
}

Can this be fixed in the app so a workaround like this wouldn't be needed?

p1gp1g commented 1 year ago

You should use fastcgi_* directives in your case : https://github.com/UP-NextPush/server-app/issues/28#issuecomment-1314904169

I hope some could share their config here https://github.com/UP-NextPush/server-app/tree/main/reverse_proxy_examples

Sandelinos commented 1 year ago

You should use fastcgi_* directives in your case

Not sure how that is relevant. My issue is about URI rewrites needed for the matrix gateway, not timeouts.

p1gp1g commented 1 year ago

Indeed, sorry.

Could you try the following, with break instead of last ?

location /_matrix/push/v1/notify {
    rewrite ^.*$ /index.php/apps/uppush/gateway/matrix break;
}
Sandelinos commented 1 year ago

last is the correct directive in this case. break continues the processing in the same location block and since there is nothing else there, a 404 is returned to the client. With the fastcgi_param REQUEST_URI workaround mentioned in the 2nd half of my original message the rewrite works as expected.

The issue stems from the fact that the app (or maybe nextcloud itself? I couldn't find any references to "request_uri" in the app code.) determines the requested path from the fastcgi REQUEST_URI parameter (which isn't affected by internal rewrites) instead of the PATH_INFO, which is the one that is affected by internal rewrites.

p1gp1g commented 1 year ago

According to the nginx doc it should be break under the location.

You can also try the following in the server block

rewrite ^.*$ /index.php/apps/uppush/gateway/matrix last;

The issue you mention is related to nextcloud internals but it should be possible to resolve it with another nginx conf.

Sandelinos commented 1 year ago

According to the nginx doc it should be break under the location.

Since the php handler is in a seperate location block, last is the correct setting to use.

You can also try the following in the server block

rewrite ^.*$ /index.php/apps/uppush/gateway/matrix last;

Having this configuration in the root of the server block would just rewrite all requests to the uppush matrix gateway path, completely breaking nextcloud.

As mentioned in my original message I already have a working Nginx configuration. I'm simply asking if something could be changed on the app's side so the workaround mentioned in the 2nd half of my original message wouldn't be necessary.

The issue you mention is related to nextcloud internals

This answers my question. Thanks.

p1gp1g commented 1 year ago

You can also try the following in the server block

rewrite ^.*$ /index.php/apps/uppush/gateway/matrix last;

Having this configuration in the root of the server block would just rewrite all requests to the uppush matrix gateway path, completely breaking nextcloud.

Of course, I meant


rewrite ^/_matrix/push/v1/notify$ /index.php/apps/uppush/gateway/matrix last;
p1gp1g commented 1 year ago

Of course, I meant

rewrite ^/_matrix/push/v1/notify$ /index.php/apps/uppush/gateway/matrix last;

Have you tried it ?