# heavily modified from https://github.com/mastodon/mastodon/blob/v4.0.2/dist/nginx.conf

map $http_upgrade $connection_upgrade {
	default upgrade;
	''      close;
}

upstream backend {
	server 127.0.0.1:3000 fail_timeout=0;
}

upstream streaming {
	server 127.0.0.1:4000 fail_timeout=0;
}

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CACHE:10m inactive=7d max_size=1g;

server {
	listen [::]:443 http2 ssl;
	listen 443 http2 ssl;

	server_name mastodon.example.com;

	root /home/mastodon/live/public;

	# assumes certbot has been run, nginx will not start with this config enabled otherwise
	ssl_certificate /etc/letsencrypt/live/mastodon.example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/mastodon.example.com/privkey.pem;
	ssl_trusted_certificate /etc/letsencrypt/live/mastodon.example.com/chain.pem;
	ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
	ssl_stapling on;
	ssl_stapling_verify on;
	include snippets/ssl-params.conf;

	keepalive_timeout 30;
	sendfile on;
	client_max_body_size 100m;

	gzip on;
	gzip_disable "msie6";
	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	gzip_buffers 16 8k;
	gzip_http_version 1.1;
	gzip_min_length 256;
	gzip_types	application/atom+xml application/javascript application/json application/rss+xml
			application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
			application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
			font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
			image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;

	# https://github.com/google/ngx_brotli#sample-configuration
	# https://github.com/jakejarvis/mastodon-utils/wiki/nginx#brotli-compression
	# brotli on;
	# brotli_comp_level 4;
	# brotli_static on;
	# brotli_min_length 256;
	# brotli_types	application/atom+xml application/javascript application/json application/rss+xml
	# 		application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
	# 		application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
	# 		font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
	# 		image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;

	# sends most paths to the backend proxy and ignores the location blocks below, except if
	# the file exists in /home/mastodon/live
	location / {
		add_header Strict-Transport-Security "max-age=63072000" always;
		try_files $uri @proxy;
	}

	# condensed version of original Mastodon nginx.conf
	location ~ ^/(?:assets|avatars|emoji|headers|packs|shortcuts|sounds)/ {
		add_header Cache-Control "public, max-age=2419200, must-revalidate"; # 28 days
		add_header Strict-Transport-Security "max-age=63072000" always;
		try_files $uri =404;
	}

	# media uploads & cache (irrelevant if offloading to S3)
	location ~ ^/system/ {
		add_header Cache-Control "public, max-age=2419200, immutable"; # 28 days
		add_header Strict-Transport-Security "max-age=63072000" always;
		try_files $uri =404;
	}

	# static files *only in the root* of /public (/favicon.ico, /sw.js, /robots.txt, etc.)
	location ~ ^/[^/]+\.(?:js|css|png|gif|jpg|txt|ico)$ {
		add_header Cache-Control "public, max-age=604800, must-revalidate"; # 7 days
		add_header Strict-Transport-Security "max-age=63072000" always;
		try_files $uri =404;
	}

	location ^~ /api/v1/streaming {
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header Proxy "";

		proxy_pass http://streaming;
		proxy_buffering off;
		proxy_redirect off;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;

		# security headers
		proxy_hide_header Strict-Transport-Security;
		proxy_hide_header X-Powered-By;
		add_header Strict-Transport-Security "max-age=63072000" always;

		# debugging headers
		add_header Via "1.1 $proxy_host" always;

		tcp_nodelay on;
	}

	location @proxy {
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header Proxy "";
		# uncomment to allow the 'Server: Mastodon' header to override nginx's:
		# proxy_pass_header Server;

		proxy_pass http://backend;
		proxy_buffering on;
		proxy_redirect off;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;

		proxy_cache CACHE;
		proxy_cache_valid 200 7d;
		proxy_cache_valid 410 24h;
		proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

		# security headers
		proxy_hide_header Referrer-Policy;
		proxy_hide_header Strict-Transport-Security;
		proxy_hide_header X-Powered-By;
		proxy_hide_header X-Clacks-Overhead;
		proxy_hide_header X-XSS-Protection;
		add_header Referrer-Policy "strict-origin" always;
		add_header Strict-Transport-Security "max-age=63072000" always;

		# debugging headers
		add_header Via "1.1 $proxy_host" always;
		add_header X-Cache-Status $upstream_cache_status always;

		tcp_nodelay on;
	}

	error_page 404 500 501 502 503 504 /500.html;
}

server {
	listen [::]:80;
	listen 80;

	server_name mastodon.example.com;

	if ($host = mastodon.example.com) {
		return 308 https://$host$request_uri;
	}

	return 403;
}
