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