【问题标题】:Setup nginx as Github Pages frontend, with https enabled将 nginx 设置为 Github Pages 前端,启用 https
【发布时间】:2016-02-04 13:36:24
【问题描述】:

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

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

所以我在想nginx反向代理:去掉CNAME文件和DNS设置,让3个链接共存,将http自定义域重定向到https 1,将https 1的请求转发到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;
    }
}

【问题讨论】:

    标签: nginx github-pages


    【解决方案1】:

    通过添加 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 块上添加服务器端推送 css 文件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2016-08-15
      • 2012-06-30
      • 1970-01-01
      • 2020-08-03
      • 2023-04-10
      相关资源
      最近更新 更多