首页 文章

将nginx设置为Github Pages前端,启用https

提问于
浏览
0

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

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

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

但是,结果并不完美:正确加载主页和css(在/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;
    }
}

1 回答

  • 0

    通过添加2行修复:

    proxy_redirect https://example.github.io https://example.com;
    proxy_redirect http://example.github.io https://example.com;
    

    顺便说一下,如果你想在Github页面上主持Jekyll / Ghost,请确保你的帖子的永久链接在 / 结束,否则它将花费另外301 .

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

    add_header Link '</css/main.css>; rel=preload; as=stylesheet';
    

相关问题