nginx.conf 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. map $http_upgrade $connection_upgrade {
  2. default upgrade;
  3. '' close;
  4. }
  5. upstream backend {
  6. server 127.0.0.1:3000 fail_timeout=0;
  7. }
  8. upstream streaming {
  9. # Instruct nginx to send connections to the server with the least number of connections
  10. # to ensure load is distributed evenly.
  11. least_conn;
  12. server 127.0.0.1:4000 fail_timeout=0;
  13. # Uncomment these lines for load-balancing multiple instances of streaming for scaling,
  14. # this assumes your running the streaming server on ports 4000, 4001, and 4002:
  15. # server 127.0.0.1:4001 fail_timeout=0;
  16. # server 127.0.0.1:4002 fail_timeout=0;
  17. }
  18. proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CACHE:10m inactive=7d max_size=1g;
  19. server {
  20. listen 80;
  21. listen [::]:80;
  22. server_name example.com;
  23. root /home/mastodon/live/public;
  24. location /.well-known/acme-challenge/ { allow all; }
  25. location / { return 301 https://$host$request_uri; }
  26. }
  27. server {
  28. listen 443 ssl http2;
  29. listen [::]:443 ssl http2;
  30. server_name example.com;
  31. ssl_protocols TLSv1.2 TLSv1.3;
  32. ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  33. ssl_prefer_server_ciphers on;
  34. ssl_session_cache shared:SSL:10m;
  35. ssl_session_tickets off;
  36. # Uncomment these lines once you acquire a certificate:
  37. # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  38. # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  39. keepalive_timeout 70;
  40. sendfile on;
  41. client_max_body_size 99m;
  42. root /home/mastodon/live/public;
  43. gzip on;
  44. gzip_disable "msie6";
  45. gzip_vary on;
  46. gzip_proxied any;
  47. gzip_comp_level 6;
  48. gzip_buffers 16 8k;
  49. gzip_http_version 1.1;
  50. gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/x-icon;
  51. location / {
  52. try_files $uri @proxy;
  53. }
  54. # If Docker is used for deployment and Rails serves static files,
  55. # then needed must replace line `try_files $uri =404;` with `try_files $uri @proxy;`.
  56. location = /sw.js {
  57. add_header Cache-Control "public, max-age=604800, must-revalidate";
  58. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  59. try_files $uri =404;
  60. }
  61. location ~ ^/assets/ {
  62. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  63. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  64. try_files $uri =404;
  65. }
  66. location ~ ^/avatars/ {
  67. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  68. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  69. try_files $uri =404;
  70. }
  71. location ~ ^/emoji/ {
  72. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  73. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  74. try_files $uri =404;
  75. }
  76. location ~ ^/headers/ {
  77. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  78. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  79. try_files $uri =404;
  80. }
  81. location ~ ^/packs/ {
  82. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  83. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  84. try_files $uri =404;
  85. }
  86. location ~ ^/shortcuts/ {
  87. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  88. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  89. try_files $uri =404;
  90. }
  91. location ~ ^/sounds/ {
  92. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  93. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  94. try_files $uri =404;
  95. }
  96. location ~ ^/system/ {
  97. add_header Cache-Control "public, max-age=2419200, immutable";
  98. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  99. add_header X-Content-Type-Options nosniff;
  100. add_header Content-Security-Policy "default-src 'none'; form-action 'none'";
  101. try_files $uri =404;
  102. }
  103. location ^~ /api/v1/streaming {
  104. proxy_set_header Host $host;
  105. proxy_set_header X-Real-IP $remote_addr;
  106. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  107. proxy_set_header X-Forwarded-Proto $scheme;
  108. proxy_set_header Proxy "";
  109. proxy_pass http://streaming;
  110. proxy_buffering off;
  111. proxy_redirect off;
  112. proxy_http_version 1.1;
  113. proxy_set_header Upgrade $http_upgrade;
  114. proxy_set_header Connection $connection_upgrade;
  115. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  116. tcp_nodelay on;
  117. }
  118. location @proxy {
  119. proxy_set_header Host $host;
  120. proxy_set_header X-Real-IP $remote_addr;
  121. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  122. proxy_set_header X-Forwarded-Proto $scheme;
  123. proxy_set_header Proxy "";
  124. proxy_pass_header Server;
  125. proxy_pass http://backend;
  126. proxy_buffering on;
  127. proxy_redirect off;
  128. proxy_http_version 1.1;
  129. proxy_set_header Upgrade $http_upgrade;
  130. proxy_set_header Connection $connection_upgrade;
  131. proxy_cache CACHE;
  132. proxy_cache_valid 200 7d;
  133. proxy_cache_valid 410 24h;
  134. proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
  135. add_header X-Cached $upstream_cache_status;
  136. tcp_nodelay on;
  137. }
  138. error_page 404 500 501 502 503 504 /500.html;
  139. }