nginx.conf 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # You can use https://ssl-config.mozilla.org/ to generate your cipher set.
  33. # We recommend their "Intermediate" level.
  34. ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
  35. ssl_prefer_server_ciphers on;
  36. ssl_session_cache shared:SSL:10m;
  37. ssl_session_tickets off;
  38. # Uncomment these lines once you acquire a certificate:
  39. # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  40. # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  41. keepalive_timeout 70;
  42. sendfile on;
  43. client_max_body_size 99m;
  44. root /home/mastodon/live/public;
  45. gzip on;
  46. gzip_disable "msie6";
  47. gzip_vary on;
  48. gzip_proxied any;
  49. gzip_comp_level 6;
  50. gzip_buffers 16 8k;
  51. gzip_http_version 1.1;
  52. 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;
  53. location / {
  54. try_files $uri @proxy;
  55. }
  56. # If Docker is used for deployment and Rails serves static files,
  57. # then needed must replace line `try_files $uri =404;` with `try_files $uri @proxy;`.
  58. location = /sw.js {
  59. add_header Cache-Control "public, max-age=604800, must-revalidate";
  60. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  61. try_files $uri =404;
  62. }
  63. location ~ ^/assets/ {
  64. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  65. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  66. try_files $uri =404;
  67. }
  68. location ~ ^/avatars/ {
  69. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  70. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  71. try_files $uri =404;
  72. }
  73. location ~ ^/emoji/ {
  74. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  75. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  76. try_files $uri =404;
  77. }
  78. location ~ ^/headers/ {
  79. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  80. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  81. try_files $uri =404;
  82. }
  83. location ~ ^/packs/ {
  84. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  85. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  86. try_files $uri =404;
  87. }
  88. location ~ ^/shortcuts/ {
  89. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  90. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  91. try_files $uri =404;
  92. }
  93. location ~ ^/sounds/ {
  94. add_header Cache-Control "public, max-age=2419200, must-revalidate";
  95. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  96. try_files $uri =404;
  97. }
  98. location ~ ^/system/ {
  99. add_header Cache-Control "public, max-age=2419200, immutable";
  100. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  101. add_header X-Content-Type-Options nosniff;
  102. add_header Content-Security-Policy "default-src 'none'; form-action 'none'";
  103. try_files $uri =404;
  104. }
  105. location ^~ /api/v1/streaming {
  106. proxy_set_header Host $host;
  107. proxy_set_header X-Real-IP $remote_addr;
  108. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  109. proxy_set_header X-Forwarded-Proto $scheme;
  110. proxy_set_header Proxy "";
  111. proxy_pass http://streaming;
  112. proxy_buffering off;
  113. proxy_redirect off;
  114. proxy_http_version 1.1;
  115. proxy_set_header Upgrade $http_upgrade;
  116. proxy_set_header Connection $connection_upgrade;
  117. add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
  118. tcp_nodelay on;
  119. }
  120. location @proxy {
  121. proxy_set_header Host $host;
  122. proxy_set_header X-Real-IP $remote_addr;
  123. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  124. proxy_set_header X-Forwarded-Proto $scheme;
  125. proxy_set_header Proxy "";
  126. proxy_pass_header Server;
  127. proxy_pass http://backend;
  128. proxy_buffering on;
  129. proxy_redirect off;
  130. proxy_http_version 1.1;
  131. proxy_set_header Upgrade $http_upgrade;
  132. proxy_set_header Connection $connection_upgrade;
  133. proxy_cache CACHE;
  134. proxy_cache_valid 200 7d;
  135. proxy_cache_valid 410 24h;
  136. proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
  137. add_header X-Cached $upstream_cache_status;
  138. tcp_nodelay on;
  139. }
  140. error_page 404 500 501 502 503 504 /500.html;
  141. }