Nginx as reverse proxy with acme (letsencrypt): Difference between revisions
m (→NGINX HTTP) |
|||
Line 19: | Line 19: | ||
The security settings are taken from https://cipherli.st. Please also read https://hstspreload.org for details about HSTS. | The security settings are taken from https://cipherli.st. Please also read https://hstspreload.org for details about HSTS. | ||
< | {{Cat|/etc/nginx/nginx.conf|<nowiki> | ||
# | # /etc/nginx/nginx.conf | ||
user nginx; | user nginx; | ||
Line 38: | Line 38: | ||
# secure nginx according to https://cipherli.st/ | # secure nginx according to https://cipherli.st/ | ||
ssl_protocols TLSv1 TLSv1. | #ssl_protocols TLSv1.3; # Requires nginx >= 1.13.0 else use TLSv1.2 | ||
ssl_protocols TLSv1.2; # We use TLSv1.2 because current stable nginx release don't support TLSv1.3 | |||
ssl_prefer_server_ciphers on; | ssl_prefer_server_ciphers on; | ||
ssl_dhparam /etc/nginx/dhparam.pem; # openssl dhparam -dsaparam -out /etc/nginx/dhparam.pem 4096 | |||
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; | ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; | ||
ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0 | ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0 | ||
Line 51: | Line 53: | ||
add_header X-Frame-Options DENY; | add_header X-Frame-Options DENY; | ||
add_header X-Content-Type-Options nosniff; | add_header X-Content-Type-Options nosniff; | ||
add_header X-XSS-Protection "1; mode=block"; | |||
add_header X-Robots-Tag none; | |||
# nginx will find this file in the config directory set at nginx build time | # nginx will find this file in the config directory set at nginx build time | ||
Line 66: | Line 68: | ||
include conf.d/*.conf; | include conf.d/*.conf; | ||
} | } | ||
</ | </nowiki>}} | ||
==== Diffie–Hellman Parameters ==== | ==== Diffie–Hellman Parameters ==== |
Revision as of 07:26, 9 December 2017
Introduction
This setup will allow you to have multiple servers/containers accessible via a single IP address with the added benefit of a centralized generation of letsencrypt certificates and secure https (according to ssllabs ssltest). Be aware that you first need to setup a regular HTTP server in order to be able to generate your HTTPS certificates and keys. After you have generated them, you can then add your HTTPS host based configuration.
Installation
For this howto, we need three tools: NGINX, acme-client and libressl (to generate Diffie–Hellman Parameters).
apk add nginx acme-client libressl
Setup
NGINX HTTP
Global configuration
First step is to refactor our global nginx.conf
. Its target at a low traffic http server, to increase performance make changes at top level.
The security settings are taken from https://cipherli.st. Please also read https://hstspreload.org for details about HSTS.
Contents of /etc/nginx/nginx.conf
Diffie–Hellman Parameters
In the above configuration ssl_dhparam is used, so we need to generate a global dhparam
file. We want to use a 4096 key size, but this can take a very long time. Because of this, we are adding an extra option (dsaparam) to generate our dhparam
file (see this wiki section):
openssl dhparam -dsaparam -out /etc/nginx/dhparam.pem 4096
At this point, you should be able to (re)start your nginx server, but it will not use any of the security features yet.
Per site configuration files (conf.d)
Since Alpine v3.5, we ship NGINX with a default.conf
within the /etc/nginx/conf.d directory.
To add support for another website, you can add files with the .conf extension to this directory:
Contents of /etc/nginx/conf.d/alpinelinux.org.conf
Common configuration includes
If you need to setup multiple proxy setups, you can include duplicated data such as shown below:
Contents of /etc/nginx/conf.d/proxy_set_header.inc
acme-client
To allow NGINX to support https, we need to add certificates and support for ACME (Automatic Certificate Management Environment) responses.
ACME responses
Contents of /etc/nginx/conf.d/acme.inc
And add this to your proxy configuration:
Contents of /etc/nginx/conf.d/alpinelinux.org.conf
Automatic generation of certificates
Create the following file:
Contents of /etc/periodic/weekly/acme-client
Make it executable:
chmod +x /etc/periodic/weekly/acme-client
This script will run weekly to verify whether one of your certificates is outdated and renew them when needed.
If you have several domains, you can add them to the hosts= variable with a space between each domain. This will create a separate certificate and key for each:
hosts="alpinelinux.org example.com foo.org bar.io"
Initial generation of keys and certificates
To create your initial certificates and keys, you have to run this manually the first time:
/etc/periodic/weekly/acme-client
Watch the output and see if all goes well. When it's finished, you should have files in:
/etc/ssl/acme/alpinelinux.nl/fullchain.pem /etc/ssl/acme/private/alpinelinux.org/privkey.pem
NGINX HTTPS
Per site HTTPS configuration
Add the following below the previous HTTP configuration:
Contents of /etc/nginx/conf.d/alpinelinux.org.conf
Redirect HTTP to HTTPS
Create the following file:
Contents of /etc/nginx/conf.d/redirect_http.inc
Update host configuration
Contents of /etc/nginx/conf.d/alpinelinux.org.conf
Complete host example with IPv6 support
Contents of /etc/nginx/conf.d/alpinelinux.org.conf