Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 786ad8d

Browse files
matifalibpmct
andauthored
docs: add nginx reverse-proxy example (#6185)
* docs: Add nginx reverse-proxy example This PR adds nginx reverse-proxy example to provision coder with tls certificate using letsencrypt certbot. This will partially resolve #6086. * change nginx example to to absolute path * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * refactor: replaced bullets with numbered lists * remove the ambiguous ip addr. * fixed a typo * correctly handle the wildcard subdomain * simplified after testing * fmt: prettier formatting * Adapt to the coder style guide * fix: agent disconnection * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * Update docs/admin/configure.md Co-authored-by: Ben Potter <[email protected]> * Update examples/web-server/nginx/README.md Co-authored-by: Ben Potter <[email protected]> * updated with suggested changes * updated with requested changes * add reference to certbot docs for other dns providers --------- Co-authored-by: Ben Potter <[email protected]>
1 parent 3b7b96a commit 786ad8d

File tree

2 files changed

+169
-5
lines changed

2 files changed

+169
-5
lines changed

docs/admin/configure.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ of the options, run `coder server --help` on the host.
44
## Access URL
55

66
`CODER_ACCESS_URL` is required if you are not using the tunnel. Set this to the external URL
7-
that users and workspaces use to connect to Coder (e.g. https://coder.example.com). This
7+
that users and workspaces use to connect to Coder (e.g. <https://coder.example.com>). This
88
should not be localhost.
99

1010
> Access URL should be a external IP address or domain with DNS records pointing to Coder.
@@ -46,22 +46,23 @@ subdomain that resolves to Coder (e.g. `*.coder.example.com`).
4646

4747
The Coder server can directly use TLS certificates with `CODER_TLS_ENABLE` and accompanying configuration flags. However, Coder can also run behind a reverse-proxy to terminate TLS certificates from LetsEncrypt, for example.
4848

49-
- Example: [Run Coder with Caddy and LetsEncrypt](https://github.com/coder/coder/tree/main/examples/web-server/caddy)
49+
- Caddy: [Run Coder with Caddy and LetsEncrypt](https://github.com/coder/coder/tree/main/examples/web-server/caddy)
50+
- NGINX: [Run Coder with Nginx and LetsEncrypt](https://github.com/coder/coder/tree/main/examples/web-server/nginx)
5051

5152
## PostgreSQL Database
5253

5354
Coder uses a PostgreSQL database to store users, workspace metadata, and other deployment information.
5455
Use `CODER_PG_CONNECTION_URL` to set the database that Coder connects to. If unset, PostgreSQL binaries will be
55-
downloaded from Maven (https://repo1.maven.org/maven2) and store all data in the config root.
56+
downloaded from Maven (<https://repo1.maven.org/maven2>) and store all data in the config root.
5657

5758
> Postgres 13 is the minimum supported version.
5859
5960
If you are using the built-in PostgreSQL deployment and need to use `psql` (aka
6061
the PostgreSQL interactive terminal), output the connection URL with the following command:
6162

6263
```console
63-
$ coder server postgres-builtin-url
64-
$ psql "postgres://coder@localhost:49627/coder?sslmode=disable&password=feU...yI1"
64+
coder server postgres-builtin-url
65+
psql "postgres://coder@localhost:49627/coder?sslmode=disable&password=feU...yI1"
6566
```
6667

6768
## System packages

examples/web-server/nginx/README.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# How to use NGINX as a reverse-proxy with LetsEncrypt
2+
3+
## Requirements
4+
5+
1. Start a Coder deployment and be sure to set the following [configuration values](https://coder.com/docs/v2/latest/admin/configure):
6+
7+
```console
8+
CODER_HTTP_ADDRESS=127.0.0.1:3000
9+
CODER_ACCESS_URL=https://coder.example.com
10+
CODER_WILDCARD_ACCESS_URL=*coder.example.com
11+
```
12+
13+
Throughout the guide, be sure to replace `coder.example.com` with the domain you intend to use with Coder.
14+
15+
2. Configure your DNS provider to point your coder.example.com and \*.coder.example.com to your server's public IP address.
16+
17+
> For example, to use `coder.example.com` as your subdomain, configure `coder.example.com` and `*.coder.example.com` to point to your server's public ip. This can be done by adding A records in your DNS provider's dashboard.
18+
19+
3. Install NGINX (assuming you're on Debian/Ubuntu):
20+
21+
```console
22+
sudo apt install nginx
23+
```
24+
25+
4. Stop NGINX service:
26+
27+
```console
28+
sudo systemctl stop nginx
29+
```
30+
31+
## Adding Coder deployment subdomain
32+
33+
> This example assumes Coder is running locally on `127.0.0.1:3000` and that you're using `coder.example.com` as your subdomain.
34+
35+
1. Create NGINX configuration for this app:
36+
37+
```console
38+
sudo touch /etc/nginx/sites-available/coder.example.com
39+
```
40+
41+
2. Activate this file:
42+
43+
```console
44+
sudo ln -s /etc/nginx/sites-available/coder.example.com /etc/nginx/sites-enabled/coder.example.com
45+
```
46+
47+
## Install and configure LetsEncrypt Certbot
48+
49+
1. Install LetsEncrypt Certbot: Refer to the [CertBot documentation](https://certbot.eff.org/instructions?ws=apache&os=ubuntufocal&tab=wildcard). Be sure to pick the wildcard tab and select your DNS provider for instructions to install the necessary DNS plugin.
50+
51+
## Create DNS provider credentials
52+
53+
> This example assumes you're using CloudFlare as your DNS provider. For other providers, refer to the [CertBot documentation](https://eff-certbot.readthedocs.io/en/stable/using.html#dns-plugins).
54+
55+
1. Create an API token for the DNS provider you're using: e.g. [CloudFlare](https://dash.cloudflare.com/profile/api-tokens) with the following permissions:
56+
57+
- Zone - DNS - Edit
58+
59+
2. Create a file in `.secrets/certbot/cloudflare.ini` with the following content:
60+
61+
```ini
62+
dns_cloudflare_api_token = YOUR_API_TOKEN
63+
```
64+
65+
```console
66+
mkdir -p ~/.secrets/certbot
67+
touch ~/.secrets/certbot/cloudflare.ini
68+
nano ~/.secrets/certbot/cloudflare.ini
69+
```
70+
71+
3. Set the correct permissions:
72+
73+
```console
74+
sudo chmod 600 ~/.secrets/certbot/cloudflare.ini
75+
```
76+
77+
## Create the certificate
78+
79+
1. Create the wildcard certificate:
80+
81+
```console
82+
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini -d coder.example.com -d *.coder.example.com
83+
```
84+
85+
## Configure nginx
86+
87+
1. Edit the file with:
88+
89+
```console
90+
sudo nano /etc/nginx/sites-available/coder.example.com
91+
```
92+
93+
2. Add the following content:
94+
95+
```nginx
96+
server {
97+
server_name coder.example.com *.coder.example.com;
98+
99+
# HTTP configuration
100+
listen 80;
101+
listen [::]:80;
102+
103+
# HTTP to HTTPS
104+
if ($scheme != "https") {
105+
return 301 https://$host$request_uri;
106+
}
107+
108+
# HTTPS configuration
109+
listen [::]:443 ssl ipv6only=on;
110+
listen 443 ssl;
111+
ssl_certificate /etc/letsencrypt/live/coder.example.com/fullchain.pem;
112+
ssl_certificate_key /etc/letsencrypt/live/coder.example.com/privkey.pem;
113+
114+
location / {
115+
proxy_pass http://127.0.0.1:3000; # Change this to your coder deployment port default is 3000
116+
proxy_http_version 1.1;
117+
proxy_set_header Upgrade $http_upgrade;
118+
proxy_set_header Connection upgrade;
119+
proxy_set_header Host $host;
120+
proxy_set_header X-Real-IP $remote_addr;
121+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
122+
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
123+
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains" always;
124+
}
125+
}
126+
```
127+
128+
> Don't forget to change: `coder.example.com` by your (sub)domain
129+
130+
3. Test the configuration:
131+
132+
```console
133+
sudo nginx -t
134+
```
135+
136+
## Refresh certificates automatically
137+
138+
1. Create a new file in `/etc/cron.weekly`:
139+
140+
```console
141+
sudo touch /etc/cron.weekly/certbot
142+
```
143+
144+
2. Make it executable:
145+
146+
```console
147+
sudo chmod +x /etc/cron.weekly/certbot
148+
```
149+
150+
3. And add this code:
151+
152+
```sh
153+
#!/bin/sh
154+
sudo certbot renew -q
155+
```
156+
157+
## Restart NGINX
158+
159+
```console
160+
sudo systemctl restart nginx
161+
```
162+
163+
And that's it, you should now be able to access Coder at your sub(domain) e.g. `https://coder.example.com`.

0 commit comments

Comments
 (0)