1

I would like to make my apache2 webserver serve both http and https on the same port.
With the different method i tried it was either not working on http or on https..

How can I do this?

Update:
If I enable SSL and then visit the with http I get page like this:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />
Instead use the HTTPS scheme to access this URL, please.<br />
<blockquote>Hint: <a href="https://server/"><b>https://server/</b></a></blockquote></p>
<hr>
<address>Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny16 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g Server at server Port 443</address>
</body></html>

Because of this, it seems very much possible to have both http and https on the same port.
A first step would be to change this default-page so it would present a 301-Moved header.

Update2: According to this, it is possible. Now, the question is just how to configure apache to do it.

6
  • First, it would be a great idea to tell us what method you tried that didn't work. Second, why want you do this? Standard port for https is 443, and if you change that to e.g. 80, you'll need to open the site in the browser like this: https://example.com:80. Third: I don't believe it is possible anyway.
    – Sven
    Sep 22, 2012 at 15:49
  • We are having other services on non-standard ports like 999. Now I want it to be possible for our users to use both server:999 and server:999.
    – Zulakis
    Sep 22, 2012 at 15:52
  • Apache is even telling you that it can't do what you want.
    – Sven
    Sep 22, 2012 at 16:01
  • No it is not. It is telling me that it does only accept https connections (for security reasons, obviously). That it is ACTUALY TELLING me this is proof that it can do both http and https on the same port.
    – Zulakis
    Sep 22, 2012 at 16:06
  • 1
    @Zulakis Yes, it's possible to detect the protocol that the client is attempting to use before the server sends any response - but it's not possible to configure Apache to serve up anything other than errors via HTTP on an HTTPS port. To do this, you'd probably need to heavily modify mod_ssl. Sep 22, 2012 at 16:39

4 Answers 4

2

SSL trafic can not work on same port as non-SSL traffic, TLS can do that but it is not used on web servers TLS is mostls used on SMTP server precisesly because of that reason

The same question was already asked several times (see two examples beneath) by different people, and it was always said, it is not possible by using Apache alone, you would have to use some other software or make your own listener of some kind:

Apache2 Rewrite http to https on port 5553

Apache answer both HTTP and HTTPS on the same port

Also I could not find any official Apache document that mentions http and https traffic on same port without some redirecting.

Keep in mind that when people say SSL they usually mean SSL/TLS which are not the same.

I would take this answer from apache.org as a definitive NO to your question to working with http and https on same port

    Can we have both http and https listening on the same port?

Comment 1 Eric Covener 2011-11-22 14:36:45 UTC

No, and bugzilla is not for support or Q&A.  Try the users mailing list.

https://issues.apache.org/bugzilla/show_bug.cgi?id=52228

You can try the mod_rewrite module but I suggest that you open another question on how to use it to make https redirects (you will still need to have multiple ports). I haven't worked with it so can't give you details about how to do it.

Like I said don't know much about the module so my guess is that you would need three ports. One non-SSL port for mod_rewrite to listen, one non-SSL for http traffic and one SSL port for https, you would have to make the 999 port for mod_rewrite and then have your services listen on two different port for http and https.

Pay attention to what Shane Madden said in his comment, http request on SSL enabled port would only give you errors even if you put mod_rewrite on that port.

14
  • Please have a look at my updated initial post.
    – Zulakis
    Sep 22, 2012 at 15:56
  • check this two posts. stackoverflow.com/questions/5327850/…, blog.magicaltux.net/2008/12/07/…
    – ralz
    Sep 22, 2012 at 16:00
  • Okay, according to the posts it's possible. Now, how can I do this with apache2?
    – Zulakis
    Sep 22, 2012 at 16:03
  • 1
    SSL is predecessor to TLS, and as far as I know (could be wrong) https connections on server side use SSL connection. TLS send first hello message in clear text and then sees if it gets encrypted or unecrypted response and based on that client send data either in clear text pr encrypted. SSL starts with encyrption from start so neither the server or the client can choose the type of traffic
    – ralz
    Sep 22, 2012 at 16:10
  • 1
    @Zulakis take a look at my answer, I edited it, I say definitely not possible
    – ralz
    Sep 22, 2012 at 16:38
2

It is possible to redirect http to https by using a custom ErrorDocument.
Having the whole webserver run on both http and https is not possible at the current version of Apache though.

Sadly, there is a bug in Apache 2.2-16 which has not been fixed yet because of which the redirection does only work in Apache 2.4.

For further infos have a look here: https://issues.apache.org/bugzilla/show_bug.cgi?id=50823

Update
Here is a proof-of-concept snippet I testet with apache 2.4:

<?php
if ($_SERVER["REDIRECT_STATUS"] == "400" && preg_match("/.*?Reason: You're speaking plain HTTP to an SSL-enabled server port\..*/", $_SERVER["REDIRECT_ERROR_NOTES"])) {
    header("Location: https://localhost:999");
} else {
    //echo normal error message
}
?>

Use it by setting ErrorDocument 400 /redirect-400-error.php in your apache config file. You can find more info on the implementation of custom ErrorDocuments here.

1

Easy Solution:

ErrorDocument 400 https://server:port/

Now you get a "Found" redirect, and no nasty error message.

3
  • That's pretty much the same solution I came up with above. Just without the checking if it's actually a error with http:// instead of https://
    – Zulakis
    Oct 19, 2012 at 15:57
  • How is that not checking the error? Apache already knows there's an error, thus you have a 400 status to begin with. What you did with your code was rewrite something that Apache already did ... You're overthinking it. ;-)
    – Catalyst
    Nov 10, 2012 at 3:43
  • If the code ever produces a 400 error this is clearly wrong. Dec 7, 2015 at 23:23
0

I think it could work by defining a static ErrorDocument eg:

ErrorDocument 400 /redirect_https.txt

Then create the file redirect_https.txt in your document root with the following content:

HTTP/1.1 302 Found
Location: https://www.example.com/mypage.html

This works (even tried) and sends the browser a "faked" HTTP-redirect.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .