]> Untitled Git - lemmy.git/blob - docker/prod/nginx.conf
Fix invalid config in docker/prod (fixes #2520) (#2524)
[lemmy.git] / docker / prod / nginx.conf
1 # nginx example config
2 # replace {{yourdomain}} and review the certbot/letsencrypt config
3
4 limit_req_zone $binary_remote_addr zone={{yourdomain}}_ratelimit:10m rate=1r/s;
5
6 upstream lemmy {
7     # this needs to map to the lemmy (server) docker service hostname
8     server "lemmy:8536";
9 }
10 upstream lemmy-ui {
11     # this needs to map to the lemmy-ui docker service hostname
12     server "lemmy-ui:1234";
13 }
14
15 server {
16     # allow letsencrypt challenge
17     # redirect everything else to 443
18     listen 80;
19     listen [::]:80;
20     server_name {{yourdomain}};
21     location /.well-known/acme-challenge/ {
22         root /var/www/certbot;
23     }
24     location / {
25         return 301 https://$host$request_uri;
26     }
27 }
28
29 server {
30     listen 443 ssl http2;
31     listen [::]:443 ssl http2;
32     server_name {{yourdomain}};
33
34     ssl_certificate /etc/letsencrypt/live/{{yourdomain}}/fullchain.pem;
35     ssl_certificate_key /etc/letsencrypt/live/{{yourdomain}}/privkey.pem;
36
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;
45     ssl_stapling on;
46     ssl_stapling_verify on;
47
48     # Hide nginx version
49     server_tokens off;
50
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.
54     gzip on;
55     gzip_types text/css application/javascript image/svg+xml;
56     gzip_vary on;
57
58     # Only connect to this site via HTTPS for the two years
59     add_header Strict-Transport-Security "max-age=63072000";
60
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";
66
67     # Upload limit for pictrs
68     client_max_body_size 20M;
69
70     # frontend
71     location / {
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";
75
76         if ($http_accept = "application/activity+json") {
77             set $proxpass "http://lemmy";
78         }
79         if ($http_accept = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") {
80             set $proxpass "http://lemmy";
81         }
82         if ($request_method = POST) {
83             set $proxpass "http://lemmy";
84         }
85         proxy_pass $proxpass;
86
87         rewrite ^(.+)/+$ $1 permanent;
88
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;
93     }
94
95     # backend
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";
101
102         # Rate limit
103         limit_req zone={{yourdomain}}_ratelimit burst=30 nodelay;
104
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;
109     }
110
111     # pictrs only - for adding browser cache control.
112     location ~ ^/(pictrs) {
113         # allow browser cache, images never update, we can apply long term cache
114         expires 120d;
115         add_header Pragma "public";
116         add_header Cache-Control "public";
117
118         proxy_pass "http://lemmy";
119         proxy_http_version 1.1;
120         proxy_set_header Upgrade $http_upgrade;
121         proxy_set_header Connection "upgrade";
122
123         # Rate limit
124         limit_req zone={{yourdomain}}_ratelimit burst=30 nodelay;
125
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;
130     }
131
132     # Redirect pictshare images to pictrs
133     location ~ /pictshare/(.*)$ {
134         return 301 /pictrs/image/$1;
135     }
136 }
137
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;
144     ::1                         $remote_addr;
145     default                     0.0.0.0;
146 }
147 access_log /var/log/nginx/access.log combined;