大网数据广告活动
服务器运维问答

Is there an alternative for the error_page 497 option from nginx for haproxy?

· 52CE 我爱测

When using nginx I can just use the 497 error code specific to nginx 497 HTTP Request Sent to HTTPS Port and redirect to https with this rule: error_page 497 https://$host:$server_port$request_uri; When using haproxy the only sol…

问题原文

原帖链接 · MADforFUNandHappy · CC BY-SA 4.0

When using nginx I can just use the 497 error code specific to nginx

497 HTTP Request Sent to HTTPS Port

and redirect to https with this rule:

error_page 497 https://$host:$server_port$request_uri;

When using haproxy the only solution I know of is to use two ports e.g. port 80 and 443 but I only have one port (8443).

I tried the errorloc option but when a client tries to connect to the https port I get an ssl handshare error that can not be catched with the errorloc option.

Ideally I would use the following but that does not work:

frontend http-in
    bind :8443 ssl crt /usr/local/etc/haproxy/ssl/fullchain.pem alpn h2,http/1.1
    redirect scheme https code 301 if !{ ssl_fc }
    maxconn 50

    default_backend backend-server

已采纳解答

原帖链接 · MADforFUNandHappy · CC BY-SA 4.0

After some research it turns out that this can indeed be achieved with HAProxy (one port for https and http and redirecting all http requests to https)

I finally found a solution in the HAProxy forum from lukastribus.

The following example is from lukastribus:

frontend port801_combined
    mode tcp
    bind :801
    tcp-request inspect-delay 2s
    tcp-request content accept if HTTP
    tcp-request content accept if { req.ssl_hello_type 1 }
    use_backend recir_http if HTTP
    default_backend recir_https

backend recir_http
    mode tcp
    server loopback-for-http abns@haproxy-http send-proxy-v2
backend recir_https
    mode tcp
    server loopback-for-https abns@haproxy-https send-proxy-v2

frontend fe-https
    mode http
    bind abns@haproxy-https accept-proxy ssl crt /etc/ssl/private/unified-cert-file.pem
    # whatever you need todo for HTTPS traffic
frontend fe-http
    mode http
    bind abns@haproxy-http accept-proxy
    # whatever you need todo for HTTP traffic

By adding

redirect scheme https code 301

To the frontend fe-http section like this:

frontend fe-http
    mode http
    bind abns@haproxy-http accept-proxy
    redirect scheme https code 301

All http requests will be redirected to https.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。