加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx – HAProxy根据URI重定向和匹配

发布时间:2020-12-13 21:40:18 所属栏目:Nginx 来源:网络整理
导读:我正在尝试设置一个测试haproxy服务器,它将涵盖2个基本区域.如果用户使用http,则自动重定向到https,但仅当特定的uri部分不存在时才自动重定向到https. 例如,如果用户转到http://www.test.com,则会将其重定向到https://www.test.com.但如果用户转到https://ww

我正在尝试设置一个测试haproxy服务器,它将涵盖2个基本区域.如果用户使用http,则自动重定向到https,但仅当特定的uri部分不存在时才自动重定向到https.

例如,如果用户转到http://www.test.com,则会将其重定向到https://www.test.com.但如果用户转到https://www.test.com/blog或http://www.test.com/blog,则会将其重定向到http://www.test.com/blog.

这是我目前的测试haproxy.cfg.我正在运行haproxy 1.5-dev17

非常感谢任何帮助.

global
  log 127.0.0.1   local0
  log 127.0.0.1   local1 notice
  maxconn 15000
  user haproxy
  group haproxy

defaults
  log     global
  mode    http
  option  httplog
  option  dontlognull
  option abortonclose
  option  http-server-close
  option redispatch
  retries 3  
  timeout queue 600s
  timeout connect 9s
  timeout client 60s
  timeout server 60s
  balance  roundrobin

# Set up application listeners here.

frontend incoming
  bind *:80 name http

  acl has_blog_uri path /blog /blog/

  redirect scheme https if !has_blog_uri !{ ssl_fc }

  bind *:443 ssl crt /etc/haproxy/test.pem


  use_backend blog_app if has_blog_uri

  default_backend rails_app

backend rails_app
  option httpchk GET /app_health
 # server app1 10.1.1.1:8080 weight 1 check
  server app2 10.1.1.2:8080 weight 1 check

backend blog_app

  option httpchk GET /blog/check.txt
  server blog 10.1.1.3:8080 check
最佳答案
我建议一些事情:

>您应该将不安全和安全的配置分成两个单独的块,这样您就可以更轻松地控制ACL和重定向案例.
>对于重定向,请尝试使用格式重定向位置< absolute_url>如果< conditions>
>要检测URI路径,请尝试使用path_beg -i / blog
>最后,您无法真正将人们从HTTPS连接重定向,这被认为是不安全的,不受支持.

根据这些注释,我建议对配置的传入部分进行修改.这应该重定向http://www.test.com到https://www.test.com并且在尝试https://www.test.com/blog时会失败(您可以在那里放置一个带有用户链接建议的页面).

    frontend public
      bind *:80
      acl has_blog_uri path_beg -i /blog
      redirect location https://www.test.com if !has_blog_uri
      use_backend blog_app if has_blog_uri    

    frontend public-ssl
      bind *:443 ssl crt /etc/haproxy/test.pem
      acl has_blog_uri path_beg -i /blog
      use_backend rails_app if !has_blog_uri

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读