Alta Behind nginx (X-Forwarded-For)
When nginx or another reverse proxy stands in front of Alta, every request comes to Alta from the proxy address. The proxy passes the viewer address in the X-Forwarded-For header. This article shows how to set up nginx and Alta so that sessions and authorization use the viewer address.
This article describes Alta 261020 and newer.
How it works
Section titled “How it works”- A viewer requests
http://tv.example.com/news/index.m3u8. - nginx forwards the request to Alta and adds the header
X-Forwarded-For: <viewer address>. - Alta checks that the request came from an address in the X-Forwarded-For IP list of the OTT server.
- If it did, Alta uses the address from the header as the viewer address: on the Sessions page and in the authorizers.
Alta reads the header once, when a viewer opens the channel playlist: index.m3u8 or an archive link from URL Format. Media playlists and segments do not check the address. WebRTC (WHEP) requests do not use the header: the authorizers see the proxy address.
Set up nginx
Section titled “Set up nginx”server { listen 80; server_name tv.example.com;
location / { proxy_pass http://127.0.0.1:8102; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; }}8102: the port of the Alta OTT server.X-Forwarded-For $remote_addr: Alta accepts exactly one address in the header. Do not use$proxy_add_x_forwarded_for: if the request already has the header, nginx makes a list such as203.0.113.7, 198.51.100.20. Alta ignores a list, and the session gets the nginx address.Host: Alta writes full links into playlists, built from theHostheader, for examplehttp://tv.example.com/news/.... Without this line the links point to127.0.0.1:8102, and players cannot open them.X-Forwarded-Proto: needed when nginx accepts HTTPS. Alta then writeshttps://links into playlists.
A CDN in front of nginx
Section titled “A CDN in front of nginx”If a CDN or another proxy stands in front of nginx, $remote_addr holds the CDN address. Let the nginx realip module take the viewer address from the CDN header:
set_real_ip_from 192.0.2.0/24; # CDN addressesreal_ip_header X-Forwarded-For;real_ip_recursive on;After that $remote_addr holds the viewer address, and the configuration above passes it to Alta.
Set up Alta
Section titled “Set up Alta”- Open Settings → HTTP Servers and edit the OTT Server.
- In X-Forwarded-For IP, click Add IP and enter the nginx address as Alta sees it. For nginx on the same server, enter
127.0.0.1. A network is accepted too, for example10.0.0.0/24. - If nginx runs on the same server, set Default Interface to
localhost. Viewers then cannot reach the Alta port directly, past nginx. - Save the settings.
The API Server has its own X-Forwarded-For IP list. There it changes only the client address in the API request log.
Requests from other addresses
Section titled “Requests from other addresses”After you add the proxy addresses, a request with one address in X-Forwarded-For from an address outside the list is handled like this:
- the channel has an authorizer: Alta ignores the header and checks the connection address;
- the channel has no authorizer: Alta answers
403 Forbiddenand writes to the service log:Real IP check failed for path ... from IP ..., X-Forwarded-For: ....
Authorization behind a proxy
Section titled “Authorization behind a proxy”A request from a trusted proxy carries two addresses: the connection address (nginx) and the viewer address from the header.
- IP: a rule applies when it matches either address. A rule for the nginx address, for example
allow 127.0.0.1, matches every viewer. Write rules for viewer addresses only. - Securetoken: Alta accepts a token made for either address. Make tokens for the viewer address.
- HTTP Backend: the
X-Real-Ipheader holds the viewer address.
Check the setup
Section titled “Check the setup”-
Request the playlist through nginx from another computer:
Terminal window curl -s http://tv.example.com/news/index.m3u8The links in the answer start with
http://tv.example.com/. -
Open the channel in a player and look at the Sessions page: the IP column shows the viewer address, not the nginx address.