<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Zixo</id>
	<title>Alpine Linux - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.alpinelinux.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Zixo"/>
	<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/wiki/Special:Contributions/Zixo"/>
	<updated>2026-04-30T12:21:34Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://wiki.alpinelinux.org/w/index.php?title=Nginx_as_reverse_proxy_with_acme_(letsencrypt)&amp;diff=14147</id>
		<title>Nginx as reverse proxy with acme (letsencrypt)</title>
		<link rel="alternate" type="text/html" href="https://wiki.alpinelinux.org/w/index.php?title=Nginx_as_reverse_proxy_with_acme_(letsencrypt)&amp;diff=14147"/>
		<updated>2017-11-23T13:07:22Z</updated>

		<summary type="html">&lt;p&gt;Zixo: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This setup will allow you to have multiple servers/containers be accessible via a single IP address with the added benefit of centralized generation of letsencrypt certificates and secure https (according to ssllabs ssltest). Be aware you first need to setup regular HTTP server to be able to generate your HTTPS certificates and keys. After you have generated them you can add your HTTPS host based configuration.&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
For this howto we need two tools, NGINX and acme-client.&lt;br /&gt;
&lt;br /&gt;
{{Cmd|apk add nginx acme-client}}&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
&lt;br /&gt;
=== NGINX HTTP ===&lt;br /&gt;
&lt;br /&gt;
==== Global configuration ====&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The security settings are taken from https://cipherli.st . Please also read https://hstspreload.org for details about HSTS.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# ngnix configuration file&lt;br /&gt;
&lt;br /&gt;
user  nginx;&lt;br /&gt;
&lt;br /&gt;
worker_processes  1; # use &amp;quot;auto&amp;quot; to use all available cores (high performance)&lt;br /&gt;
&lt;br /&gt;
events {&lt;br /&gt;
    worker_connections  1024; # increase if you need more connections&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
http {&lt;br /&gt;
    # server_names_hash_bucket_size controls the maximum length&lt;br /&gt;
    # of a virtual host entry (ie the length of the domain name).&lt;br /&gt;
    server_names_hash_bucket_size   64;&lt;br /&gt;
    server_tokens                   off; # hide who we are&lt;br /&gt;
    sendfile                        off; # can cause issues&lt;br /&gt;
&lt;br /&gt;
    # secure nginx according to https://cipherli.st/&lt;br /&gt;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;&lt;br /&gt;
    ssl_prefer_server_ciphers on;&lt;br /&gt;
    ssl_ciphers &amp;quot;EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH&amp;quot;;&lt;br /&gt;
    ssl_ecdh_curve secp384r1; # Requires nginx &amp;gt;= 1.1.0&lt;br /&gt;
    ssl_session_cache shared:SSL:10m;&lt;br /&gt;
    ssl_session_tickets off; # Requires nginx &amp;gt;= 1.5.9&lt;br /&gt;
    ssl_stapling on; # Requires nginx &amp;gt;= 1.3.7&lt;br /&gt;
    ssl_stapling_verify on; # Requires nginx =&amp;gt; 1.3.7&lt;br /&gt;
    resolver 8.8.8.8 8.8.4.4 valid=300s;&lt;br /&gt;
    resolver_timeout 5s;&lt;br /&gt;
    add_header Strict-Transport-Security &amp;quot;max-age=63072000&amp;quot;; # https://hstspreload.org&lt;br /&gt;
    add_header X-Frame-Options DENY;&lt;br /&gt;
    add_header X-Content-Type-Options nosniff;&lt;br /&gt;
&lt;br /&gt;
    ssl_dhparam dhparam.pem;&lt;br /&gt;
&lt;br /&gt;
    # nginx will find this file in the config directory set at nginx build time&lt;br /&gt;
    include mime.types;&lt;br /&gt;
&lt;br /&gt;
    #fallback in case we can&#039;t determine a type&lt;br /&gt;
    default_type application/octet-stream;&lt;br /&gt;
&lt;br /&gt;
    # buffering causes issues&lt;br /&gt;
    proxy_buffering off;&lt;br /&gt;
&lt;br /&gt;
    # include hosts&lt;br /&gt;
    include conf.d/*.conf;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Diffie–Hellman Parameters ====&lt;br /&gt;
&lt;br /&gt;
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: https://wiki.openssl.org/index.php/Manual:Dhparam(1)#OPTIONS)&lt;br /&gt;
&lt;br /&gt;
{{Cmd|openssl dhparam -dsaparam -out /etc/nginx/dhparam.pem 4096}}&lt;br /&gt;
&lt;br /&gt;
At this point you should be able to (re)start your nginx server but it will not use any of the security features (yet).&lt;br /&gt;
&lt;br /&gt;
==== Per site configuration files (conf.d) ====&lt;br /&gt;
&lt;br /&gt;
Since Alpine v3.5 we ship NGINX with an default.conf within the /etc/nginx/conf.d directory.&lt;br /&gt;
To add support for another website you can add files with the .conf extension to this directory.&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/alpinelinux.org.conf:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
server {&lt;br /&gt;
    listen        80;&lt;br /&gt;
    server_name   alpinelinux.org;&lt;br /&gt;
&lt;br /&gt;
    location / {&lt;br /&gt;
        include			conf.d/proxy_set_header.inc;&lt;br /&gt;
        proxy_pass		http://downstream_http_server_host;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Common configuration includes ====&lt;br /&gt;
&lt;br /&gt;
If you need to setup multiple proxy setups you can include duplicated data like shown below.&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/proxy_set_header.inc:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
proxy_set_header    X-Forwarded-By       $server_addr:$server_port;&lt;br /&gt;
proxy_set_header    X-Forwarded-For      $remote_addr;&lt;br /&gt;
proxy_set_header    X-Forwarded-Proto    $scheme;&lt;br /&gt;
proxy_set_header    Host                 $host;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== acme-client ===&lt;br /&gt;
&lt;br /&gt;
To allow NGINX to support https we need to add certificates and support for ACME (Automatic Certificate Management Environment) responses.&lt;br /&gt;
&lt;br /&gt;
==== ACME responses ====&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/acme.inc:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
location /.well-known/acme-challenge {&lt;br /&gt;
    alias /var/www/acme;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add this to your proxy configuration&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/alpinelinux.org.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
server {&lt;br /&gt;
    listen        80;&lt;br /&gt;
    server_name   alpinelinux.org;&lt;br /&gt;
    include       conf.d/acme.inc;&lt;br /&gt;
&lt;br /&gt;
    location / {&lt;br /&gt;
        include			conf.d/proxy_set_header.inc;&lt;br /&gt;
        proxy_pass		http://downstream_http_server_host;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Automatic generate certificates ====&lt;br /&gt;
&lt;br /&gt;
Create the following file and make it executable&lt;br /&gt;
 /etc/periodic/weekly/acme-client&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
&lt;br /&gt;
hosts=&amp;quot;alpinelinux.org&amp;quot;&lt;br /&gt;
&lt;br /&gt;
for host in $hosts; do&lt;br /&gt;
        acme-client -a https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf -Nnmv $host &amp;amp;&amp;amp; renew=1&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
[ &amp;quot;$renew&amp;quot; = 1 ] &amp;amp;&amp;amp; rc-service nginx reload&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This script will run weekly to verify if one of your certificates is outdated and renew them when needed.&lt;br /&gt;
&lt;br /&gt;
==== Initial generation of keys and certificates ====&lt;br /&gt;
&lt;br /&gt;
To create your initial certificates and keys you have to run this manually the first time.&lt;br /&gt;
&lt;br /&gt;
 {{Cmd|/etc/periodic/weekly/acme-client}}&lt;br /&gt;
&lt;br /&gt;
Watch the output and see if all goes well. When its finished you should have files in:&lt;br /&gt;
&lt;br /&gt;
 /etc/ssl/acme/alpinelinux.nl/fullchain.pem&lt;br /&gt;
&lt;br /&gt;
 /etc/ssl/acme/private/alpinelinux.org/privkey.pem&lt;br /&gt;
&lt;br /&gt;
=== NGINX HTTPS ===&lt;br /&gt;
&lt;br /&gt;
==== Per site HTTPS configuration ====&lt;br /&gt;
&lt;br /&gt;
Add the following below the previous HTTP configuration:&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/alpinelinux.org.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
server {&lt;br /&gt;
    listen                  443 ssl;&lt;br /&gt;
    server_name             alpinelinux.org&lt;br /&gt;
    ssl                     on;&lt;br /&gt;
    ssl_certificate         /etc/ssl/acme/alpinelinux.org/fullchain.pem;&lt;br /&gt;
    ssl_certificate_key     /etc/ssl/acme/private/alpinelinux.org/privkey.pem;&lt;br /&gt;
&lt;br /&gt;
    location / {&lt;br /&gt;
        include     conf.d/proxy_set_header.inc;&lt;br /&gt;
        proxy_pass  http://downstream_http_server_host;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Redirect HTTP to HTTPS ===&lt;br /&gt;
&lt;br /&gt;
==== Shared configuration ====&lt;br /&gt;
&lt;br /&gt;
Create the following file:&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/redirect_http.inc&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
location / {&lt;br /&gt;
        return 301 https://$host$request_uri;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Update host configuration ====&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/alpinelinux.org.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
server {&lt;br /&gt;
    listen        80;&lt;br /&gt;
    server_name   alpinelinux.org;&lt;br /&gt;
    include       conf.d/acme.inc;&lt;br /&gt;
    include       conf.d/redirect_http.inc;&lt;br /&gt;
&lt;br /&gt;
    location / {&lt;br /&gt;
        include			conf.d/proxy_set_header.inc;&lt;br /&gt;
        proxy_pass		http://downstream_http_server_host;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Complete host example with IPv6 support ===&lt;br /&gt;
&lt;br /&gt;
 /etc/nginx/conf.d/alpinelinux.org.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# alpinelinux.org&lt;br /&gt;
&lt;br /&gt;
server {&lt;br /&gt;
    listen                  80;&lt;br /&gt;
    listen                  [::]:80;&lt;br /&gt;
    server_name             alpinelinux.org;&lt;br /&gt;
    include                 conf.d/acme.inc;&lt;br /&gt;
    include                 conf.d/redirect_http.inc;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
server {&lt;br /&gt;
    listen                  443 ssl;&lt;br /&gt;
    listen                  [::]:443 ssl;&lt;br /&gt;
    server_name             alpinelinux.org;&lt;br /&gt;
    ssl                     on;&lt;br /&gt;
    ssl_certificate         /etc/ssl/acme/alpinelinux.org/fullchain.pem;&lt;br /&gt;
    ssl_certificate_key     /etc/ssl/acme/private/alpinelinux.org/privkey.pem;&lt;br /&gt;
&lt;br /&gt;
    location / {&lt;br /&gt;
        include     conf.d/proxy_set_header.inc;&lt;br /&gt;
        proxy_pass  http://downstream_http_server_host;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
[[Category:Networking]]&lt;/div&gt;</summary>
		<author><name>Zixo</name></author>
	</entry>
</feed>