peertube 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. server {
  6. listen 80;
  7. listen [::]:80;
  8. server_name ${WEBSERVER_HOST};
  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. }
  15. upstream backend {
  16. server ${PEERTUBE_HOST};
  17. }
  18. server {
  19. listen 443 ssl http2;
  20. listen [::]:443 ssl http2;
  21. server_name ${WEBSERVER_HOST};
  22. access_log /var/log/nginx/peertube.access.log; # reduce I/0 with buffer=10m flush=5m
  23. error_log /var/log/nginx/peertube.error.log;
  24. ##
  25. # Certificates
  26. # you need a certificate to run in production. see https://letsencrypt.org/
  27. ##
  28. ssl_certificate /etc/letsencrypt/live/${WEBSERVER_HOST}/fullchain.pem;
  29. ssl_certificate_key /etc/letsencrypt/live/${WEBSERVER_HOST}/privkey.pem;
  30. location ^~ '/.well-known/acme-challenge' {
  31. default_type "text/plain";
  32. root /var/www/certbot;
  33. }
  34. ##
  35. # Security hardening (as of Nov 15, 2020)
  36. # based on Mozilla Guideline v5.6
  37. ##
  38. ssl_protocols TLSv1.2 TLSv1.3;
  39. ssl_prefer_server_ciphers on;
  40. 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
  41. ssl_session_timeout 1d; # defaults to 5m
  42. ssl_session_cache shared:SSL:10m; # estimated to 40k sessions
  43. ssl_session_tickets off;
  44. ssl_stapling on;
  45. ssl_stapling_verify on;
  46. # HSTS (https://hstspreload.org), requires to be copied in 'location' sections that have add_header directives
  47. #add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  48. ##
  49. # Application
  50. ##
  51. location @api {
  52. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  53. proxy_set_header Host $host;
  54. proxy_set_header X-Real-IP $remote_addr;
  55. client_max_body_size 100k; # default is 1M
  56. proxy_connect_timeout 10m;
  57. proxy_send_timeout 10m;
  58. proxy_read_timeout 10m;
  59. send_timeout 10m;
  60. proxy_pass http://backend;
  61. }
  62. location / {
  63. try_files /dev/null @api;
  64. }
  65. location = /api/v1/videos/upload-resumable {
  66. client_max_body_size 0;
  67. proxy_request_buffering off;
  68. try_files /dev/null @api;
  69. }
  70. location ~ ^/api/v1/videos/(upload|([^/]+/studio/edit))$ {
  71. limit_except POST HEAD { deny all; }
  72. # This is the maximum upload size, which roughly matches the maximum size of a video file.
  73. # Note that temporary space is needed equal to the total size of all concurrent uploads.
  74. # This data gets stored in /var/lib/nginx by default, so you may want to put this directory
  75. # on a dedicated filesystem.
  76. client_max_body_size 12G; # default is 1M
  77. 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)
  78. try_files /dev/null @api;
  79. }
  80. location ~ ^/api/v1/(videos|video-playlists|video-channels|users/me) {
  81. client_max_body_size 6M; # default is 1M
  82. add_header X-File-Maximum-Size 4M always; # inform backend of the set value in bytes before mime-encoding (x * 1.4 >= client_max_body_size)
  83. try_files /dev/null @api;
  84. }
  85. ##
  86. # Websocket
  87. ##
  88. location @api_websocket {
  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. proxy_pass http://backend;
  96. }
  97. location /socket.io {
  98. try_files /dev/null @api_websocket;
  99. }
  100. location /tracker/socket {
  101. # Peers send a message to the tracker every 15 minutes
  102. # Don't close the websocket before then
  103. proxy_read_timeout 15m; # default is 60s
  104. try_files /dev/null @api_websocket;
  105. }
  106. # Plugin websocket routes
  107. location ~ ^/plugins/[^/]+(/[^/]+)?/ws/ {
  108. try_files /dev/null @api_websocket;
  109. }
  110. ##
  111. # Performance optimizations
  112. # For extra performance please refer to https://github.com/denji/nginx-tuning
  113. ##
  114. root /var/www/peertube/storage;
  115. # Enable compression for JS/CSS/HTML, for improved client load times.
  116. # It might be nice to compress JSON/XML as returned by the API, but
  117. # leaving that out to protect against potential BREACH attack.
  118. gzip on;
  119. gzip_vary on;
  120. gzip_types # text/html is always compressed by HttpGzipModule
  121. text/css
  122. application/javascript
  123. font/truetype
  124. font/opentype
  125. application/vnd.ms-fontobject
  126. image/svg+xml;
  127. gzip_min_length 1000; # default is 20 bytes
  128. gzip_buffers 16 8k;
  129. gzip_comp_level 2; # default is 1
  130. client_body_timeout 30s; # default is 60
  131. client_header_timeout 10s; # default is 60
  132. send_timeout 10s; # default is 60
  133. keepalive_timeout 10s; # default is 75
  134. resolver_timeout 10s; # default is 30
  135. reset_timedout_connection on;
  136. proxy_ignore_client_abort on;
  137. tcp_nopush on; # send headers in one piece
  138. tcp_nodelay on; # don't buffer data sent, good for small data bursts in real time
  139. # If you have a small /var/lib partition, it could be interesting to store temp nginx uploads in a different place
  140. # See https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_temp_path
  141. #client_body_temp_path /var/www/peertube/storage/nginx/;
  142. # Bypass PeerTube for performance reasons. Optional.
  143. # Should be consistent with client-overrides assets list in /server/controllers/client.ts
  144. 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|default-playlist\.jpg|default-avatar-account\.png|default-avatar-account-48x48\.png|default-avatar-video-channel\.png|default-avatar-video-channel-48x48\.png))$ {
  145. add_header Cache-Control "public, max-age=31536000, immutable"; # Cache 1 year
  146. root /var/www/peertube;
  147. try_files /storage/client-overrides/$1 /peertube-latest/client/dist/$1 @api;
  148. }
  149. # Bypass PeerTube for performance reasons. Optional.
  150. location ~ ^/client/(.*\.(js|css|png|svg|woff2|otf|ttf|woff|eot))$ {
  151. add_header Cache-Control "public, max-age=31536000, immutable"; # Cache 1 year
  152. alias /var/www/peertube/peertube-latest/client/dist/$1;
  153. }
  154. # Bypass PeerTube for performance reasons. Optional.
  155. location ~ ^/static/(thumbnails|avatars)/ {
  156. if ($request_method = 'OPTIONS') {
  157. add_header Access-Control-Allow-Origin '*';
  158. add_header Access-Control-Allow-Methods 'GET, OPTIONS';
  159. add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  160. add_header Access-Control-Max-Age 1728000; # Preflight request can be cached 20 days
  161. add_header Content-Type 'text/plain charset=UTF-8';
  162. add_header Content-Length 0;
  163. return 204;
  164. }
  165. add_header Access-Control-Allow-Origin '*';
  166. add_header Access-Control-Allow-Methods 'GET, OPTIONS';
  167. add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  168. add_header Cache-Control "public, max-age=7200"; # Cache response 2 hours
  169. rewrite ^/static/(.*)$ /$1 break;
  170. try_files $uri @api;
  171. }
  172. location ~ ^(/static/(webseed|streaming-playlists)/private/)|^/download {
  173. # We can't rate limit a try_files directive, so we need to duplicate @api
  174. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  175. proxy_set_header Host $host;
  176. proxy_set_header X-Real-IP $remote_addr;
  177. proxy_limit_rate 5M;
  178. proxy_pass http://backend;
  179. }
  180. # Bypass PeerTube for performance reasons. Optional.
  181. location ~ ^/static/(webseed|redundancy|streaming-playlists)/ {
  182. limit_rate_after 5M;
  183. # Clients usually have 4 simultaneous webseed connections, so the real limit is 3MB/s per client
  184. set $peertube_limit_rate 800k;
  185. # Increase rate limit in HLS mode, because we don't have multiple simultaneous connections
  186. if ($request_uri ~ -fragmented.mp4$) {
  187. set $peertube_limit_rate 5M;
  188. }
  189. # Use this line with nginx >= 1.17.0
  190. limit_rate $peertube_limit_rate;
  191. # Or this line with nginx < 1.17.0
  192. # set $limit_rate $peertube_limit_rate;
  193. if ($request_method = 'OPTIONS') {
  194. add_header Access-Control-Allow-Origin '*';
  195. add_header Access-Control-Allow-Methods 'GET, OPTIONS';
  196. add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  197. add_header Access-Control-Max-Age 1728000; # Preflight request can be cached 20 days
  198. add_header Content-Type 'text/plain charset=UTF-8';
  199. add_header Content-Length 0;
  200. return 204;
  201. }
  202. if ($request_method = 'GET') {
  203. add_header Access-Control-Allow-Origin '*';
  204. add_header Access-Control-Allow-Methods 'GET, OPTIONS';
  205. add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  206. # Don't spam access log file with byte range requests
  207. access_log off;
  208. }
  209. # Enabling the sendfile directive eliminates the step of copying the data into the buffer
  210. # and enables direct copying data from one file descriptor to another.
  211. sendfile on;
  212. sendfile_max_chunk 1M; # prevent one fast connection from entirely occupying the worker process. should be > 800k.
  213. aio threads;
  214. rewrite ^/static/webseed/(.*)$ /videos/$1 break;
  215. rewrite ^/static/(.*)$ /$1 break;
  216. try_files $uri @api;
  217. }
  218. }