Redirect http port 8080 to https 80


(Milad Ranjbar) #1

I am trying to configure Caddy to do the following:
redirect all requests for (http://example.com:8080/) --> (https://.example.com/)

http://example.com:8080 {
        redir https://{host}{uri}
}

 https://example.com {
         proxy / localhost:3000
        tls someone@gmail.com
}

but when i enter http://example.com:8080 in my browser it my broswer says:
# This site can’t provide a secure connection

**example.com**  sent an invalid response.

* [Try running Windows Network Diagnostics](javascript:diagnoseErrors()).

ERR_SSL_PROTOCOL_ERROR

(Matthew Fay) #2

This part in particular is the problem. {host} here resolves to example.com:8080, so when Caddy issues that redirect, the client is going to try connect to https://example.com:8080/ (which, as we know, is a HTTP listener).

Change {host} to {hostonly} to give the hostname without the port information and redirect the client to https://example.com/ instead.

https://caddyserver.com/docs/placeholders


(Milad Ranjbar) #3

thanks, i changed caddy config file according to what you said, but still broswer tried to open https://example.com:8080/ getting error from browser that said:

 This site can’t provide a secure connection
**example.com**  sent an invalid response.
* [Try running Windows Network Diagnostics](javascript:diagnoseErrors()).
ERR_SSL_PROTOCOL_ERROR

(Matthew Fay) #4

Try browsing to http://bisimapp.com:8080 instead of https://bisimapp.com:8080. You should then be redirected to https://bisimapp.com/ (which should connect over port 443, as the default for HTTPS).

It’s impossible to run HTTP and HTTPS on the same port.


(Milad Ranjbar) #5

i tried to browsing to http://bisimapp.com:8080 but broswer or caddy redirect me to https://bisimapp.com:8080


(Milad Ranjbar) #6

ok thanks, it is working now :slight_smile: (i changed my browser maybe because of cache).

another question i sent a post request to http://example.com:8080/api/test
but it return me Cannot GET /api/test is it because of redirect? so how to solve in this situation or maybe i should changed from redirect to proxy / https://{hostonly}{uri}


(Matthew Fay) #7

This works in my testing:

root@erasmus:~# caddy -version
Caddy 0.11.0 (non-commercial use only)

root@erasmus:~# cat Caddyfile
http://localhost:8080 {
  redir https://{hostonly}{uri}
}

https://localhost {
  tls self_signed
  status 200 /
}
root@erasmus:~# curl -kIL localhost:8080
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://localhost/
Server: Caddy
Date: Fri, 02 Nov 2018 08:04:26 GMT

HTTP/1.1 200 OK
Server: Caddy
Date: Fri, 02 Nov 2018 08:04:26 GMT

I would check your browser cache is not interfering.


(Matthew Fay) #8

Ahh, you beat me by about a second! Glad to hear it’s working.

As for the other question, this StackExchange post will likely be a good resource:

In essence, a 301 redirected POST becomes a GET. You can issue a different status - 308 - to indicate a permanent redirect that should remain as a POST request. I’d highly suggest reading further into the “potential for abuse” elaborated on in that StackExchange post before going ahead, though.


(Milad Ranjbar) #9

your help is amazing, but i’m newbie to caddy and my web service is down and my boss want kill me :smile: so security is not matter at this time.
how can do this in caddy according to what you said?


(Matthew Fay) #10

No worries! You’d just specify the status code explicitly in the redir directive. You can’t use the shorthand redir [target], though, you have to type out the full form. It looks like this:

redir 308 {
  / https://{hostonly}{uri}
}

https://caddyserver.com/docs/redir


Ninja edit: The semi-shorthand redir / https://{hostonly}{uri} 308 should work, too, as a one-line option.


(Milad Ranjbar) #11

oh caddy is easy to config. cheers