Úvod
LXD (vyslovováno „Lex-Dee“) je systémový správce kontejnerů postavený na Linuxových kontejnerech (LXC) podporovaných společností Canonical. Cílem LXD je poskytnout zážitek podobný virtuálnímu stroji, ale spíše prostřednictvím kontejnerizace než hardwarové virtualizace. Ve srovnání s Dockerem pro poskytování aplikací nabízí LXD téměř plnou funkčnost operačního systému s dalšími funkcemi, jako jsou snímky, živé migrace a správa úložiště.
Reverzní proxy je server, který se nachází mezi interními aplikacemi a externími klienty a předává požadavky klientů na příslušný server. Zatímco mnoho běžných aplikací, jako je Node.js, je schopno fungovat jako servery samostatně, může jim chybět řada pokročilých funkcí pro vyrovnávání zátěže, zabezpečení a akcelerace.
Tato příručka vysvětluje vytvoření reverzního proxy v kontejneru LXD za účelem hostování více webových stránek, každý ve svých vlastních dodatečných kontejnerech. Budete využívat webové servery NGINX a Apache a zároveň se budete spoléhat na NGINX jako reverzní proxy.
Pro pochopení reverzního proxy vytvořeného v této příručce se prosím podívejte na následující diagram.
<šablona x-if=visible><šablona x-if=$isMobile()>
V této příručce budete:
-
Nainstalujte a nakonfigurujte kontejnery pro webové servery NGINX i Apache.
-
Přečtěte si, jak nainstalovat a nakonfigurovat reverzní proxy v kontejneru.
-
Získejte podporu SSL/TLS prostřednictvím certifikátů Let’s Encrypt s automatickou obnovou certifikátu.
-
Odstraňování běžných chyb.
Poznámka Pro zjednodušení výraz kontejner se v této příručce používá k popisu kontejnerů systému LXD.
Než začnete
-
CompleteA Beginner’s Guide to LXD:Setting Apache Web Server in a Container. Průvodce vám dá pokyn k vytvoření kontejneru s názvem
web
s webovým serverem Apache pro testovací účely. Odeberte tento kontejner spuštěním následujících příkazů.lxc stop web lxc delete web
Poznámka
Pro tuto příručku je zapotřebí LXD verze 3.3 nebo novější. Zkontrolujte verzi pomocí následujícího příkazu:
lxd --version
Pokud verze není 3.3 nebo novější, aktualizujte na nejnovější verzi instalací balíčku snap podle pokynů v Příručce pro začátečníky k LXD:Nastavení webového serveru Apache v kontejneru a použijte následující příkaz:
sudo lxd.migrate
-
Tato příručka používá názvy hostitelů
apache1.example.com
anginx1.example.com
pro dva příklady webových stránek. Nahraďte tyto názvy názvy hostitelů, které vlastníte, a nastavte jejich záznamy DNS tak, aby je nasměrovaly na adresu IP serveru, který jste vytvořili. Nápovědu k DNS naleznete v naší příručce správce DNS.
Vytvoření kontejnerů
-
Vytvořte dva kontejnery s názvem
apache1
anginx1
, jeden s webovým serverem Apache a druhý s webovým serverem NGINX. Pro jakékoli další webové stránky můžete vytvořit nové kontejnery pomocí vámi zvoleného softwaru webového serveru.lxc launch ubuntu:18.04 apache1 lxc launch ubuntu:18.04 nginx1
-
Vytvořte
proxy
kontejner pro reverzní proxy.lxc launch ubuntu:18.04 proxy
-
Vypište kontejnery pomocí příkazu list.
lxc list
-
Výstup vypadá podobně jako následující.
+---------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +---------+---------+---------------------+-----------------------------------------------+------------+-----------+ | apache1 | RUNNING | 10.10.10.204 (eth0) | fd42:67a4:b462:6ae2:216:3eff:fe01:1a4e (eth0) | PERSISTENT | | +---------+---------+---------------------+-----------------------------------------------+------------+-----------+ | nginx1 | RUNNING | 10.10.10.251 (eth0) | fd42:67a4:b462:6ae2:216:3eff:febd:67e3 (eth0) | PERSISTENT | | +---------+---------+---------------------+-----------------------------------------------+------------+-----------+ | proxy | RUNNING | 10.10.10.28 (eth0) | fd42:67a4:b462:6ae2:216:3eff:fe00:252e (eth0) | PERSISTENT | | +---------+---------+---------------------+-----------------------------------------------+------------+-----------+
Existují tři kontejnery, všechny v RUNNING stát – každý se svou vlastní privátní IP adresou. Poznamenejte si IP adresy (IPv4 i IPv6) pro kontejner
proxy
. Budete je potřebovat ke konfiguraciproxy
kontejneru v pozdější sekci.Nyní, když jsou kontejnery vytvořeny, následující kroky podrobně popisují, jak nastavit software webového serveru v
apache1
anginx1
kontejnery aproxy
kontejner, aby byly webové servery přístupné z internetu.
Konfigurace kontejneru webového serveru Apache
Při použití reverzního proxy před webovým serverem webový server nezná IP adresy návštěvníků. Webový server vidí pouze IP adresu reverzního proxy. Každý webový server má však způsob, jak identifikovat skutečnou vzdálenou IP adresu návštěvníka. U Apache se to provádí pomocí modulu Remote IP Apache. Aby modul fungoval, musí být reverzní proxy nakonfigurováno tak, aby předávalo informace o vzdálené IP adrese.
-
Spusťte shell v
apache1
kontejner.lxc exec apache1 -- sudo --user ubuntu --login
-
Aktualizujte seznam balíčků v
apache1
kontejner.sudo apt update
-
Nainstalujte balíček apache2 v kontejneru.
sudo apt install -y apache2
-
Vytvořte soubor
/etc/apache2/conf-available/remoteip.conf
.- Soubor:remoteip .conf
1 2
RemoteIPHeader X-Real-IP RemoteIPTrustedProxy 10.10.10.28 fd42:67a4:b462:6ae2:216:3eff:fe00:252e
Můžete použít
nano
textový editor spuštěním příkazusudo nano /etc/apache2/conf-available/remoteip.conf
. Všimněte si, že toto jsou IP adresyproxy
kontejner uvedený výše, pro IPv4 i IPv6. Nahraďte je adresami IP ze svéholxc list
výstup.Poznámka Místo zadávání IP adres můžete také použít název hostitele
proxy.lxd
. Modul RemoteIP Apache je však zvláštní při použití názvu hostitele a používá pouze jednu ze dvou IP adres (buď IPv4 nebo IPv6), což znamená, že webový server Apache nezná skutečnou zdrojovou IP adresu pro některá připojení. Explicitním uvedením adres IPv4 i IPv6 si můžete být jisti, že RemoteIP úspěšně přijímá zdrojové informace o IP ze všech připojení reverzního proxy. -
Povolte nový
remoteip.conf
konfigurace.sudo a2enconf remoteip
Enabling conf remoteip. To activate the new configuration, you need to run: systemctl reload apache2
-
Povolte
remoteip
Modul Apache.sudo a2enmod remoteip
Enabling module remoteip. To activate the new configuration, you need to run: systemctl restart apache2
-
Upravte výchozí webovou stránku pro Apache tak, aby odkazovala na to, že běží v kontejneru LXD.
sudo nano /var/www/html/index.html
Změňte řádek "Funguje to!" (řádek číslo 224) na „Funguje to uvnitř kontejneru LXD!“ Uložte a ukončete.
-
Restartujte webový server Apache.
sudo systemctl reload apache2
-
Vraťte se zpět k hostiteli.
exit
Vytvořili jste a nakonfigurovali webový server Apache, ale server ještě není přístupný z Internetu. Zpřístupní se po konfiguraci proxy
kontejneru v pozdější sekci.
Vytvoření kontejneru webového serveru NGINX
Stejně jako Apache, NGINX nezná IP adresy návštěvníků při použití reverzního proxy před webovým serverem. Místo toho vidí pouze IP adresu reverzního proxy. Každý software webového serveru NGINX dokáže identifikovat skutečnou vzdálenou IP adresu návštěvníka pomocí modulu Real IP. Aby modul fungoval, musí být reverzní proxy nakonfigurováno tak, aby předávalo informace týkající se vzdálených IP adres.
-
Spusťte shell v
nginx1
kontejner.lxc exec nginx1 -- sudo --user ubuntu --login
-
Aktualizujte seznam balíčků v
nginx1
kontejner.sudo apt update
-
Nainstalujte NGINX v kontejneru.
sudo apt install -y nginx
-
Vytvořte soubor
/etc/nginx/conf.d/real-ip.conf
.- Soubor:real -ip.conf
1 2
real_ip_header X-Real-IP; set_real_ip_from proxy.lxd;
Můžete použít
nano
textový editor spuštěním příkazusudo nano /etc/nginx/conf.d/real-ip.conf
.Poznámka Zadali jste název hostitele reverzního proxy,
proxy.lxd
. Každý kontejner LXD automaticky získá název hostitele, což je název kontejneru plus přípona.lxd
. Zadánímset_real_ip_from
pole sproxy.lxd
, dáváte webovému serveru NGINX pokyn, aby přijal informace o skutečné IP adrese pro každé připojení, pokud toto připojení pochází zproxy.lxd
. Informace o skutečné IP adrese naleznete v HTTP hlavičceX-Real-IP
v každém spojení. -
Upravte výchozí webovou stránku pro NGINX tak, aby odkazovala na to, že běží v kontejneru LXD.
sudo nano /var/www/html/index.nginx-debian.html
Změňte řádek „Vítejte v nginx!“ (řádek číslo 14) na „Vítejte v nginx běžícím v kontejneru systému LXD!“. Uložte a ukončete.
-
Restartujte webový server NGINX.
sudo systemctl reload nginx
-
Vraťte se zpět k hostiteli.
exit
Vytvořili jste a nakonfigurovali webový server NGINX, ale server ještě není přístupný z internetu. Zpřístupní se po konfiguraci proxy
kontejneru v další sekci.
Nastavení zpětného proxy
V této sekci nakonfigurujete kontejner proxy
. Nainstalujete NGINX a nastavíte jej jako reverzní proxy, poté přidáte příslušné LXD proxy zařízení aby byly oba porty 80 a 443 vystaveny internetu.
-
Přidejte LXD proxy zařízení přesměrovat připojení z internetu na porty 80 (HTTP) a 443 (HTTPS) na serveru na příslušné porty na
proxy
kontejner.lxc config device add proxy myport80 proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80 proxy_protocol=true lxc config device add proxy myport443 proxy listen=tcp:0.0.0.0:443 connect=tcp:127.0.0.1:443 proxy_protocol=true
Device myport80 added to proxy Device myport443 added to proxy
lxc config device add
příkaz bere jako argumenty:Argument Vysvětlení proxy
Název kontejneru. myport80
Název tohoto proxy zařízení. proxy
Typ zařízení LXD (LXD proxy zařízení). listen=tcp:0.0.0.0:80
Zařízení proxy naslouchá na hostiteli (výchozí) na portu 80, protokol TCP, na všech rozhraních. connect=tcp:127.0.0.1:80
Zařízení proxy se připojuje ke kontejneru na portu 80, protokol TCP, na rozhraní zpětné smyčky. V předchozích verzích LXD jste mohli zadat localhost
tady. V LXD 3.13 nebo novějším však můžete zadat pouze adresy IP.proxy_protocol=true
Požadavek na povolení protokolu PROXY, aby reverzní proxy získal původní IP adresu ze zařízení proxy. Poznámka
Pokud chcete odebrat zařízení proxy, použijte
lxc config device remove
. Pokud chcete odebrat výše uvedené zařízenímyport80
, spusťte následující příkaz:lxc config device remove proxy myport80
Kde proxy je název kontejneru a myport80 je název zařízení.
-
Spusťte shell v
proxy
kontejner.lxc exec proxy -- sudo --user ubuntu --login
-
Aktualizujte seznam balíčků.
sudo apt update
-
Nainstalujte NGINX do kontejneru.
sudo apt install -y nginx
-
Odhlaste se z kontejneru.
logout
Přímý provoz na webový server Apache z reverzního proxy
Reverzní proxy kontejner je spuštěn a balíček NGINX byl nainstalován. Chcete-li pracovat jako reverzní proxy, přidejte příslušnou konfiguraci webu, aby se NGINX mohl identifikovat (pomocí server_name
níže) příslušný název hostitele a poté předejte (pomocí proxy_pass
níže) připojení k příslušnému kontejneru LXD.
-
Spusťte shell v
proxy
kontejner.lxc exec proxy -- sudo --user ubuntu --login
-
Vytvořte soubor
apache1.example.com
v/etc/nginx/sites-available/
pro konfiguraci vašeho prvního webu.- Soubor:apache1 .example.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
server { listen 80 proxy_protocol; listen [::]:80 proxy_protocol; server_name apache1.example.com; location / { include /etc/nginx/proxy_params; proxy_pass http://apache1.lxd; } real_ip_header proxy_protocol; set_real_ip_from 127.0.0.1; }
Můžete spustit
sudo nano /etc/nginx/sites-available/apache1.example.com
otevřete textový editor a přidejte konfiguraci. Poznámka, v tomto případě stačí upravitserver_name
být názvem hostitele webu. -
Povolte web.
sudo ln -s /etc/nginx/sites-available/apache1.example.com /etc/nginx/sites-enabled/
-
Restartujte reverzní proxy NGINX. Restartováním služby NGINX přečte a použije nové pokyny pro web právě přidané do
/etc/nginx/sites-enabled
.sudo systemctl reload nginx
-
Ukončete kontejner proxy a vraťte se zpět k hostiteli.
logout
-
Z místního počítače navštivte pomocí webového prohlížeče adresu URL svého webu. Měli byste vidět výchozí stránku Apache:
<šablona x-if=visible><šablona x-if=$isMobile()>
Poznámka Pokud se podíváte na soubor Apache access.log (výchozí soubor
/var/log/apache2/access.log
), stále zobrazuje soukromou IP adresuproxy
kontejner místo skutečné IP adresy. Tento problém je specifický pro webový server Apache a souvisí s tím, jak server tiskne protokoly. Ostatní software na webovém serveru je schopen používat skutečnou IP. Chcete-li to opravit pomocí protokolů Apache, přečtěte si část Odstraňování problémů.
Přímý provoz na webový server NGINX z reverzního proxy
Je spuštěn kontejner reverzního proxy a NGINX
balíček byl nainstalován. Chcete-li pracovat jako reverzní proxy, musíte přidat příslušnou konfiguraci webu, tedy NGINX
lze identifikovat (pomocí server_name
níže) příslušný název hostitele a poté předejte (pomocí proxy_pass
níže) připojení k příslušnému kontejneru LXD se skutečným softwarem webového serveru.
-
Spusťte shell v
proxy
kontejner.lxc exec proxy -- sudo --user ubuntu --login
-
Vytvořte soubor
nginx1.example.com
v/etc/nginx/sites-available/
pro konfiguraci vašeho druhého webu.- Soubor:nginx1 .example.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
server { listen 80 proxy_protocol; listen [::]:80 proxy_protocol; server_name nginx1.example.com; location / { include /etc/nginx/proxy_params; proxy_pass http://nginx1.lxd; } real_ip_header proxy_protocol; set_real_ip_from 127.0.0.1; }
Můžete spustit
sudo nano /etc/nginx/sites-available/nginx1.example.com
pro vytvoření konfigurace. Všimněte si, že stačí upravit poleserver_name
být názvem hostitele webu. -
Povolte web.
sudo ln -s /etc/nginx/sites-available/nginx1.example.com /etc/nginx/sites-enabled/
-
Restartujte službu reverzního proxy NGINX.
sudo systemctl reload nginx
-
Ukončete kontejner proxy a vraťte se zpět k hostiteli.
logout
-
Z místního počítače navštivte pomocí webového prohlížeče adresu URL svého webu. Měli byste vidět následující výchozí stránku NGINX.
<šablona x-if=visible><šablona x-if=$isMobile()>
Přidání podpory pro HTTPS pomocí Let's Encrypt
-
Spusťte shell v
proxy
kontejner.lxc exec proxy -- sudo --user ubuntu --login
-
Přidejte úložiště
ppa:certbot/certbot
spuštěním následujícího příkazu.sudo add-apt-repository ppa:certbot/certbot
-
Výstup vypadá podobně jako následující.
This is the PPA for packages prepared by Debian Let's Encrypt Team and backported for Ubuntu(s). More info: https://launchpad.net/~certbot/+archive/ubuntu/certbot Press [ENTER] to continue or Ctrl-c to cancel adding it. Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] ... Fetched 3360 kB in 2s (2018 kB/s) Reading package lists... Done
-
Nainstalujte si následující dva balíčky, abyste a) podporovali vytváření certifikátů Let’s Encrypt; a b) automatickou konfiguraci reverzního proxy serveru NGINX pro použití certifikátů Let’s Encrypt. Balíčky jsou staženy z nově vytvořeného úložiště.
sudo apt-get install certbot python-certbot-nginx
Poznámka Tím se nakonfiguruje reverzní proxy, aby také fungovala jako Proxy ukončení TLS . Jakákoli konfigurace HTTPS se nachází pouze v
proxy
kontejner. Díky tomu není nutné uvnitř kontejnerů webového serveru provádět žádné úkoly související s certifikáty a Let’s Encrypt. -
Spusťte
certbot
jako root s--nginx
za účelem provedení automatické konfigurace Let’s Encrypt pro první web. Budete požádáni o poskytnutí platné e-mailové adresy pro naléhavé oznámení o obnovení a zabezpečení. Poté budete požádáni, abyste přijali podmínky služby a zda si přejete být v budoucnu kontaktováni Electronic Frontier Foundation. Dále zadejte web, pro který aktivujete HTTPS. Nakonec se můžete rozhodnout nastavit zařízení, které automaticky přesměruje připojení HTTP na připojení HTTPS.sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: N Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: apache1.example.com 2: nginx1.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 1 Obtaining a new certificate Performing the following challenges: http-01 challenge for apache1.example.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/apache1.example.com Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/apache1.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://apache1.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=apache1.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/apache1.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/apache1.example.com/privkey.pem Your cert will expire on 2019-10-07. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
-
Spusťte
certbot
jako root s--nginx
za účelem provedení automatické konfigurace Let’s Encrypt pro druhý web. Toto je podruhé, co spouštímecertbot
, proto jsme přímo požádáni, abychom vybrali webovou stránku ke konfiguraci.sudo certbot --nginx
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: apache1.example.com 2: nginx1.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 2 Obtaining a new certificate Performing the following challenges: http-01 challenge for nginx1.example.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/nginx1.example.com Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2 Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/nginx1.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://nginx1.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=nginx1.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/nginx1.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/nginx1.example.com/privkey.pem Your cert will expire on 2019-10-07. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
-
Po přidání všech webových stránek proveďte zkušební provoz, abyste otestovali obnovu certifikátů. Zkontrolujte, zda se všechny webové stránky úspěšně aktualizují, abyste zajistili, že automatické zařízení aktualizovalo certifikáty bez dalšího úsilí.
sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/apache1.example.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator nginx, Installer nginx Renewing an existing certificate Performing the following challenges: http-01 challenge for apache1.example.com Waiting for verification... Cleaning up challenges - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed with reload of nginx server; fullchain is /etc/letsencrypt/live/apache1.example.com/fullchain.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/nginx1.example.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator nginx, Installer nginx Renewing an existing certificate Performing the following challenges: http-01 challenge for nginx1.example.com Waiting for verification... Cleaning up challenges - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed with reload of nginx server; fullchain is /etc/letsencrypt/live/nginx1.example.com/fullchain.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/apache1.example.com/fullchain.pem (success) /etc/letsencrypt/live/nginx1.example.com/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal.
Note The
certbot
package adds a systemd timer in order to activate the automated renewal of Let’s Encrypt certificates. You can view the details of this timer by runningsystemctl list-timers
. -
The certbot tool edits and changes the NGINX configuration files of your websites. In doing so, certbot does not obey initial
listen
directive (listen 80 proxy_protocol;
) and does not add theproxy_protocol
parameter to the newly addedlisten 443 ssl;
lines. You must edit the configuration files for each website and append “proxy_protocol” to each “listen 443 ssl;” line.sudo nano /etc/nginx/sites-enabled/apache1.example.com sudo nano /etc/nginx/sites-enabled/nginx1.example.com
listen 443 ssl proxy_protocol; # managed by Certbot listen [::]:443 ssl proxy_protocol; # managed by Certbot
Note Each website configuration file has two pairs of
listen
directives:HTTP and HTTPS, respectively. The first is the original pair for HTTP that was added in a previous section. The second pair was added by certbot for HTTPS. These are pairs because they they cover both IPv4 and IPv6. The notation[::]
refers to IPv6. When adding the parameterproxy_protocol
, add it before the;
on each line as shown above. -
Restart NGINX.
sudo systemctl restart nginx
Troubleshooting
Browser Error “SSL_ERROR_RX_RECORD_TOO_LONG”
You have configured Certbot and created the appropriate Let’s Encrypt configuration for each website. But when you access the website from your browser, you get the following error.
Secure Connection Failed
An error occurred during a connection to apache1.example.com. SSL received a record that exceeded the maximum permissible length. Error code: SSL_ERROR_RX_RECORD_TOO_LONG
The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
Please contact the website owners to inform them of this problem.
This error is caused when the NGINX reverse proxy in the proxy
container does not have the proxy_protocol
parameter in the listen 443
directives. Without the parameter, the reverse proxy does not consume the PROXY protocol information before it performs the HTTPS work. It mistakenly passes the PROXY protocol information to the HTTPS module, hence the record too long error.
Follow the instructions in the previous section and add proxy_protocol
to all listen 443
directives. Finally, restart NGINX.
Error “Unable to connect” or “This site can’t be reached”
When you attempt to connect to the website from your local computer and receive Unable to connect or This site can’t be reached errors, it is likely the proxy devices have not been configured.
Run the following command on the host to verify whether LXD is listening and is able to accept connections to ports 80 (HTTP) and 443 (HTTPS).
sudo ss -ltp '( sport = :http || sport = :https )'
NoteThe
ss
command is similar tonetstat
andlsof
. It shows information about network connections. In this case, we use it to verify whether there is a service on ports 80 and 443, and which service it is.
-l
, to display the listening sockets,-t
, to display only TCP sockets,-p
, to show which processes use those sockets,( sport = :http || sport = :https )
, to show only ports 80 and 443 (HTTP and HTTPS, respectively).
In the following output we can verify that both ports 80 and 443 (HTTP and HTTPS, respectively) are in the LISTEN state. In the last column we verify that the process listening is lxd
itself.
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:http *:* users:(("lxd",pid=1301,fd=7),("lxd",pid=1301,fd=5))
LISTEN 0 128 *:https *:* users:(("lxd",pid=1349,fd=7),("lxd",pid=1349,fd=5))
If you see a process listed other than lxd
, stop that service and restart the proxy
container. By restarting the proxy
container, LXD applies the proxy devices again.
The Apache access.log Shows the IP Address of the Proxy Container
You have set up the apache1
container and verified that it is accessible from the internet. But the logs at /var/log/apache2/access.log
still show the private IP address of the proxy
container, either the private IPv4 (10.x.x.x ) or the private IPv6 addresses. What went wrong?
The default log formats for printing access logs in Apache only print the IP address of the host of the last hop (i.e. the proxy server). This is the %h
format specifier as shown below.
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
The %h
must be manually replaced with the %a
format specifier, which prints the value as returned by the real RemoteIP Apache module.
LogFormat "%v:%p %a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%a %l %u %t \"%r\" %>s %O" common
-
Run the following command in the
apache1
container to edit the configuration filehttpd.conf
and perform the change from%h
to%a
.sudo nano /etc/apache2/apache2.conf
-
Reload the Apache web server service.
sudo systemctl reload apache2
Next Steps
You have set up a reverse proxy to host many websites on the same server and installed each website in a separate container. You can install static or dynamic websites in the containers. For dynamic websites, you may need additional configuration; check the respective documentation for setup using a reverse proxy. In addition, you may also use NGINX as a reverse proxy for non-HTTP(S) services.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
- LXD Introduction
- LXD support community
- Try LXD Online
- NGINX Web Server
- Apache Web Server
- NGINX Reverse Proxy Settings
- Proxy Protocol
- TLS Termination Proxy