首页 > 解决方案 > 如何从 /SOMETHING/ 路径访问后端资源

问题描述

我正在尝试通过我的 HAProxy 服务器访问我的路由器 Web 界面。到目前为止,它看起来很容易!但经过一次繁重的搜索之后,我来将我的命运交到你手中。

环境:

它应该以这种方式使用特定路径:

  1. 用户在浏览器中输入此网址: https ://example.com/freebox

  2. HAProxy 配置检测端口 443 上的路径 /freebox,然后将其转发到相应的后端。

  3. 后端看到 /freebox 路径并将其剥离到 / 所以 url 看起来像这样:https ://example.com/

  4. 此 url 与后端 url 匹配(意味着没有特定路径),因此它应该可以工作,但不能。

什么对我有用:

  1. 我可以在两边都使用 SSL 配置。

  2. 我可以检测到正确的路径并提供正确的后端

  3. 我可以更改检测到的路径并提供相应的后端 Frontend: https://example.com/home --> Backend: https://192.168.1.2/domoticz

我试图解决我的问题:

  1. 在请求到达后端之前,我做了几件事:
Frontend www-https
...
    acl path_freebox url_beg /freebox
    http-request set-header         X-Location-Path %[capture.req.uri] if path_freebox
    http-request replace-header     X-Location-Path /freebox / if path_freebox
    http-request redirect location  %[hdr(X-Location-Path)] if path_freebox
    use_backend backend_freebox if path_freebox
...
  1. 我已经将 /freebox 剥离到 / (仅供参考,“或”在 conf 文件中):
backend backend_freebox
    reqirep ^([^\ :]*)\ /freebox/(.*)     \1\2
    or
    reqrep ^([^\ ]*\ /)freebox[/]?(.*)    \1\2
    or
    reqirep ^([^\ :]*)\ /freebox/(.*)  \1\ /\2
    or
    reqrep ^([^\ ]*\ /)freebox[/]?(.*)     \1\/\2
    or
    http-request set-path %[path,regsub(^/freebox/?,/)]
    or
    http-request set-uri %[url,regsub(^/freebox,/,)]

    server freebox 192.168.1.254:443 ssl verify none
  1. 我设置了标题然后删除路径:
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }
    reqirep ^([^\ :]*)\ /freebox/(.*)     \1\2

    server freebox 192.168.1.254:443 ssl verify none
  1. 我改变了原点,然后去掉了尾随路径:
    http-request set-header Orig-Path /freebox/
    http-request set-header X-Script-Path /freebox/
    http-request set-header Host example.com
    or
    http-request set-header Orig-Path /freebox
    http-request set-header X-Script-Path /freebox
    http-request set-header Host example.com

    reqirep ^([^\ :]*)\ /freebox/(.*)     \1\2
    server freebox 192.168.1.254:443 ssl verify none

这一半有效,我可以使用该方法查看门户网站,但无法使用它,因为上面写了“错误”。

我的完整 HAProxy 配置:

global  
    # Logging to rsyslog on localhost local2
    log 127.0.0.1 local2 debug

    # System Security
    #chroot /var/lib/haproxy
    #stats socket /run/haproxy/admin.sock mode 660 level admin
    #stats timeout 30s
    #user haproxy
    #group haproxy

    # Start as daemon
    daemon

    # Default 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 no-tls-tickets
    ssl-default-server-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-server-options no-sslv3 no-tls-tickets
    tune.ssl.default-dh-param 2048

defaults
    mode                    tcp
    log                     global
    option                  httplog
    option                  dontlognull
    option          http-server-close
    option          forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
    errorfile 400 /usr/local/etc/haproxy/errors/400.http
    errorfile 403 /usr/local/etc/haproxy/errors/403.http
    errorfile 408 /usr/local/etc/haproxy/errors/408.http
    errorfile 500 /usr/local/etc/haproxy/errors/500.http
    errorfile 502 /usr/local/etc/haproxy/errors/502.http
    errorfile 503 /usr/local/etc/haproxy/errors/503.http
    errorfile 504 /usr/local/etc/haproxy/errors/504.http

frontend www-https
    # Bind this frontend to the port 443 with ssl configuration
    bind *:443 ssl crt /etc/ssl/example.com

    mode http   

    # Full logging to localhost
    log 127.0.0.1 local2
    option httplog

    # Whitelist
    #acl network_allowed src 20.30.40.50 8.9.9.0/27
    #tcp-request connection reject if !network_allowed

    # Pour forwarder l'identité des clients (To be verified)
    reqadd X-Forwarded-Proto:\ http
    reqadd X-Forwarded-Proto:\ https

    # Default Backend
    default_backend backend_trash

    # ACLs to determine the backend defined on the path
    acl path_domoticz url_beg /domoticz/
    use_backend backend_domoticz if path_domoticz

    acl path_gitlab url_beg /gitlab
    use_backend backend_gitlab if path_gitlab

    acl path_pihole url_beg /pihole
    use_backend backend_pihole if path_pihole

    acl path_freebox url_beg /freebox
    use_backend backend_freebox if path_freebox

    #acl path_orbi url_beg /orbi
    #use_backend backend_orbi if path_orbi

# Backend Configuration
backend backend_domoticz
    mode http
    server domoticz 192.168.1.2:443 ssl verify none

backend backend_gitlab
    mode http
    server gitlab 192.168.1.6:443 ssl verify none

backend backend_pihole
    mode http
    reqirep ^([^\ :]*)\ /pihole/(.*)     \1\ /hole/\2
    server hole 192.168.1.8:443 ssl verify none

backend backend_freebox
    mode http
    #option forwardfor
    #http-request set-header X-Forwarded-Port %[dst_port]
    #http-request add-header X-Forwarded-Proto https if { ssl_fc }
    #option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    #reqrep ^([^\ ]*\ /)freebox[/]?(.*)    \1\2
    #reqirep ^([^\ :]*)\ /freebox/(.*)     \1\2
    #http-request set-uri %[url,regsub(^/freebox,/,)]
    #http-request set-path %[path,regsub(^/freebox/?,/)]

    #http-request set-header Orig-Path /freebox/
    #http-request set-header X-Script-Path /freebox/
    #http-request set-header Host starfly.ovh
    #reqirep ^([^\ :]*)\ /freebox/(.*)  \1\ /\2
    #reqrep ^([^\ ]*\ /)freebox[/]?(.*)     \1\/\2

    server freebox 192.168.1.254:443 ssl verify none

#backend backend_orbi
#(Not Working)
    #mode http
        #reqirep ^([^\ :]*)\ /orbi/(.*)     \1\2
        #server orbi 192.168.1.56:443 ssl verify none

backend backend_trash
    mode http
    http-request deny

预期行为:

我希望我可以保留我的尾随路径 /freebox 并且仍然可以访问 / 上的后端。

不知道有没有可能,大佬能帮我看看吗?

非常感谢 !

标签: haproxy

解决方案


推荐阅读