将nginx设置为Github页面前端,并启用https

将nginx设置为Github页面前端,并启用https,nginx,github-pages,Nginx,Github Pages,我想为Github页面使用自定义域,使用我自己的证书(不是CloudFlare证书)启用HTTPS 根据Github Pages说明,第一部分是通过在项目根文件夹和DNS中设置CNAME来完成的,但这种方式不支持HTTPS:https://example.github.io自定义域example.com只能在http://example.com,https://example.com不起作用 因此我想到了nginx反向代理:删除CNAME文件和DNS设置,让3个链接共存,将http自定义域重定向

我想为Github页面使用自定义域,使用我自己的证书(不是CloudFlare证书)启用HTTPS

根据Github Pages说明,第一部分是通过在项目根文件夹和DNS中设置CNAME来完成的,但这种方式不支持HTTPS:
https://example.github.io
自定义域
example.com
只能在
http://example.com
https://example.com
不起作用

因此我想到了nginx反向代理:删除CNAME文件和DNS设置,让3个链接共存,将http自定义域重定向到https-one,将https-one的请求转发到
github.io
地址

然而,结果并不完美:主页和css(in/css/main.css!)加载正确,所有链接都正常显示,但单击它们将生成301并重定向到github.io

我的nginx版本是1.9.5,端口80的配置:

server {
    listen 80;
    server_name myblog.com;
    return 301 https://$server_name$request_uri;
}
对于443:

server {
    listen 443 ssl http2;
    server_name myblog.com;
    ssl_certificate /etc/nginx/ssl/orz.crt;
    ssl_certificate_key /etc/nginx/ssl/orz.key;
    add_header Strict-Transport-Security max-age=31536000;

    location / {
        proxy_pass https://example.github.io;
        proxy_set_header Host example.github.io;
    }
}

通过添加2行进行修复:

proxy_redirect https://example.github.io https://example.com;
proxy_redirect http://example.github.io https://example.com;
顺便说一句,如果你想在Github页面上托管Jekyll/Ghost,请确保你的帖子的永久链接以
/
结尾,否则会花费301美元

如果使用Jekyll,在
server
block上为css文件添加服务器端推送:

add_header Link '</css/main.css>; rel=preload; as=stylesheet';
add_header Link';rel=预载;as=样式表';