Skip to content

Instantly share code, notes, and snippets.

@spikegrobstein
Last active September 22, 2023 04:19
Show Gist options
  • Save spikegrobstein/4384954 to your computer and use it in GitHub Desktop.
Save spikegrobstein/4384954 to your computer and use it in GitHub Desktop.
nginx config for proxying requests for plex over a hostname-based virtualhost.
upstream plex-upstream {
# change plex-server.example.com:32400 to the hostname:port of your plex server.
# this can be "localhost:32400", for instance, if Plex is running on the same server as nginx.
server plex-server.example.com:32400;
}
server {
listen 80;
# server names for this server.
# any requests that come in that match any these names will use the proxy.
server_name
tv
plex
tv.example.com
plex.example.com;
# this is where everything cool happens (you probably don't need to change anything here):
location / {
# if a request to / comes in, 301 redirect to the main plex page.
# but only if it doesn't contain the X-Plex-Device-Name header
# this fixes a bug where you get permission issues when accessing the web dashboard
if ($http_x_plex_device_name = '') {
rewrite ^/$ http://$http_host/web/index.html;
}
# set some headers and proxy stuff.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
# include Host header
proxy_set_header Host $http_host;
# proxy request to plex server
proxy_pass http://plex-upstream;
}
}
@savahu
Copy link

savahu commented Feb 16, 2017

Having some problems with downloading (not streaming) movies from Plex, using this Nginx configuration.
The downloads stop after exactly 1024MB.
Any else tried to download something from their Plex server behind Nginx?

Edit: This is a Plex bug, I think.

https://forums.plex.tv/discussion/256039/download-stopping-at-exactly-1024mb#latest
https://www.reddit.com/r/PleX/comments/5ulg53/downloading_stops_after_1024mb/

@andrewm659
Copy link

So I am having this same problem on latest version of plex and version 1.10.2 of nginx.

Here is my code:

`upstream plex-upstream {
# change plex-server.example.com:32400 to the hostname:port of your plex server.
# this can be "localhost:32400", for instance, if Plex is running on the same server as nginx.
server asm-tv-pc01.me.local:32400;
}

server {
listen 80;

    # server names for this server.
    # any requests that come in that match any these names will use the proxy.
    server_name
            tv
            plex
            tv.example.com
            plex.me.local;

    # this is where everything cool happens (you probably don't need to change anything here):
    location /plex {
            # if a request to / comes in, 301 redirect to the main plex page.
            # but only if it doesn't contain the X-Plex-Device-Name header
            # this fixes a bug where you get permission issues when accessing the web dashboard
            if ($http_x_plex_device_name = '') {
                    #rewrite ^/$ http://$http_host/web/index.html;
                    rewrite /plex(/.*) $1 break;
            }

            # set some headers and proxy stuff.
            # Plex headers
            proxy_set_header X-Plex-Client-Identifier $http_x_plex_client_identifier;
            proxy_set_header X-Plex-Device $http_x_plex_device;
            proxy_set_header X-Plex-Device-Name $http_x_plex_device_name;
            proxy_set_header X-Plex-Platform $http_x_plex_platform;
            proxy_set_header X-Plex-Platform-Version $http_x_plex_platform_version;
            proxy_set_header X-Plex-Product $http_x_plex_product;
            proxy_set_header X-Plex-Token $http_x_plex_token;
            proxy_set_header X-Plex-Version $http_x_plex_version;
            proxy_set_header X-Plex-Nocache $http_x_plex_nocache;
            proxy_set_header X-Plex-Provides $http_x_plex_provides;
            proxy_set_header X-Plex-Device-Vendor $http_x_plex_device_vendor;
            proxy_set_header X-Plex-Model $http_x_plex_model;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;

            # include Host header
            proxy_set_header Host $http_host;
            proxy_set_header        Host                      $server_addr;
            proxy_set_header        Referer                   $server_addr;
            proxy_set_header        Origin                    $server_addr;

            # proxy request to plex server
            proxy_pass http://plex-upstream;
    }

}
`

Getting this in my logs:

2017/03/15 19:37:37 [error] 65282#100149: *1 open() "/usr/local/www/nginx/plex" failed (2: No such file or directory), client: 10.150.1.192, server: localhost, request: "GET /plex HTTP/1.1", host: "nginxtest.me.local"
2017/03/15 19:40:37 [error] 65944#100248: *2 open() "/usr/local/www/nginx/plex" failed (2: No such file or directory), client: 10.150.1.192, server: localhost, request: "GET /plex HTTP/1.1", host: "nginxtest.me.local"
2017/03/15 19:42:21 [error] 66013#100268: *1 open() "/usr/local/www/nginx/plex" failed (2: No such file or directory), client: 10.150.1.192, server: localhost, request: "GET /plex HTTP/1.1", host: "nginxtest.me.local"

@jeaber
Copy link

jeaber commented Dec 6, 2017

thanks

@BeyondEvil
Copy link

Did you ever get it working @andrewm659 ?

@dsculptor
Copy link

dsculptor commented Aug 24, 2021

Update: The original gist without the proxy_set_headers as mentioned in follow-ups works well.
Thanks @spikegrobstein.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment