Repository: paperless-ngx/paperless-ngx

Issue:
[Bug] Websockets do not respect subpath
### Discussed in https://github.com/paperless-ngx/paperless-ngx/discussions/2236

<div type='discussions-op-text'>

<sup>Originally posted by **davidgiga1993** December 21, 2022</sup>
I migrated from paprless-ng to paperless-ngx and the websocket connection isn't working anymore:
```
WebSocket connection to 'wss://home.dev-core.org/paperless/ws/status/' failed:
```
The server returns status code 500.

I'm using `/paperless` as path and configured it according to the [documentation](https://docs.paperless-ngx.com/configuration/#hosting-and-security)
```
PAPERLESS_FORCE_SCRIPT_NAME=/paperless
PAPERLESS_STATIC_URL=/paperless/static/
PAPERLESS_MEDIA_URL=/paperless/media/

PAPERLESS_URL=https://my-domain.com
```

my nginx config hasn't changed since and worked fine before:
```
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Uri $request_uri;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;

client_body_buffer_size 128k;
proxy_next_upstream error timeout;
proxy_redirect  http://  $scheme://;
proxy_http_version 1.1;
proxy_cache_bypass $cookie_session;
proxy_no_cache $cookie_session;
proxy_buffers 64 256k;

# Allow websockets
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

real_ip_header X-Forwarded-For;
real_ip_recursive on;

send_timeout 5m;
proxy_read_timeout 360;
proxy_send_timeout 360;
proxy_connect_timeout 360;
```

It looks like paperless ngx isn't using the context path for the websockets - here is a test using curl. The first request (with the correct path) returns `500`, the second one without any context path returns correctly `403`
```
nas paperless # curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: my-domain.com" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" -H "Sec-WebSocket-Version: 13" localhost:8000/paperless/ws/status/
HTTP/1.1 500 Internal Server Error
content-type: text/plain; charset=utf-8
content-length: 21
connection: close

Internal Server Error#
nas paperless # curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: my-domain.com" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" -H "Sec-WebSocket-Version: 13" localhost:8000/ws/status/
HTTP/1.1 403 Forbidden
Date: Sun, 11 Dec 2022 10:03:35 GMT
Server: Python/3.9 websockets/10.4
Content-Length: 0
Content-Type: text/plain
Connection: close
```

any ideas? Anything else I should test?
    </div>


Discussion:
@davidgiga1993 can you tell me what the `<base>` tag is set to in your index page? E.g. `<base href="/">` in the HTML source
> @davidgiga1993 can you tell me what the `<base>` tag is set to in your index page? E.g. `<base href="/">` in the HTML source

Sure:
```
<base href="/paperless/">
```
So the entire frontend works fine, just the websocket part in the backend doesn't handle the subpath.
Adding that path to https://github.com/paperless-ngx/paperless-ngx/blob/384a11867259d0eeeb3a36f5d9b7e0f653486f86/src/paperless/urls.py#L157 seems to fix this, presumably `settings.BASE_URL` needs to be included?
@tribut happy new year, any interest in looking more into this one when you have a sec? :)

Resolving patch:
diff --git a/src/paperless/urls.py b/src/paperless/urls.py
--- a/src/paperless/urls.py
+++ b/src/paperless/urls.py
@@ -154,7 +154,7 @@
 
 
 websocket_urlpatterns = [
-    re_path(r"ws/status/$", StatusConsumer.as_asgi()),
+    path(settings.BASE_URL.lstrip("/") + "ws/status/", StatusConsumer.as_asgi()),
 ]
 
 # Text in each page's <h1> (and above login form).


Test patch: