]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - support/nginx/peertube
Resumable video uploads (#3933)
[github/Chocobozzz/PeerTube.git] / support / nginx / peertube
1 # Minimum Nginx version required: 1.13.0 (released Apr 25, 2017)
2 # Please check your Nginx installation features the following modules via 'nginx -V':
3 # STANDARD HTTP MODULES: Core, Proxy, Rewrite, Access, Gzip, Headers, HTTP/2, Log, Real IP, SSL, Thread Pool, Upstream, AIO Multithreading.
4 # THIRD PARTY MODULES: None.
5
6 server {
7 listen 80;
8 listen [::]:80;
9 server_name ${WEBSERVER_HOST};
10
11 location /.well-known/acme-challenge/ {
12 default_type "text/plain";
13 root /var/www/certbot;
14 }
15 location / { return 301 https://$host$request_uri; }
16 }
17
18 upstream backend {
19 server ${PEERTUBE_HOST};
20 }
21
22 server {
23 listen 443 ssl http2;
24 listen [::]:443 ssl http2;
25 server_name ${WEBSERVER_HOST};
26
27 access_log /var/log/nginx/peertube.access.log; # reduce I/0 with buffer=10m flush=5m
28 error_log /var/log/nginx/peertube.error.log;
29
30 ##
31 # Certificates
32 # you need a certificate to run in production. see https://letsencrypt.org/
33 ##
34 ssl_certificate /etc/letsencrypt/live/${WEBSERVER_HOST}/fullchain.pem;
35 ssl_certificate_key /etc/letsencrypt/live/${WEBSERVER_HOST}/privkey.pem;
36
37 location ^~ '/.well-known/acme-challenge' {
38 default_type "text/plain";
39 root /var/www/certbot;
40 }
41
42 ##
43 # Security hardening (as of Nov 15, 2020)
44 # based on Mozilla Guideline v5.6
45 ##
46
47 ssl_protocols TLSv1.2 TLSv1.3;
48 ssl_prefer_server_ciphers on;
49 ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256; # add ECDHE-RSA-AES256-SHA if you want compatibility with Android 4
50 ssl_session_timeout 1d; # defaults to 5m
51 ssl_session_cache shared:SSL:10m; # estimated to 40k sessions
52 ssl_session_tickets off;
53 ssl_stapling on;
54 ssl_stapling_verify on;
55 # HSTS (https://hstspreload.org), requires to be copied in 'location' sections that have add_header directives
56 #add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
57
58 ##
59 # Application
60 ##
61
62 location @api {
63 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
64 proxy_set_header Host $host;
65 proxy_set_header X-Real-IP $remote_addr;
66
67 client_max_body_size 100k; # default is 1M
68
69 proxy_connect_timeout 10m;
70 proxy_send_timeout 10m;
71 proxy_read_timeout 10m;
72 send_timeout 10m;
73
74 proxy_pass http://backend;
75 }
76
77 location / {
78 try_files /dev/null @api;
79 }
80
81 location = /api/v1/videos/upload-resumable {
82 client_max_body_size 0;
83 proxy_request_buffering off;
84
85 try_files /dev/null @api;
86 }
87
88 location = /api/v1/videos/upload {
89 limit_except POST HEAD { deny all; }
90
91 # This is the maximum upload size, which roughly matches the maximum size of a video file.
92 # Note that temporary space is needed equal to the total size of all concurrent uploads.
93 # This data gets stored in /var/lib/nginx by default, so you may want to put this directory
94 # on a dedicated filesystem.
95 client_max_body_size 12G; # default is 1M
96 add_header X-File-Maximum-Size 8G always; # inform backend of the set value in bytes before mime-encoding (x * 1.4 >= client_max_body_size)
97
98 try_files /dev/null @api;
99 }
100
101 location ~ ^/api/v1/(videos|video-playlists|video-channels|users/me) {
102 client_max_body_size 3M; # default is 1M
103 add_header X-File-Maximum-Size 2M always; # inform backend of the set value in bytes before mime-encoding (x * 1.4 >= client_max_body_size)
104
105 try_files /dev/null @api;
106 }
107
108 ##
109 # Websocket
110 ##
111
112 location @api_websocket {
113 proxy_http_version 1.1;
114 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
115 proxy_set_header Host $host;
116 proxy_set_header X-Real-IP $remote_addr;
117 proxy_set_header Upgrade $http_upgrade;
118 proxy_set_header Connection "upgrade";
119
120 proxy_pass http://backend;
121 }
122
123 location /socket.io {
124 try_files /dev/null @api_websocket;
125 }
126
127 location /tracker/socket {
128 # Peers send a message to the tracker every 15 minutes
129 # Don't close the websocket before then
130 proxy_read_timeout 15m; # default is 60s
131
132 try_files /dev/null @api_websocket;
133 }
134
135 ##
136 # Performance optimizations
137 # For extra performance please refer to https://github.com/denji/nginx-tuning
138 ##
139
140 root /var/www/peertube/storage;
141
142 # Enable compression for JS/CSS/HTML, for improved client load times.
143 # It might be nice to compress JSON/XML as returned by the API, but
144 # leaving that out to protect against potential BREACH attack.
145 gzip on;
146 gzip_vary on;
147 gzip_types # text/html is always compressed by HttpGzipModule
148 text/css
149 application/javascript
150 font/truetype
151 font/opentype
152 application/vnd.ms-fontobject
153 image/svg+xml;
154 gzip_min_length 1000; # default is 20 bytes
155 gzip_buffers 16 8k;
156 gzip_comp_level 2; # default is 1
157
158 client_body_timeout 30s; # default is 60
159 client_header_timeout 10s; # default is 60
160 send_timeout 10s; # default is 60
161 keepalive_timeout 10s; # default is 75
162 resolver_timeout 10s; # default is 30
163 reset_timedout_connection on;
164 proxy_ignore_client_abort on;
165
166 tcp_nopush on; # send headers in one piece
167 tcp_nodelay on; # don't buffer data sent, good for small data bursts in real time
168
169 # If you have a small /var/lib partition, it could be interesting to store temp nginx uploads in a different place
170 # See https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_temp_path
171 #client_body_temp_path /var/www/peertube/storage/nginx/;
172
173 # Bypass PeerTube for performance reasons. Optional.
174 # Should be consistent with client-overrides assets list in /server/controllers/client.ts
175 location ~ ^/client/(assets/images/(icons/icon-36x36\.png|icons/icon-48x48\.png|icons/icon-72x72\.png|icons/icon-96x96\.png|icons/icon-144x144\.png|icons/icon-192x192\.png|icons/icon-512x512\.png|logo\.svg|favicon\.png))$ {
176 add_header Cache-Control "public, max-age=31536000, immutable"; # Cache 1 year
177
178 root /var/www/peertube;
179
180 try_files /storage/client-overrides/$1 /peertube-latest/client/dist/$1 @api;
181 }
182
183 # Bypass PeerTube for performance reasons. Optional.
184 location ~ ^/client/(.*\.(js|css|png|svg|woff2|otf|ttf|woff|eot))$ {
185 add_header Cache-Control "public, max-age=31536000, immutable"; # Cache 1 year
186
187 alias /var/www/peertube/peertube-latest/client/dist/$1;
188 }
189
190 # Bypass PeerTube for performance reasons. Optional.
191 location ~ ^/static/(thumbnails|avatars)/ {
192 if ($request_method = 'OPTIONS') {
193 add_header Access-Control-Allow-Origin '*';
194 add_header Access-Control-Allow-Methods 'GET, OPTIONS';
195 add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
196 add_header Access-Control-Max-Age 1728000; # Preflight request can be cached 20 days
197 add_header Content-Type 'text/plain charset=UTF-8';
198 add_header Content-Length 0;
199 return 204;
200 }
201
202 add_header Access-Control-Allow-Origin '*';
203 add_header Access-Control-Allow-Methods 'GET, OPTIONS';
204 add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
205 add_header Cache-Control "public, max-age=7200"; # Cache response 2 hours
206
207 rewrite ^/static/(.*)$ /$1 break;
208
209 try_files $uri @api;
210 }
211
212 # Bypass PeerTube for performance reasons. Optional.
213 location ~ ^/static/(webseed|redundancy|streaming-playlists)/ {
214 limit_rate_after 5M;
215
216 # Clients usually have 4 simultaneous webseed connections, so the real limit is 3MB/s per client
217 set $peertube_limit_rate 800k;
218
219 # Increase rate limit in HLS mode, because we don't have multiple simultaneous connections
220 if ($request_uri ~ -fragmented.mp4$) {
221 set $peertube_limit_rate 5M;
222 }
223
224 # Use this line with nginx >= 1.17.0
225 #limit_rate $peertube_limit_rate;
226 # Or this line if your nginx < 1.17.0
227 set $limit_rate $peertube_limit_rate;
228
229 if ($request_method = 'OPTIONS') {
230 add_header Access-Control-Allow-Origin '*';
231 add_header Access-Control-Allow-Methods 'GET, OPTIONS';
232 add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
233 add_header Access-Control-Max-Age 1728000; # Preflight request can be cached 20 days
234 add_header Content-Type 'text/plain charset=UTF-8';
235 add_header Content-Length 0;
236 return 204;
237 }
238
239 if ($request_method = 'GET') {
240 add_header Access-Control-Allow-Origin '*';
241 add_header Access-Control-Allow-Methods 'GET, OPTIONS';
242 add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
243
244 # Don't spam access log file with byte range requests
245 access_log off;
246 }
247
248 # Enabling the sendfile directive eliminates the step of copying the data into the buffer
249 # and enables direct copying data from one file descriptor to another.
250 sendfile on;
251 sendfile_max_chunk 1M; # prevent one fast connection from entirely occupying the worker process. should be > 800k.
252 aio threads;
253
254 # Use this in tandem with fuse-mounting i.e. https://docs.joinpeertube.org/admin-remote-storage
255 # to serve files directly from a public bucket without proxying.
256 # Assumes you have buckets named after the storage subdirectories, i.e. 'videos', 'redundancy', etc.
257 #set $cdn <your S3-compatiable bucket public url mounted via fuse>;
258 #rewrite ^/static/webseed/(.*)$ $cdn/videos/$1 redirect;
259 #rewrite ^/static/(.*)$ $cdn/$1 redirect;
260 rewrite ^/static/webseed/(.*)$ /videos/$1 break;
261 rewrite ^/static/(.*)$ /$1 break;
262
263 try_files $uri @api;
264 }
265 }