为了在 Rancher Server 启用 https 访问,你需要在Rancher Server前使用一个代理服务器代理https请求,并能设置http的头参数。我们会在以下的内容中提供一个使用NGINX、HAProxy或者Apache作为代理的例子。同时我们使用花生壳内网穿透实现外网访问。

  • 需求

除了一般的Rancher Server需求外,你还需要:

  • 有效的SSL证书:如果你的证书并不是标准的Ubuntu CA bundle,请参考以下内容使用自签名证书。
  • 相关域名的DNS配置

1、花生壳HTTPS映射

2、宝塔安装配置

3、宝塔配置负载

3.1、配置 SSL

3.2、配置 Sockets

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''  close;
    }

server
{
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name 192.168.1.130;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/rancher-proxy;

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END

    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-00.conf;
    #PHP-INFO-END

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    #include /www/server/panel/vhost/rewrite/hiwepy.eicp.vip.conf;
    #REWRITE-END

    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null;
    }

    location ~ / {
        # 没有配置OPTIONS的话,浏览器如果是自动识别协议(http or https),那么浏览器的自动OPTIONS请求会返回不能跨域
        if ($request_method = OPTIONS ) {            return 200;
        }
        return 301 https://$server_name$request_uri;
    }

    access_log  /www/wwwlogs/hiwepy.eicp.vip.log;
    error_log  /www/wwwlogs/hiwepy.eicp.vip.error.log;
}
server
{
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    listen [::]:80 default_server;
    server_name hiwepy.eicp.vip;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/rancher-proxy;

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/hiwepy.eicp.vip/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/hiwepy.eicp.vip/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;


    #SSL-END

    #proxy_connect_timeout 30; #Nginx服务器与被代理的服务器建立连接的超时时间 默认60秒 
    #proxy_read_timeout 10; #Nginx服务器被想被代理服务器组发出read请求后,等待响应的超时时间,默认60秒 
    #proxy_send_timeout 30; #Nginx服务器想被代理服务器组发出write请求后,等待响应的超时时间,默认60秒 
    #proxy_ignore_client_abort off; # 客户端断网时,Nginx服务器是否终结对代理服务器的请求,默认off

    location ~ / {
         # 没有配置OPTIONS的话,浏览器如果是自动识别协议(http or https),那么浏览器的自动OPTIONS请求会返回不能跨域
        if ($request_method = OPTIONS ) {            return 200;
        }
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #rewrite ^/api/(.*)$ /$1 break;
        #rewrite ^(.*)$ https://192.168.1.130 permanent;
        #proxy_pass http://gateways;
        #rewrite ^(.*) https://$server_name$1 permanent;
        proxy_pass https://192.168.1.130;
        proxy_redirect off;
        # 配置参数(重要)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        # This allows the ability for the execute shell window to remain open for up to 15 minutes. Without this parameter, the default is 1 minute and will automatically close.
        proxy_connect_timeout 60s;
        proxy_read_timeout 360s;
        proxy_send_timeout 900s;

    }

    access_log  /www/wwwlogs/hiwepy.eicp.vip.log;
    error_log  /www/wwwlogs/hiwepy.eicp.vip.error.log;
}

参考资料

https://rancher.com/docs/rancher/v1.6/zh/installing-rancher/installing-server/basic-ssl-config/

作者:Jeebiz  创建时间:2020-01-11 20:27
 更新时间:2024-08-02 14:28