如何在CentOS/RHEL 8上安装Hitch

# dnf install epel-release
# dnf install hitch openssl

Hitch 的配置文件:/etc/hitch/hitch.conf

获得SSL/TLS认证

创建SSL/TLS证书包

可以是自签名证书、商业证书或者使用Let 's Encrypt提供的免费证书。

使用自签名证书

自签名证书只能用在本地测试环境

# mkdir /etc/ssl/myoir.org
# cd /etc/ssl/myoir.org/
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout  myoir.org.key -out myoir.org.crt

创建密钥

# cat myoir.crt myoir.key >myoir.pem

使用商业CA证书

如果买了商业证书,需要合并私钥、证书和CA包

# cat example.com.key example.com.crt example.com-ca-bundle.crt > /etc/ssl/example.com.pem 

使用Let 's Encrypt提供的免费证书

所有的证书和密钥都放在 **/etc/letsencrypt/live/example.com/**目录下,

所有安装下面的方法创建证书和密钥包:

# cat /etc/letsencrypt/live/example.com/fullchain.pem /etc/letsencrypt/live/example.com/privkey.pem >/etc/letsencrypt/live/example.com/example.com_bundle.pem

拓扑图

varnish cache https

配置Hitch

在配置文件 /etc/hitch/hitch.conf中,

frontend 部分定义了IP地址和端口。
默认配置是监听附加在服务器上的所有IPv4和IPv6的443端口, 处理传入的HTTPS请求,将它们交给Varnish处理。

我们需要将后端代理的端口修改为 8443 (默认是6086)

backend = "[127.0.0.1]:8443"
#pem-dir = "/etc/pki/tls/private"
pem-file = "/etc/ssl/myoir.org/myoir.pem"

启动hitch服务

设置开机自启动, 并直接指定 --now, 会同时启动hitch服务,

# systemctl enable --now hitch
# systemctl status hitch

设置防火墙,开放https

# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload

为Hitch配置Varnish缓存

为Varnish 添加一个新的监听端口 8443, 用于与Hitch通讯。

# systemctl edit --full varnish

找到 ExecStart一行,添加 -a 127.0.0.1:8443,proxy

ExecStart=/usr/sbin/varnishd -a :80 -a 127.0.0.1:8443,proxy -f /etc/varnish/default.vcl -s malloc,256m 

重启服务,使配置生效

# systemctl restart varnish

测试https

使用浏览器打开 https://服务器ip

在Varnish缓存中将HTTP重定向到HTTPS

在Hitch配置文件 /etc/hitch/hitch.conf 中添加以下配置。

  1. vlc 4.0下面添加一行 import std;

  2. 找到 sub vcl_recv, 修改如下:

sub vcl_recv {
    if (std.port(server.ip) != 443) {
        set req.http.location = "https://" + req.http.host + req.url;
        return(synth(301));
    }
}
  1. 找到 sub vcl_synth,修改如下:
sub vcl_synth {
        if (resp.status == 301) {
                set resp.http.location = req.http.location;
		  set resp.status = 301;
                return (deliver);
        }
}
  1. 重启varnish服务,使配置生效
# systemctl restart varnish
  1. 使用浏览器打开 http://服务器ip,检查是否能重定向到 https://服务器ip
在CentOS8中如何为Varnish缓存配置https

Varnish缓存缺乏对SSL/TLS和与端口443关联的其他协议的本地支持。
如果您使用Varnish缓存来提高您的web应用程序的性能,那么您需要安装和配置另一个称为SSL/TLS终止代理的软件,通过它和Varnish缓存一起启用HTTPS。
我们可以使用 Hitch

Hitch是一个免费的开源、基于libe、可扩展的SSL/TLS代理,专为Varnish缓存设计,目前可以在Linux、OpenBSD、FreeBSD和MacOSX上运行。它通过监听端口443 (HTTPS连接的默认端口)来终止TLS/SSL连接,并将未加密的通信转发到Varnish缓存。

日期:2019-04-29 03:17:15 来源:oir作者:oir