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