HAproxy将所有根请求重定向到www

时间:2016-11-10 11:19:19

标签: redirect haproxy

我刚刚在服务器上安装了HAproxy,该服务器不应该作为任何传入裸域请求(http和https)的重定向端点,而是www. - 版本。就像服务wwwizer.com一样。这是我的cfg:

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # Default ciphers to use on SSL-enabled listening sockets.
        # For more information, see ciphers(1SSL). This list is from:
        #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
        ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
        ssl-default-bind-options no-sslv3

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend http
        bind *:80
        bind *:443

        acl has_www hdr_beg(host) -i www
        http-request redirect code 301 location www.%[hdr(host)]%[req.uri] unless has_www 

我的域DNS的A记录指向服务器的IP。但是,如果我转到http://exmaple_url.com,我会转到:http://exmaple_url.com/www.exmaple_url.com,而不仅仅是http://www.exmaple_url.com

我做错了什么?

我按照文档中的说明进行操作:https://www.haproxy.com/doc/aloha/7.0/haproxy/http_redirection.html

2 个答案:

答案 0 :(得分:2)

我希望在重定向位置添加https://是您需要的修补程序:

http-request redirect code 301 location https://www.%[hdr(host)]%[req.uri] unless has_www

答案 1 :(得分:0)

使用此答案的任何人的更新。规范肯定已经改变了,我需要使用这个修改过的 http-request 来工作:

http-request redirect code 301 location https://www.%[hdr(host)]%[capture.req.uri] unless has_www

根据第 7.3.6 节。关于 req.uri 的文档:
https://www.haproxy.org/download/2.4/doc/configuration.txt

这是我使用 HA-Proxy 版本 2.3.9-53945bf 2021/03/30 的经验