about summary refs log tree commit diff
path: root/docker-compose-with-nginx.md
blob: 4a8aa09f7be3db7775f27df46698db613ae2ea9f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Run docker-compose with nginx

## docker-compose.yml

Put your [.env.production](https://github.com/tootsuite/mastodon/blob/master/.env.production.sample) in the same directory where your docker-compose.yml is and modify the /path/to's with your own:

```
version: '3'
services:

  db:
    restart: always
    image: postgres:9-alpine
    networks:
      - db_network
    volumes:
      - ./.db:/var/lib/postgresql/data

  redis:
    restart: always
    image: redis:alpine
    networks:
      - redis_network
    volumes:
      - ./.redis:/data

#  es:
#    restart: always
#    image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.1.3
#    environment:
#      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
#    networks:
#      - internal_network
#    volumes:
#      - ./elasticsearch:/usr/share/elasticsearch/data

  web:
    image: pluralcafe/mastodon:stable
    restart: always
    env_file: .env.production
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    networks:
      - db_network
      - web_network
      - redis_network
    depends_on:
      - db
      - redis
#      - es
    volumes:
      - ./public/system:/mastodon/public/system
      - /etc/localtime:/etc/localtime:ro

  streaming:
    image: pluralcafe/mastodon:stable
    restart: always
    env_file: .env.production
    command: yarn start
    networks:
      - db_network
      - streaming_network
      - redis_network
    depends_on:
      - db
      - redis

  sidekiq:
    image: pluralcafe/mastodon:stable
    restart: always
    env_file: .env.production
    command: bundle exec sidekiq -q default -q mailers -q pull -q push
    depends_on:
      - db
      - redis
    networks:
      - external_network
      - db_network
      - redis_network
    volumes:
      - ./public/system:/mastodon/public/system

  nginx:
    image: nginx:alpine
    restart: always
    environment:
      - NGINX_SERVER=example.com
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/site.template:ro
      - ./public:/var/www/html:ro
      - /var/log/nginx:/var/log/nginx
      - /var/cache/nginx:/var/cache/nginx
      - /path/to/letsencrypt/fullchain.pem:/etc/ssl/certs/fullchain.pem:ro
      - /path/to/letsencrypt/key.pem:/etc/ssl/private/key.pem:ro
      - /path/to/letsencrypt/cert.pem:/etc/ssl/certs/cert.pem:ro
      - /path/to/dhparam.pem:/etc/ssl/dhparam.pem:ro
    ports:
      - "80:80"
      - "443:443"
    networks:
      - external_network
      - web_network
      - streaming_network
    command: sh -c "(cat /etc/nginx/conf.d/site.template | sed 's|NGINX_SERVER|$NGINX_SERVER|g') > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

networks:
  external_network:
  db_network:
    internal: true
  redis_network:
    internal: true
  web_network:
    internal: true
  streaming_network:
    internal: true
```

## nginx.conf (No Modifications)

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

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

  root /var/www/html;
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}

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

  server_name NGINX_SERVER;
  server_tokens off;

  ssl_certificate /etc/ssl/certs/fullchain.pem;
  ssl_certificate_key /etc/ssl/private/key.pem;
  ssl_trusted_certificate /etc/ssl/certs/cert.pem;

  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers ECDHE+CHACHA20:AES256+EECDH:AES256+EDH:!aNULL;
  ssl_prefer_server_ciphers on;
  ssl_ecdh_curve secp521r1:secp384r1;
  ssl_session_cache shared:TLS:2m;
  ssl_session_timeout 10m;
  ssl_session_tickets off;
  ssl_stapling on;
  ssl_stapling_verify on;

  resolver 8.8.8.8 8.8.4.4 valid=300s;
  resolver_timeout 5s;

  ssl_dhparam /etc/ssl/dhparam.pem;

  keepalive_timeout 70;
  sendfile on;
  client_max_body_size 0;

  add_header X-Frame-Options DENY;
  add_header X-Content-Type-Options nosniff;
  add_header X-XSS-Protection "1; mode=block";
  add_header Referrer-Policy "same-origin";
  add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
  add_header Content-Security-Policy "Content-Security-Policy: frame-ancestors 'none'; object-src 'none'; script-src 'self'; base-uri 'none';";
  add_header Access-Control-Allow-Origin https://NGINX_SERVER;
  add_header X-Cache-Status $upstream_cache_status;

  location / {
    try_files $uri @proxy;
  }

  location /sw.js {
    add_header Cache-Control "public, max-age=0";
    try_files $uri @proxy;
  }

  location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) {
    add_header Cache-Control "public, max-age=31536000, immutable";

    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_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    try_files $uri @proxy;
  }

  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 https;
    proxy_set_header Proxy "";
    proxy_pass_header Server;

    proxy_pass http://web:3000;
    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_use_stale error timeout updating http_500 http_502 http_503 http_504;
    proxy_cache_lock on;
    proxy_cache_revalidate on;

    tcp_nodelay on;
  }

  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 https;
    proxy_set_header Proxy "";

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

    tcp_nodelay on;
  }

  error_page 403 /assets/403.html;
  error_page 404 /assets/404.html;
  error_page 410 /assets/410.html;
  error_page 422 /assets/422.html;
  error_page 500 501 502 503 504 /assets/500.html;
}

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



## Start Services as Daemon

For initial installation:

```
$ docker-compose pull
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
Digest: sha256:692147c8a1673fc172f12724f5313e00b8459291e3fe17a10856b4d2c0bf56fb
Status: Image is up to date for redis:alpine
Pulling db (postgres:9-alpine)...
9-alpine: Pulling from library/postgres
Digest: sha256:49f5c42990833857f4caa6d06437b3ce6391e830b05e07c11c6c7737be535649
Status: Image is up to date for postgres:9-alpine
Pulling streaming (pluralcafe/mastodon:stable)...
stable: Pulling from pluralcafe/mastodon
Digest: sha256:10d01d4376454e86bddcdb836b8d088e9eff4a390a1b4e0250b7893dd87f307e
Status: Downloaded newer image for pluralcafe/mastodon:stable
Pulling sidekiq (pluralcafe/mastodon:stable)...
stable: Pulling from pluralcafe/mastodon
Digest: sha256:10d01d4376454e86bddcdb836b8d088e9eff4a390a1b4e0250b7893dd87f307e
Status: Image is up to date for pluralcafe/mastodon:stable
Pulling web (pluralcafe/mastodon:stable)...
stable: Pulling from pluralcafe/mastodon
Digest: sha256:10d01d4376454e86bddcdb836b8d088e9eff4a390a1b4e0250b7893dd87f307e
Status: Image is up to date for pluralcafe/mastodon:stable

$ docker-compose up -d
Starting redis... done
Starting db... done
Starting nginx... done
Starting web... done
Starting streaming... done
Starting sidekiq... done
```

For updating:

```
$ (docker-compose pull 2>&1 | grep --silent "Downloaded newer") && (docker-compose stop && docker-compose up -d)
```