Merge pull request #6136 from strapi/docs/proxy/caddy

Add Caddy proxy documentation
This commit is contained in:
Jim LAURIE 2020-05-14 16:58:15 +02:00 committed by GitHub
commit 98701dad6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,184 @@
# Caddy Proxying
As Strapi does not handle SSL directly and hosting a Node.js service on the "edge" network is not a secure solution it is recommended that you use some sort of proxy application such as Nginx, Apache, HAProxy, Traefik, or others. Below you will find some sample configurations for Caddy, naturally these configs may not suit all environments and you will likely need to adjust them to fit your needs.
Caddy has some very easy to use options relating to Let's encrypt and automated SSL certificate issuing/renewing, while it is certainly the "newer" web server market, it is quickly becoming a great "non-technical" user application for proxying. **Please do note that Caddy is still very much in development** and as such is evolving often, this guide is based on Caddy v2.0.0.
## Configuration
The below configuration is based on "Caddy File" type, this is a single file config that Caddy will use to run the web server. There are multiple other options such as the Caddy REST API that this guide will not cover, you should review the [Caddy documentation](https://caddyserver.com/docs/) for further information on alternatives. You can also visit the [Caddy Community](https://caddy.community/) to speak with others relating to configuration questions.
### Caddy file
The Caddyfile is a convenient Caddy configuration format for humans. It is most people's favorite way to use Caddy because it is easy to write, easy to understand, and expressive enough for most use cases.
In the below examples you will need to replace your domain, and should you wish to use SSL you will need to tweak these Caddy file configs to suit your needs, SSL is not covered in this guide and you should review the Caddy documentation.
Below are 3 example Caddy configurations:
- Sub-domain based such as `api.example.com`
- Sub-folder based with both the API and Admin on the same sub-folder such as `example.com/api` and `example.com/api/admin`
- Sub-folder based with split API and Admin such as `example.com/api` and `example.com/dashboard`
::::: tabs
:::: tab Sub-Domain
#### Sub-Domain
This config is using the sub-domain that is dedicated to Strapi only. It will bind to port 80 HTTP and proxies all requests (both api and admin) to the Strapi server running locally on the address specified. If you have configured Caddy properly to handle automated SSL you can remove the `http://` and Caddy will automatically convert all HTTP to HTTPS traffic.
---
Example Domain: `api.example.com`
**Path —** `/etc/caddy/Caddyfile`
```
http://api.example.com {
reverse_proxy 127.0.0.1:1337
}
```
::::
:::: tab Sub-Folder-Unified
#### Sub-Folder Unified
This config is using a sub-folder that is dedicated to Strapi only. It will bind to port 80 HTTP and hosts the "frontend" files on `/var/www` like a normal web server, but proxies all strapi requests on the `example.com/api` sub-path.
::: warning
Please note that this config is not focused on the frontend hosting, you will most likely need to adjust this to your frontend software requirements, it is only being shown here as an example.
:::
---
Example Domain: `example.com/api`
**Path —** `/etc/caddy/Caddyfile`
```
http://api.example.com {
root * /var/www
file_server
route /api* {
uri strip_prefix /api
reverse_proxy 127.0.0.1:1337
}
}
```
::::
:::: tab Sub-Folder-Split
#### Sub-Folder Split
This config is using two sub-folders that are dedicated to Strapi. It will bind to port 80 HTTP and hosts the "frontend" files on `/var/www` like a normal web server, but proxies all strapi API requests on the `example.com/api` sub-path. Likewise it will proxy all admin requests on the `example.com/dashboard` sub-path.
Alternatively for the admin, you can replace the proxy instead with serving the admin `build` folder directly from Caddy, such centralizing the admin but load balancing the backend APIs. The example for this is not shown, but it would likely be something you would build into your CI/CD platform.
::: warning
Please note that this config is not focused on the frontend hosting, you will most likely need to adjust this to your frontend software requirements, it is only being shown here as an example.
:::
---
Example API Domain: `example.com/api`
Example Admin Domain: `example.com/dashboard`
**Path —** `/etc/caddy/Caddyfile`
```
http://api.example.com {
root * /var/www
file_server
route /api* {
uri strip_prefix /api
reverse_proxy 127.0.0.1:1337
}
route /dashboard* {
reverse_proxy 127.0.0.1:1337
}
}
```
::::
:::::
### Strapi Server
In order to take full advantage of a proxied Strapi application you will need to configure Strapi to make it aware of the upstream proxy. Like with the above Caddy configurations there are 3 matching examples. To read more about this server configuration file please see the [server configuration concept](../concepts/configurations.md#server) documentation.
::::: tabs
:::: tab Sub-Domain
#### Sub-Domain Strapi config
---
Example Domain: `api.example.com`
**Path —** `config/server.js`
```js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
url: 'https://api.example.com',
});
```
::::
:::: tab Sub-Folder-Unified
#### Sub-Folder Unified Strapi config
---
Example Domain: `example.com/api`
**Path —** `config/server.js`
```js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
url: 'https://example.com/api',
});
```
::::
:::: tab Sub-Folder-Split
#### Sub-Folder Split Strapi config
---
Example API Domain: `example.com/api`
Example Admin Domain: `example.com/dashboard`
**Path —** `config/server.js`
```js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
url: 'https://example.com/api',
admin: {
url: 'https://example.com/dashboard',
},
});
```
::::
:::::

View File

@ -74,6 +74,18 @@ Manual guides for deployment on various platforms, for One-click and docker plea
Additional guides for optional software additions that compliment or improve the deployment process when using Strapi in a production or production-like environment.
<div>
<InstallLink link="../deployment/caddy-proxy">
<template #icon>
<svg width="100%" height="100%" viewBox="0 0 103 103" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;fill:fff"><g transform="matrix(1,0,0,1,-469.827,-182.266)"><g id="Icon" transform="matrix(0.672861,0,0,0.672861,438.989,27.7667)"><circle cx="122.367" cy="306.152" r="73.563" style="fill:3498DB"/><clipPath id="_clip1"><circle cx="122.367" cy="306.152" r="73.563"/></clipPath><g clip-path="url(#_clip1)"><g transform="matrix(1.55283,0,0,1.55283,-66.5904,101.019)"><g transform="matrix(0.146503,0,0,0.15211,81.614,98.6353)"><path d="M85.551,183.291C85.551,128.882 102.812,88.234 135.453,61.494C166.928,35.708 212.88,22.79 270.112,23.514C328.331,24.245 374.014,37.487 405.1,63.019C437.585,89.699 454.581,130.12 454.581,185.182L454.385,299.127L465.87,287.662L393.91,287.662L405.395,299.147L405.395,214.64L405.37,214.262C405.253,212.503 405.182,210.523 405.084,206.73C404.776,194.814 404.565,190.027 403.813,182.495C402.737,171.714 400.852,161.708 397.84,152.132C391.076,130.625 379.053,112.815 360.448,99.724C338.827,84.512 309.329,76.514 271.083,76.514C232.573,76.514 202.721,84.545 180.754,99.789C161.792,112.947 149.442,130.896 142.568,152.426C139.23,162.883 137.286,173.837 136.402,185.436C135.768,193.755 135.712,200.667 135.953,211.011C135.957,211.188 136.001,213.041 136.013,213.552C136.033,214.451 136.047,215.151 136.058,215.785L136.056,215.6L136.056,299.147L147.541,287.662L74.066,287.662L85.551,299.147L85.551,183.291ZM62.581,183.291L62.581,310.632L159.026,310.632L159.026,215.6L159.025,215.414C159.013,214.721 158.998,213.974 158.977,213.029C158.965,212.506 158.921,210.643 158.917,210.476C158.692,200.834 158.743,194.564 159.306,187.182C160.062,177.255 161.699,168.028 164.45,159.412C169.858,142.472 179.291,128.762 193.849,118.66C211.673,106.291 236.977,99.484 271.083,99.484C304.881,99.484 329.785,106.236 347.23,118.51C361.438,128.508 370.598,142.076 375.928,159.023C378.426,166.966 380.026,175.461 380.956,184.777C381.633,191.558 381.828,195.975 382.122,207.324C382.228,211.423 382.307,213.611 382.45,215.778L382.425,215.02L382.425,310.632L477.335,310.632L477.355,299.167L477.551,185.201C477.551,123.576 457.784,76.564 419.679,45.268C384.065,16.018 333.419,1.337 270.402,0.546C140.797,-1.094 62.581,62.983 62.581,183.291Z" style="fill-rule:nonzero;"/></g><g transform="matrix(0.224865,0,0,0.22949,50.577,55.9129)"><path d="M492.019,420.805C492.019,392.917 468.912,370.275 440.45,370.275L184.447,370.275C155.985,370.275 132.878,392.917 132.878,420.805L132.878,572.108C132.878,599.996 155.985,622.637 184.447,622.637L440.45,622.637C468.912,622.637 492.019,599.996 492.019,572.108L492.019,420.805Z"/></g></g></g></g></g></svg>
</template>
<template #title>Caddy</template>
<template #description>
Overview of proxying Strapi with Caddy
</template>
</InstallLink>
</div>
<div>
<InstallLink link="../deployment/nginx-proxy">
<template #icon>