Skip to content

Instantly share code, notes, and snippets.

@spikegrobstein
Last active September 22, 2023 04:19
Star You must be signed in to star a gist
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;
}
}
Copy link

ghost commented Apr 23, 2016

I'm not sure what I'm doing wrong, this code doesn't seem to work. I tried Subtletree's combo of hasselman and thomaswebb's code and was met with this message:

nginx: [emerg] "upstream" directive is not allowed here in /usr/local/etc/nginxnginx.conf:1

I even went as far as copying the code directly from the zip and unzipping the file directly in it's place and get the same error message? I'm using nginx-1.8.1_2,2.

I don't know a terrible amount about reverse proxies except that I set up the first one for the majority of the services running in my Freenas box, and I wanted to get this to work for Plex to make it easier to access from everywhere. I will continue to read the documentation about nginx to see if I have something wrong or what have you until I see someone respond. Do I need to install additional packages to get this to work? Thank you for any insight into the matter!

update... got this to work. Problem was missing a lot of necessary code I think... I had to add some of the basic data like worker_processes 1; and some other code; is that code truly necessary for this to work? updated code below:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


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 localhost:32400;
}

server {
    listen 80;

    # server names for this server.
    # any requests that come in that match any these names will use the proxy.
    # replace <host> with actual server name (obviously).
    server_name plex
                tv;

    # 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_set_header X-Plex-Client-Identifier $http_x_plex_client_identifier;

        # include Host header
        proxy_set_header Host $http_host;

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

        proxy_redirect off;
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 36000s;
    }   
}
}

@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