HAProxy http passtrough proxy instead of 304 redirect - reverse-proxy

I am trying to get the equivalent of the nginx passtrough where a user would see my website url in the address bar but get all the content to be proxied from another website.
At the moment the code below redirects the user to example2 by returning a 304, instead of proxying the traffic.
I need it to work with http (not tcp) because I need this as part of an AB test where I need to inspect the cookies. Please check the comments on the code below for what I am trying to do.
defaults
mode http
log global
option httplog
log 127.0.0.1 local0
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen http
bind 127.0.0.1:8080
acl set_cookie path_beg /set-cookie
use_backend b-backend if { req.cook(SITEID) -m beg b-backend }
use_backend b-backend if set_cookie
default_backend ab-split-backend
backend a-backend
option forwardfor
server example1 example1.com:443 check maxconn 3000 inter 30s
backend b-backend
cookie SITEID insert
option http_proxy
# how do I get example2 to passtrough and not 304 redirect?
server example2 example2.com:443 check maxconn 3000 inter 30s
backend ab-split-backend
balance roundrobin
cookie SITEID insert indirect nocache maxlife 48h
# how do I get example2 to passtrough?
server example2 example2.com:443 weight 50 cookie b-backend
server example1 example1.com:443 weight 50 cookie a-backend

HTTP 304 is not really a redirect, it is an empty response indicating Not Modified which tells the client that the server would have responded with a 200 and served the content, but the requested asset has not changed, so the client can just use what it has cached.
So I'm not entirely sure what you're seeing is incorrect behavior. That is, your requests may be being passed through just fine, and the backend server may be correctly responding with a 304.
The server makes the decision to respond with this code based on information provided in the request headers If-Modified-Since and/or If-None-Match. So if you really want to disable this caching mechanism and ensure a complete 200 response every time, you can instruct HAProxy to delete these headers from the incoming request:
listen http
bind 127.0.0.1:8080
acl set_cookie path_beg /set-cookie
# Delete headers related to caching
http-request del-header If-Modified-Since
http-request del-header If-None-Match
use_backend b-backend if { req.cook(SITEID) -m beg b-backend }
use_backend b-backend if set_cookie
default_backend ab-split-backend

it looks like what you are trying to do is keep your system from trying to pass it through via SSL and instead do clear text based assessments for testing purposes. I would recommend seeing a snippet from my config below on http-request redirects and also look into HAProxy schemes. I would also recommend seeing the additional example for instance based redirection, specifically for a dictated location, that way you dont unencrypt traffic accidentally that you want to remain encrypted.
As for the information proxied from another location, your best bet for that would be with using Cloudflare, especially if you are looking for some form of DDoS/additional layers of security. The alternative is building your own custom anti-DDoS Solution, which is a major pain.
frontend ALL
bind *:80
bind *:443 alpn h2,http/1.1 ssl crt /etc/haproxy/certs/eduarmor.com.pem
http-request redirect scheme https code 301 if !{ ssl_fc }
http-request redirect code 301 location http://www.%[hdr(host)]%[capture.req.uri] if { hdr(Host) -i eduarmor.com }
mode http

Related

Redirect ActiveMQ web console with HAProxy

I am new to HAProxy and I am trying to do a simple redirect from <server_ip>/mq to <server_ip>:8161/admin
When I am trying to access ActiveMQ directly via <server_ip>:8161/admin everything works well.
But with the HAProxy sometimes I get 503. And when it works it doesn't load the resources at all (no css, no images..)
frontend http
bind *:80
timeout client 60s
mode http
acl app path_end -i /mq
use_backend appServer if app
default_backend all
backend appServer
timeout connect 10s
timeout server 10s
mode http
http-request set-path /admin/
server servermq <server_ip>:8161
backend all
timeout connect 10s
timeout server 10s
mode http
http-request set-path /admin/
server servermq <server_ip>:8161
In the dev tools I can see errors like this:
Refused to apply style from 'http://<server_ip>/admin/styles/site.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
But I am not sure how to fix this.
Let's address the issues one by one.
Intermittent 503
This is most likely due to more than one haproxy processes running in the system. You can find all of them by a simple ps -ef | grep haproxy. Kill all but one and it should be fixed. Another reason might be due to two frontends binding on same port which in this case is :80. Check for all the frontend where binding port is 80. Change each one of them to be different from the other.
CSS and JS not loading which is related to the mismatched MIME type error posted
There is an issue with your haproxy configuration in the backend all section. According to your frontend http rules, all request ending with /mq should go to backend appServer where a url rewrite is happening via http-request set-path /admin/. /mq is replaced with /admin/ before sending it to servermq. This is correct but when you're loading the activemq admin in the browser, the browser is making requests for *.css and *.js files which is coming empty. This is because the browser is making request on /admin/styles/sorttable.css file. Now this doesn't match with your acl app but goes to default_backend all. Here due to this rule http-request set-path /admin/ again path is set to /admin/ which returns 200 but without any css file, that's why the error:
Refused to apply style from 'http://<server_ip>/admin/styles/site.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
The correct config should be:
frontend http
bind *:80
timeout client 60s
mode http
acl app path_end -i /mq
use_backend appServer if app
default_backend all
backend appServer
timeout connect 10s
timeout server 10s
mode http
http-request set-path /admin/
server servermq <server_ip>:8161
backend all
timeout connect 10s
timeout server 10s
mode http
server servermq <server_ip>:8161

How to make HAProxy's SSL redirect and path rewrite (with reqrep) work at the same time?

I need to deploy different apps on the same domain, thus I've set up the backend to rewrite URL with reqrep ^([^\ ]*\ /)appA[/]?(.*) \1\2. It works when I only deploy the HTTP or HTTPS version of the app.
However, if I try to redirect all HTTP traffic to HTTPS, it doesn't work. The problem is that HAProxy has already rewritten the URL and stripped out the /appA part before the redirection. Thus if I try to visit http://myserver.com/appA, the page https://myserver.com will eventually be requested instead of https://myserver.com/appA.
I can't put the redirect rule before the reqrep rule as it seems that HAProxy must process all the rewrites before the redirects.
What can I do to make my configuration work as I intended? This should be obvious but I can't seem to find a relevant answer online.
My configuration:
frontend http_front
bind *:80
reqadd X-Forwarded-Proto:\ http
acl appA_url url_beg /appA
use_backend appA if appA_url
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/myserver.com.pem
reqadd X-Forwarded-Proto:\ https
acl appA_url url_beg /appA
use_backend appA if appA_url
backend appA
reqrep ^([^\ ]*\ /)appA[/]?(.*) \1\2
redirect scheme https if !{ ssl_fc }
balance roundrobin
server web1 127.0.0.1:5001 check
Use http-request directives, which are processed in declararion order. They are also newer features, and usually cleaner, more intuitive, more flexible, and more internally efficient than reqxxx.
http-request redirect scheme https if ! { ssl_fc }
http-request set-path %[path,regsub(^/appA/,/)]
See http-request. Requires 1.6 or later, where the regsub() converter is available.

HAProxy rewrite HTTP requests based on HTTP method

I have REST API.
For the sake of simplicity lets say I have two services:
read.request.com
write.request.com
How I can rewrite all read requests (GET method) to read.request.com and all write requests (POST, PUT, DELETE methods) to write.request.com with HAProxy?
Not quite sure which of these is applicable for your situation but hopefully one is.
One Backend
I think this is your situation.
frontend http-in
bind *:80
acl is_post method POST
acl is_get method GET
http-request set-header Host write.request.com if is_post
http-request set-header Host read.request.com if is_get
default_backend api
backend api
server one localhost:8080 check
All this does is check which method is being used and sets the Host header accordingly before passing the request on to localhost:8080.
Two Backends
In this set up you have have one instance of the code running for just read requests and another instance just for write requests. In this case the read code is running on localhost:8080 and the write code is running on localhost:8081.
frontend http-in
bind *:80
acl is_post method POST
acl is_get method GET
use_backend write if is_post
use_backend read if is_get
backend write
http-request set-header Host write.request.com #optional
server write_one localhost:8081 check
backend read
http-request set-header Host read.request.com #optional
server read_one localhost:8080 check
This option starts the same as the previous one by checking which method is being used but instead of using one HAProxy backend it splits out into two. The http-request line inside each backend is optional for this configuration.
Hope that helps.

HAproxy - Proxies the whole IP

In order to test every possible solution to get Socket.io working with a parallel Apache installation, I have now installed HAproxy that listens on port 80. It proxies everything to Apache, unless the hostname equals io.server.com.
We have two IPs connected to our server: 1 is for SSL, the other for all the NON-SSL subdomains we have. I have created the io.server.com subdomain to point to that NON-SSL IP-address. However, the following this occurs:
A visit to regular_http.server.com results in Apache handling that sub domain (OK)
A visit to io.server.com results in "Welcome to Socket.io" (OK)
Next visit to regular_http.example.com results in "Welcome to Socket.io"
Why is HAproxy sending requests from a subdomain not configured to go to Socket.io, to Socket.io ?
Yes, the two sub domains share the IP, but is HAproxy really proxying the whole IP under one? What is then the point with setting up ACLs based on host name?
Here's my configuration:
global
daemon
maxconn 4096
user haproxy
group haproxy
defaults
log global
#this frontend interface receives the incoming http requests
frontend http-in
mode http
bind *:80
timeout client 86400000
#default behavior sends the requests to apache
default_backend www_backend
#when "io.test.tld" is matched, an acl I call arbitrarily
# "websocket" triggers
acl websocket hdr_end(host) -i io.server.com
use_backend node_backend if websocket
Thank you!
This problem was solved using the option http-server-close configuration value in HAproxy.

Why does haproxy path_beg only work if I don't visit default site?

I have configured haproxy to redirect the path "/rawman" to port 8080 on my server. It works the first time, but as soon as I visit the default site it stops working. The default site is running on apache with mod_rewrite and it is catching invalid requests (using codeigniter) so instead of seeing the redirected site when I visit http://mysite.com/rawman?foo=bar I see the default site.
This is my haproxy config:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http_proxy
bind 0.0.0.0:8090
acl is_ast path_beg /rawman
use_backend ast if is_ast
default_backend mysite
backend ast
server ast 0.0.0.0:8080
backend mysite
server local 0.0.0.0:80
Try setting option httpclose after the srvtimeout line.
If you don't do that then haproxy uses the target server's keepalive setting. Once you visit the main site the connection is opened and kept open, and on your next request haproxy goes oh isn't that nice: I have an open connection. Lets just use it even though it shouldn't. With the httpclose option set it always closes the connection, ensuring that each new request uses the right connection.
Lost 3 hours of my life figuring that out.