How to specified HTTPS redirect port?

Hello, I am running a ssr server and a caddy in the same server. I open 80 port and 443 port for ssr server. while my caddy use 2014 port for http and 2016 for https. my firewall only open 80 and 443. when 443 port recive a request , ssr will redirect to 2016 so caddy can handle the request and no need browser send another request. But when 80 port receive a request ,ssr redirect to 2014,caddy return 301 code and ask browser redirect to 2016 so browser will get a 404 code. Here comes the question, How to specified caddy redirect HTTPS request to 443 not 2016 port?
I have googled it for serval days , I find nothing ,so could anyone help?

Hi @liamlin, welcome to the Caddy community!

You can define the HTTP listener manually to redirect to the default HTTPS port instead of 2016.

What’s in your Caddyfile at the moment?

Hello, here is my Caddyfile

:80 {
root /usr/local/www
timeouts none
tls off
gzip
}
https://www.mydomian.com:2016 {
root /usr/local/www
timeouts none
tls xx@xx.com
gzip
}

You are probably looking for the http-port and https-port flags.

1 Like

Yep, those flags will do what you need.

Change your sites a bit (note the missing ports):

http:// {
  [...]
}
https://www.mydomain.com {
  [...]
}

Then use -http-port and -https-port flags when running Caddy, specifying what ports you want Caddy to run on. Caddy will handle the rest.

https://caddyserver.com/docs/cli#http-port

Thank you very much . I do have readed the -http-port and -https-port but I didn’t understand it .
Is my Caddyfile specified which ports caddy will listen on while command line flag specified which ports caddy will redirect?

I used -http-port but is still doesn’t work well

caddy -http-port 80 -https-port 443 -conf Caddyfile

Caddyfile

http://www.mydomain.com:80 {
  root /usr/local/www
  timeouts none
  tls off
  gzip
}
https://www.mydomain.com:2016 {
	root /usr/local/www
	timeouts none
	tls xx@xx.com
	gzip
}

When I visit my websit with IE11,http and https both work well,caddy return 200, when I use chrome,only https work well,http will get 301 code and the location still is https://www.mydomain.com:2016

So ,what’s wrong?
Is chrome right or IE right?
I don’t know is my Caddyfile wrong or I runned wrong way?

You missed this part (emphasis added):

You left the ports specified in your Caddyfile. They override the flags. Remove the ports from your Caddyfile and rely on the flags to configure which ports you want Caddy to listen on.

Also, you probably want to use -https-port 2016, not 443.

1 Like

I truly appreciate your timely help,I deleted port in Caddyfile and now can initiate http request with chrome. But it’s said caddy would redirect all http request to https. now it woudn’t. Am I still miss something?

Caddy only redirects HTTP to HTTPS if you have Automatic HTTPS enabled and don’t manually override the HTTP listener.

You’ve overridden the HTTP behaviour:

http://www.mydomain.com:80 {
  root /usr/local/www
  timeouts none
  tls off
  gzip
}

If you remove this site entirely, Automatic HTTPS will replace it with an automatic redirect to HTTPS.

Thanks a million. It works well and could automatic redirect.

1 Like