2 # replace {{yourdomain}} and review the certbot/letsencrypt config
4 limit_req_zone $binary_remote_addr zone={{yourdomain}}_ratelimit:10m rate=1r/s;
7 # this needs to map to the lemmy (server) docker service hostname
11 # this needs to map to the lemmy-ui docker service hostname
12 server "lemmy-ui:1234";
16 # allow letsencrypt challenge
17 # redirect everything else to 443
20 server_name {{yourdomain}};
21 location /.well-known/acme-challenge/ {
22 root /var/www/certbot;
25 return 301 https://$host$request_uri;
31 listen [::]:443 ssl http2;
32 server_name {{yourdomain}};
34 ssl_certificate /etc/letsencrypt/live/{{yourdomain}}/fullchain.pem;
35 ssl_certificate_key /etc/letsencrypt/live/{{yourdomain}}/privkey.pem;
37 # Various TLS hardening settings
38 # https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html
39 ssl_protocols TLSv1.2 TLSv1.3;
40 ssl_prefer_server_ciphers on;
41 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';
42 ssl_session_timeout 10m;
43 ssl_session_cache shared:SSL:10m;
44 ssl_session_tickets on;
46 ssl_stapling_verify on;
51 # Enable compression for JS/CSS/HTML bundle, for improved client load times.
52 # It might be nice to compress JSON, but leaving that out to protect against potential
53 # compression+encryption information leak attacks like BREACH.
55 gzip_types text/css application/javascript image/svg+xml;
58 # Only connect to this site via HTTPS for the two years
59 add_header Strict-Transport-Security "max-age=63072000";
61 # Various content security headers
62 add_header Referrer-Policy "same-origin";
63 add_header X-Content-Type-Options "nosniff";
64 add_header X-Frame-Options "DENY";
65 add_header X-XSS-Protection "1; mode=block";
67 # Upload limit for pictrs
68 client_max_body_size 20M;
72 # distinguish between ui requests and backend
73 # don't change lemmy-ui or lemmy here, they refer to the upstream definitions on top
74 set $proxpass "http://lemmy-ui";
76 if ($http_accept = "application/activity+json") {
77 set $proxpass "http://lemmy";
79 if ($http_accept = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") {
80 set $proxpass "http://lemmy";
82 if ($request_method = POST) {
83 set $proxpass "http://lemmy";
87 rewrite ^(.+)/+$ $1 permanent;
89 # Send actual client IP upstream
90 proxy_set_header X-Real-IP $remote_addr;
91 proxy_set_header Host $host;
92 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
96 location ~ ^/(api|feeds|nodeinfo|.well-known) {
97 proxy_pass "http://lemmy";
98 proxy_http_version 1.1;
99 proxy_set_header Upgrade $http_upgrade;
100 proxy_set_header Connection "upgrade";
103 limit_req zone={{yourdomain}}_ratelimit burst=30 nodelay;
105 # Add IP forwarding headers
106 proxy_set_header X-Real-IP $remote_addr;
107 proxy_set_header Host $host;
108 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
111 # pictrs only - for adding browser cache control.
112 location ~ ^/(pictrs) {
113 # allow browser cache, images never update, we can apply long term cache
115 add_header Pragma "public";
116 add_header Cache-Control "public";
118 proxy_pass "http://lemmy";
119 proxy_http_version 1.1;
120 proxy_set_header Upgrade $http_upgrade;
121 proxy_set_header Connection "upgrade";
124 limit_req zone={{yourdomain}}_ratelimit burst=30 nodelay;
126 # Add IP forwarding headers
127 proxy_set_header X-Real-IP $remote_addr;
128 proxy_set_header Host $host;
129 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
132 # Redirect pictshare images to pictrs
133 location ~ /pictshare/(.*)$ {
134 return 301 /pictrs/image/$1;
138 # Anonymize IP addresses
139 # https://www.supertechcrew.com/anonymizing-logs-nginx-apache/
140 map $remote_addr $remote_addr_anon {
141 ~(?P<ip>\d+\.\d+\.\d+)\. $ip.0;
142 ~(?P<ip>[^:]+:[^:]+): $ip::;
143 127.0.0.1 $remote_addr;
147 access_log /var/log/nginx/access.log combined;