Using HAProxy 2.6.6, here is an example HAProxy.cfg to proxy over plain
HTTP:
# If you already have an haproxy.cfg file, you can probably leave the
# global and defaults section as-is, but you might need to increase the
# timeouts so that long-running CLI commands will work.
global
maxconn 4096
log stdout local0 debug
defaults
log global
option httplog
option dontlognull
option forwardfor
maxconn 20
timeout connect 5s
timeout client 60s
timeout server 60s
frontend http-in
log stdout format raw local0 debug #for additional logging
bind *:80
mode http
acl prefixed-with-jenkins path_beg /jenkins
http-request redirect code 301 prefix /jenkins unless prefixed-with-jenkins #Use http-request redirect prefix to add /jenkins prefix to the URL's location to ensure jenkins base url is working properly.
use_backend jenkins if prefixed-with-jenkins
backend jenkins
log stdout format raw local0 debug
mode http
server jenkins1 127.0.0.1:8080 check
http-request replace-path /jenkins(/)?(.*) /\2
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
http-request set-header X-Forwarded-Host %[req.hdr(Host)]
This assumes Jenkins is running locally on port 8080.
This assumes that you are using the /jenkins context path for both the
site exposed from HAProxy, and Jenkins itself.
If this is not the case, you will need to adjust the configuration.
Refer to the HAProxy documentation on Traffic Routing for more information.
If you are experiencing the following error when attempting to run long
CLI commands in Jenkins, and Jenkins is running behind HAProxy,
it is probably due to HAProxy timing out the CLI connection.
You can increase the timeout client
and timeout server
settings as
necessary so the command will complete successfully.
WARNING: null
hudson.cli.DiagnosedStreamCorruptionException
Read back: 0x00 0x00 0x00 0x1e 0x07
'Started reverse-proxy-test #68'
0x00 0x00 0x00 0x01 0x07 0x0a
Read ahead:
Diagnosis problem:
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
...
at hudson.cli.FlightRecorderInputStream.analyzeCrash(FlightRecorderInputStream.java:82)
at hudson.cli.PlainCLIProtocol$EitherSide$Reader.run(PlainCLIProtocol.java:153)
Caused by: java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
...
at java.io.DataInputStream.readInt(DataInputStream.java:387)
at hudson.cli.PlainCLIProtocol$EitherSide$Reader.run(PlainCLIProtocol.java:111)