stremio-docker icon indicating copy to clipboard operation
stremio-docker copied to clipboard

nginx

Open tsaridas opened this issue 10 months ago • 18 comments

use nginx to forward traffic to server and web client

  • [ ] figure out how to enable/disable autosetup server url
  • [ ] update readme
  • [ ] proxy timeouts ?
  • [ ] make sure there is backwards compatibility (for https on server )
  • [ ] add tests for autosetup server url

latest code pushed to

ghcr.io/tsaridas/stremio-docker:development

tsaridas avatar Feb 16 '25 15:02 tsaridas

Hey, first thank you for your work :) Is there a release date or something else on when this will be released?

DenuxPlays avatar Apr 11 '25 07:04 DenuxPlays

I have seen some issues with server behind nginx and il not quite sure if it’s because of my environment or something else so until im sure nothing will break I cannot promise something. You could test the image I have im GitHub repo and test yourself .

tsaridas avatar Apr 14 '25 15:04 tsaridas

Since I commented I use this image:

 ghcr.io/tsaridas/stremio-docker:testing@sha256:2143eaaf6468d2a958f3e9d3d9625622fe1bb24d87dc29327d9dfa3508537269

Haven't noticed anything yet

DenuxPlays avatar Apr 14 '25 15:04 DenuxPlays

ghcr.io/tsaridas/stremio-docker:development

tsaridas avatar Apr 22 '25 14:04 tsaridas

I've updated the tag. I noticed that the last commit hasn't been published to the registry maybe you can fix that.

Also is there a list or some issues you noticed?

Updated Tag to:

ghcr.io/tsaridas/stremio-docker:development@sha256:7055123baf0aa7cdea5f06cd0f9a3b4e664a9021c977365dd182ea0ff8f58ebf

DenuxPlays avatar Apr 23 '25 11:04 DenuxPlays

Thanks for checking. I’m not quite sure when I left it off. Main issue I saw was streaming issues and besides video getting downloaded correctly it was hanging from time to time. I’ll try to check if latest image was working correctly and it didn’t have any nginx config issues.

tsaridas avatar Apr 23 '25 13:04 tsaridas

Okay I'll wait for the workflow to finish and hopefully succeed. Tomorrow I'll do some test.

I tried the same using caddy and your "normal" image. It worked fine without any stutters.

DenuxPlays avatar Apr 23 '25 18:04 DenuxPlays

Also is there a list or some issues you noticed?

comparing the headers with nginx and direct server setup

Server: nginx
Date: Wed, 12 Mar 2025 18:34:53 GMT
Content-Type: video/mp4
Content-Length: 3364355
Connection: keep-alive
Accept-Ranges: bytes
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 116846
Content-Type: video/mp4
Date: Wed, 12 Mar 2025 17:03:20 GMT
Keep-Alive: timeout=5

you see that Keep-Alive: timeout=5 is missing from nginx. I belive this might cause the video/audio to stop. This header should be passed from the server directly and not sure why it doesn't pass it when it goes behind nginx. You can also see Access-Control-Allow-Origin being passed but I doubt this causes an issue.

tsaridas avatar Apr 23 '25 18:04 tsaridas

I think I found something:

  1. nginx uses http1.0 for backend connections (I think at least)
  2. nginx reuses tcp connections to the backend

To adress the http issue we can try this:

proxy_http_version 1.1;
# Not sure if this is needed but I saw a few times
proxy_set_header Connection "";

To proxy the keep-alive header:

proxy_pass_header Keep-Alive;

Give me a sec and I'll send you an updated config you can try

DenuxPlays avatar Apr 23 '25 19:04 DenuxPlays

Maybe something like this could fix it?

server {
    listen 80;
    listen 8080;

    server_name _;  # Catch-all server name
    access_log /dev/stdout;
    error_log /dev/stderr warn;

    # Compressions
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_http_version 1.1;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_types
        application/json
        application/javascript
        application/xml
        application/vnd.apple.mpegurl  # HLS playlists
        text/css
        text/javascript
        text/plain
        text/vtt                       # Subtitles
        text/xml;

    proxy_set_header Accept-Encoding "gzip";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Origin $http_origin;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_pass_header Keep-Alive;

    proxy_cache off;
    proxy_buffering off;
    proxy_max_temp_file_size 0;

    limit_req_status 429;
    error_page 401 = @error401;

    location @error401 {
        limit_req zone=auth burst=3 nodelay;
        return 401;
    }

    # Stremio-specific routes
    location ~ ^/(hlsv2|casting|local-addon|proxy|rar|zip|settings|create|removeAll|samples|probe|subtitlesTracks|opensubHash|subtitles|network-info|device-info|get-https|hwaccel-profiler|status|exec|stream|heartbeat|yt|tracks) {
        include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location ~ ^/([^/]+)/(stats\.json|create|remove|destroy|burn) {
        include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location ~ "^/([a-zA-Z0-9]{40})/([0-9]+)$" {
        # doesn't work with auth
        # include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location ~ ^/([^/]+)/([^/]+)/(stats\.json|hls\.m3u8|master\.m3u8|stream\.m3u8|dlna|thumb\.jpg) {
        include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location ~ ^/([^/]+)/([^/]+)/(stream-q-[^/]+\.m3u8|stream-[^/]+\.m3u8|subs-[^/]+\.m3u8|mp4stream-q-[^/]+\.m3u8|mp4stream-q-[^/]+/[^/]+\.mp4) {
        include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location ~ ^/([^/]+)/([^/]+)/(stream-q-[^/]+|stream-[^/]+)/[^/]+\.(ts|mp4) {
        include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location = /(thumb\.jpg|stats\.json) {
        include /etc/nginx/auth.conf;
        proxy_pass http://127.0.0.1:11470;
    }

    location /manifest.json {
        # Somehow this doesn't work well with auth.
        # include /etc/nginx/auth.conf;
        root /srv/stremio-server/build;
    }

    location / {
        include /etc/nginx/auth.conf;
        root /srv/stremio-server/build;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;

        location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
            expires 7d;
            add_header Cache-Control "public, no-transform";
        }
    }
}

I really do not have a lot of experience with nginx...

DenuxPlays avatar Apr 23 '25 19:04 DenuxPlays

We could also try another approach:

  1. Let nginx keep a specific amount of connections idle (like 16 or 32) to stremio
    • this would reduce tcp handshake overhead
  2. send our own keep-alive headers and transmit video and audio segments like this

I am not sure if this will work or can work just an idea I have

DenuxPlays avatar Apr 23 '25 19:04 DenuxPlays

I pushed some new changes, seems to work better for me now. if you could also test as well to make sure there are no issues. thank you for the help

tsaridas avatar Apr 23 '25 19:04 tsaridas

Yeah will do that tomorrow.

Also we could define two global Proxy backends to reduce code duplication but Thats more like a clean up. Just Wanted to mention it

DenuxPlays avatar Apr 23 '25 20:04 DenuxPlays

Yeah will do that tomorrow.

Also we could define two global Proxy backends to reduce code duplication but Thats more like a clean up. Just Wanted to mention it

Lets try to get this working and then I'm happy to discuss any other changes.

tsaridas avatar Apr 23 '25 20:04 tsaridas

I have some test results.

TLDR: Works fine

Long Version: Downloading and streaming is seamless but I've noticed a small jitter in download speeds (around 10%). But I think that is an issue with my server or my internet connection as I have the same issue when downloading the same movie from my nextcloud.

But downloading and streaming are fine. No noticable changes. Even with the jitter the total download time has only increased by 1% so I'll say that's measure tolerance.

DenuxPlays avatar Apr 24 '25 09:04 DenuxPlays

In the last 3days I ran through ~50GB of Data using your docker image. Still no issues

DenuxPlays avatar Apr 27 '25 12:04 DenuxPlays

In the last 3days I ran through ~50GB of Data using your docker image. Still no issues

I figured the issues I had was the my browser. I'll try to get this PR moving.

Any suggestions welcome.

tsaridas avatar Apr 27 '25 13:04 tsaridas

Yeah I would use upstream in the nginx config to reduce code duplication.

Like this:

  1. define a server and frontend like:
   upstream backend {
        server 127.0.0.1:11470;
        keepalive 32;
    }

Here should go all custom configuration we use

This can then be used like:

location ~ ^/ {
            proxy_pass http://backend;
        }

Other then that I currently have none and this just an optional clean-up.

If you have any other issues or I can help in any other way just let me know. :)

DenuxPlays avatar Apr 27 '25 13:04 DenuxPlays

Let me know if I should test anything

I am still using it almost every day

DenuxPlays avatar Jun 26 '25 07:06 DenuxPlays

@DenuxPlays I was just going to sent you a message to test and I saw your message :) I think I'm done. Just one small thing to update the release and nightly pipelines. You can take a look at the whole code if you are up to it. thank you in advance !

tsaridas avatar Jun 26 '25 07:06 tsaridas

image Log messages are wrong

DenuxPlays avatar Jun 26 '25 09:06 DenuxPlays

Log messages are wrong

those are from stremio server not the startup script.

tsaridas avatar Jun 26 '25 09:06 tsaridas

Ahh okay Everything else seems to work

DenuxPlays avatar Jun 26 '25 09:06 DenuxPlays