Skip to content

Instantly share code, notes, and snippets.

@spikegrobstein
Last active August 9, 2024 13:42
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;
}
}
@doonga
Copy link

doonga commented May 6, 2016

I had to add a few things to get this all working:

        # 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;

The /web redirect was also a problem with the Fire TV, it doesn't send headers, it uses query strings so I had to make some tweaks there too otherwise the Fire TV would say that it couldn't connect.

                # Redirect if not an options request.
                if ($request_method != OPTIONS ) {
                        set $test A;
                }
                if ($http_x_plex_device_name = '') {
                        set $test "${test}B";
                }
                if ($arg_X-Plex-Device-Name = '') {
                        set $test "${test}C";
                }
                if ($test = ABC) {
                        rewrite ^/$ https://$http_host/web/index.html;
                }

Hopefully that helps someone.

@duntonr
Copy link

duntonr commented Sep 14, 2016

Doonga - helped me, thanks!

@shadycuz
Copy link

shadycuz commented Sep 20, 2016

I used Doonga's Headers but I'm not sure if it mattered. I used the orginal config in this gist but had to change proxy_pass http://plex-upstream; to proxy_pass http://plex-upstream/;

Notice the trailing / at the end? That made it work for me. Now when ever any one visits my URL they get plex and do not have to enter the port or the url.

@elvetemedve
Copy link

This configuration forces me to log into Plex account from LAN. So I can reach the Web GUI only from localhost. I'm running Plex Server 1.1.4.2757.
Am I the only one having this weird thing going on?

@Subtletree
Copy link

Had some problems setting this up for plex, couchpotato, sabnzbd etc.

Problems seem to have been fixed by changing:

upstream plex-upstream {
    server localhost:32400;
}

to:

upstream plex-upstream {
    server 127.0.0.1:32400;
}

@ometa
Copy link

ometa commented Jan 3, 2017

Hey all,

After a few hours of hacking and testing, I was able to get Plex Media Server v1.3.3.3148 working behind an NGINX reverse proxy on my local LAN, without it prompting for any authentication. Here is my gist with the configuration.

The key was the following configuration, which forces plex to see the request as coming from an IP in the whitelisted network:

    proxy_set_header        Host                      $server_addr;
    proxy_set_header        Referer                   $server_addr;
    proxy_set_header        Origin                    $server_addr; 

Hope this helps others!

Devin

@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