-
Notifications
You must be signed in to change notification settings - Fork 983
docs: add nginx reverse-proxy example #6185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 27 commits
2c7f0cf
d29da64
247a060
3c247a5
8a96176
4f25817
099428a
d3913b3
7f8d795
3d306a4
69eb387
c1111b3
af32c58
b672a1f
f080259
838e008
23dd1e3
7a7e7e4
a5abc85
3f1353e
8e5531d
8c343f0
86e7dae
7d28e51
a642933
77149bc
ff6fd37
4b5362c
35247ed
30163f4
5b54368
4d0deb6
065ed25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,148 @@ | ||||||||||||||||||||||
# How to use NGINX as a reverse-proxy with LetsEncrypt | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Requirements | ||||||||||||||||||||||
|
||||||||||||||||||||||
1. Start a Coder deployment with a wildcard subdomain. See [this guide](https://coder.com/docs/v2/latest/admin/configure#wildcard-access-url) for more information. | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed it was important to have HTTP_ADDRESS as 3000. Plus the access URL is also important to set.
Suggested change
What do you think about replacing all cases with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is a better option. I will replace all |
||||||||||||||||||||||
|
||||||||||||||||||||||
2. Configure your DNS provider to point your YOUR_SUBDOMAIN and \*.YOUR_SUBDOMAIN to your server's public ip. | ||||||||||||||||||||||
matifali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
> 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. | ||||||||||||||||||||||
|
||||||||||||||||||||||
3. Install NGINX (assuming you're on Debian/Ubuntu): | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo apt install nginx | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
4. Stop NGINX service: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo systemctl stop nginx | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Adding Coder deployment subdomain | ||||||||||||||||||||||
|
||||||||||||||||||||||
> This example assumes Coder is running locally on `127.0.0.1:3000` for the subdomain `YOUR_SUBDOMAIN` e.g. `coder.example.com`. | ||||||||||||||||||||||
|
||||||||||||||||||||||
1. Create NGINX configuration for this app: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo touch /etc/nginx/sites-available/YOUR_SUBDOMAIN | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
2. Activate this file: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo ln -s /etc/nginx/sites-available/YOUR_SUBDOMAIN /etc/nginx/sites-enabled/YOUR_SUBDOMAIN | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Install and configure LetsEncrypt Certbot | ||||||||||||||||||||||
|
||||||||||||||||||||||
1. Install LetsEncrypt Certbot: Refer to the [CertBot documentation](https://certbot.eff.org/instructions?ws=other&os=pip&tab=wildcard) | ||||||||||||||||||||||
matifali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
## Create DNS provider credentials | ||||||||||||||||||||||
|
||||||||||||||||||||||
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: | ||||||||||||||||||||||
matifali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
- Zone - DNS - Edit | ||||||||||||||||||||||
|
||||||||||||||||||||||
2. Create a file in `.secrets/certbot/cloudflare.ini` with the following content: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```ini | ||||||||||||||||||||||
dns_cloudflare_api_token = YOUR_API_TOKEN | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
matifali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
||||||||||||||||||||||
3. Set the correct permissions: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo chmod 600 ~/.secrets/certbot/cloudflare.ini | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Create the certificate | ||||||||||||||||||||||
|
||||||||||||||||||||||
1. Create the wildcard certificate: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini -d coder.example.com -d *.coder.example.com | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Configure nginx | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
1. Edit the file with: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo nano /etc/nginx/sites-available/YOUR_SUBDOMAIN | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
2. Add the following content: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```nginx | ||||||||||||||||||||||
server { | ||||||||||||||||||||||
server_name YOUR_SUBDOMAIN *.YOUR_SUBDOMAIN; | ||||||||||||||||||||||
|
||||||||||||||||||||||
# HTTP configuration | ||||||||||||||||||||||
listen 80; | ||||||||||||||||||||||
listen [::]:80; | ||||||||||||||||||||||
|
||||||||||||||||||||||
# HTTP to HTTPS | ||||||||||||||||||||||
if ($scheme != "https") { | ||||||||||||||||||||||
return 301 https://$host$request_uri; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
# HTTPS configuration | ||||||||||||||||||||||
listen [::]:443 ssl ipv6only=on; | ||||||||||||||||||||||
listen 443 ssl; | ||||||||||||||||||||||
ssl_certificate /etc/letsencrypt/live/YOUR_SUBDOMAIN/fullchain.pem; | ||||||||||||||||||||||
ssl_certificate_key /etc/letsencrypt/live/YOUR_SUBDOMAIN/privkey.pem; | ||||||||||||||||||||||
|
||||||||||||||||||||||
location / { | ||||||||||||||||||||||
proxy_pass http://127.0.0.1:3000; # Change this to your coder deployment port default is 3000 | ||||||||||||||||||||||
proxy_http_version 1.1; | ||||||||||||||||||||||
proxy_set_header Upgrade $http_upgrade; | ||||||||||||||||||||||
proxy_set_header Connection upgrade; | ||||||||||||||||||||||
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 $http_x_forwarded_proto; | ||||||||||||||||||||||
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains" always; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
> Don't forget to change: | ||||||||||||||||||||||
> `YOUR_SUBDOMAIN` by your (sub)domain e.g. `coder.example.com` | ||||||||||||||||||||||
|
||||||||||||||||||||||
3. Test the configuration: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo nginx -t | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Refresh certificates automatically | ||||||||||||||||||||||
|
||||||||||||||||||||||
1. Create a new file in `/etc/cron.weekly`: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo touch /etc/cron.weekly/certbot | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
2. Make it executable: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo chmod +x /etc/cron.weekly/certbot | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
3. And add this code: | ||||||||||||||||||||||
|
||||||||||||||||||||||
```sh | ||||||||||||||||||||||
#!/bin/sh | ||||||||||||||||||||||
sudo certbot renew -q | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Restart NGINX | ||||||||||||||||||||||
|
||||||||||||||||||||||
```console | ||||||||||||||||||||||
sudo systemctl restart nginx | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
And that's it, you should now be able to access Coder at `https://YOUR_SUBDOMAIN`! |
Uh oh!
There was an error while loading. Please reload this page.