You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/infrastructure/nginx.md
+39-31Lines changed: 39 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# NGINX Setup
2
2
3
+
Original article - https://francoisromain.medium.com/host-multiple-websites-with-https-inside-docker-containers-on-a-single-server-18467484ab95
4
+
3
5
## Hosting multiple websites on a single Virtual Private Server is simple and efficient. Nowadays, HTTPS is a required feature for any website.
4
6
5
7
----------
@@ -27,16 +29,17 @@ The manual way to setup a nginx reverse-proxy is to install nginx directly on th
27
29
- The need to restart nginx after each config modification, causing a short downtime for every websites.
28
30
- The need to expose a port of each container to the host, and therefore keep track of the used ports (two containers can not use the same port).
29
31
30
-
**To avoid these downsides, the magic**`[**jwilder/nginx-proxy**](https://github.com/jwilder/nginx-proxy)`**automates the creation of nginx configs and reloads the proxy server when a container starts and stops. And it has HTTPS support.**
32
+
**To avoid these downsides, the magic**[jwilder/nginx-proxy](https://github.com/jwilder/nginx-proxy)**automates the creation of nginx configs and reloads the proxy server when a container starts and stops. And it has HTTPS support.**
31
33
32
-
**Even better, the nginx-proxy has a**`[**LetsEncrypt companion**](https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion)`**, which allows the automatic creation and renewal of HTTPS certificates.**
34
+
**Even better, the nginx-proxy has a**[LetsEncrypt companion](https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion)**, which allows the automatic creation and renewal of HTTPS certificates.**
33
35
34
36
**In one word: set-it-and-forget-it.**
35
37
36
38
# Project structure
37
39
38
40
Create a `nginx-proxy` directory next to the websites directories. In my setup this is in `/srv/www/` on the host.
39
41
42
+
```
40
43
.
41
44
+-- nginx-proxy
42
45
| +-- docker-compose.yml
@@ -48,13 +51,15 @@ Create a `nginx-proxy` directory next to the websites directories. In my setup
48
51
+-- your-website-one.tld
49
52
+-- your-website-two.tld
50
53
+-- your-website-three.tld
54
+
```
51
55
52
56
Inside `/nginx-proxy`, create four empty directories: `conf.d`, `vhost.d`, `html` and `certs`. These are used to store the nginx and the Let’s Encrypt configuration files.
53
57
54
58
# **docker-compose.yml**
55
59
56
60
Inside `/nginx-proxy/`, create a `docker-compose.yml` file with this content:
57
61
62
+
```
58
63
version: '3'services:
59
64
nginx:
60
65
image: nginx
@@ -100,29 +105,30 @@ version: '3'services:
100
105
default:
101
106
external:
102
107
name: nginx-proxy
108
+
```
103
109
104
110
This will launch three services:
105
111
106
112
-`nginx`: the nginx-reverse proxy, uses the default nginx image. The label is needed so that the letsencrypt container knows which nginx proxy container to use.
107
-
-`nginx-gen`: uses the `[jwilder/docker-gen](https://github.com/jwilder/docker-gen)` image. Its `command`instruction will render a nginx configuration (based on`nginx.tmpl`) for each website / container added to the network.
113
+
-`nginx-gen`: uses the [jwilder/docker-gen](https://github.com/jwilder/docker-gen) image. Its `command` instruction will render a nginx configuration (based on `nginx.tmpl`) for each website / container added to the network.
108
114
-`nginx-letsencrypt`: generates and renew the HTTPS certificates.
109
115
110
116
All these services are bound to the `nginx-proxy` network.
111
117
112
118
# nginx.tmpl
113
119
114
-
Inside `/nginx-proxy/`, create a `nginx.tmpl`file and copy the content from [this file](https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl). This is the used by the `nginx-gen` container to create the nginx configuration file for each website / container added to the network.
120
+
Inside `/nginx-proxy/`, create a `nginx.tmpl` file and copy the content from [this file](https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl). This is the used by the `nginx-gen` container to create the nginx configuration file for each website / container added to the network.
115
121
116
122
# Boot up
117
123
118
124
First create the network:
119
125
120
-
$ docker network create nginx-proxy
126
+
`$ docker network create nginx-proxy`
121
127
122
-
Then create the reverse proxy with the `nginx`, `nginx-gen`and `nginx-letsencrypt` containers from the `docker-compose.yml` file:
128
+
Then create the reverse proxy with the `nginx`, `nginx-gen` and `nginx-letsencrypt` containers from the `docker-compose.yml` file:
123
129
124
-
$ cd /srv/www/nginx-proxy/
125
-
$ docker-compose up -d
130
+
`$ cd /srv/www/nginx-proxy/`
131
+
`$ docker-compose up -d`
126
132
127
133
Now the reverse-proxy is running.
128
134
@@ -132,14 +138,16 @@ To link a website to the running nginx-proxy, we need to update its own `docker
132
138
133
139
**1. Environment variables**
134
140
135
-
services:
136
-
my-app:
137
-
…
138
-
environment:
139
-
VIRTUAL_HOST: your-website.tld
140
-
VIRTUAL_PORT: 3000
141
-
LETSENCRYPT_HOST: your-website.tld
141
+
```
142
+
services:
143
+
my-app:
144
+
…
145
+
environment:
146
+
VIRTUAL_HOST: your-website.tld
147
+
VIRTUAL_PORT: 3000
148
+
LETSENCRYPT_HOST: your-website.tld
142
149
LETSENCRYPT_EMAIL: your-email@domain.tld
150
+
```
143
151
144
152
-`VIRTUAL_HOST`: your domain name, used in the nginx configuration.
145
153
-`VIRTUAL_PORT`: (opt.) the port your website is listening to (default to `80`).
@@ -148,27 +156,31 @@ services:
148
156
149
157
**2. Ports**
150
158
151
-
services:
152
-
my-app:
153
-
…
154
-
expose:
159
+
```
160
+
services:
161
+
my-app:
162
+
…
163
+
expose:
155
164
- 3000
165
+
```
156
166
157
167
Same as the `VIRTUAL_PORT` above.
158
168
159
169
**3. Network**
160
170
161
-
networks:
162
-
default:
163
-
external:
171
+
```
172
+
networks:
173
+
default:
174
+
external:
164
175
name: nginx-proxy
176
+
```
165
177
166
178
**Now lets start the website with:**
167
179
168
-
$ cd /srv/www/your-website.tld
169
-
$ docker-compose up -d
180
+
`$ cd /srv/www/your-website.tld`
181
+
`$ docker-compose up -d`
170
182
171
-
**The website is automatically detected by the reverse proxy, has a HTTPS certificate and is visible at**`**https://your-website.tld**`**.**
183
+
**The website is automatically detected by the reverse proxy, has a HTTPS certificate and is visible athttps://your-website.tld.**
172
184
173
185
**Magic!**
174
186
@@ -179,14 +191,10 @@ How can we replicate this production environment, on a local dev computer? I wro
179
191
# Useful links
180
192
181
193
-[A complete guide to switching from HTTP to HTTPS](https://www.smashingmagazine.com/2017/06/guide-switching-http-https/): a very rich article explaining what HTTPS is technically, the different types of certificates and different ways to set it up on a server.
182
-
- A [Docker Compose with nginx-proxy and Let’s Encrypt](https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion) example by Ever Tramos automates the steps exposed in this article.
183
-
184
-
#### 1K
185
-
186
-
#### 30
194
+
- A [Docker Compose with nginx-proxy and Let’s Encrypt](https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion) example by Ever Tramos automates the steps exposed in this article.
0 commit comments