问题原文
原帖链接 · Kaboom · CC BY-SA 3.0
I am hosting a control panel for web management on a second Apache instance that runs on port 2083. It has port 2082 for the HTTP version of the site because it needed something to run the HTTP version on but the port is closed. When the user connects to the site like so:
https://example.com:2083/
They see the web panel. However, when they visit the site like this:
http://example.com:2083/
They get the error
Bad Request
Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.
There is no link to click to continue. Instead of showing this error, I would like to automatically force the HTTPS protocol on the URL so they use the secured version of the portal. Since 2083 is being used for the SSL port on this panel, it causes a conflict if they don't specify HTTPS. And using the .htaccess file to add a redirect does not work since the port is loaded and sees the error before it loads anything else, ending there without ever reading the .htaccess file.
I am running Apache 2.4.23 on both the main version of the site (hosted on a Windows machine) and on the control panel. As far as I am aware this is the newest available model for a win64 model machine.
Is there any way to force the HTTPS before it loads anything else?
Here is the current httpd-ssl file
Listen 2083
SSLCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS"
SSLProxyCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS"
SSLHonorCipherOrder on
SSLProtocol all -SSLv2 -SSLv3
SSLProxyProtocol all -SSLv2 -SSLv3
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:c:/panel/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
<VirtualHost *:2083>
SSLEngine on
SSLCertificateFile "c:/panel/conf/ssl/sslcert.crt"
SSLCertificateKeyFile "c:/panel/conf/ssl/sslcert.key"
SSLCertificateChainFile "c:/panel/conf/ssl/sslcert.ca-bundle"
ServerName https://sitename.com
ServerAlias www.example.com
DocumentRoot "c:/panel/htdocs/"
</VirtualHost>
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
As Simon has already pointed out (and you surmise in your question), this does not seem to be possible directly from the server. Since a request for http://example.com:2083/ simply can't be handled by the server (when it already handles https://example.com:2083/). As the response indicates, it's simply a "Bad Request".
Any solution would need to come from the client end (or some intermediary device) that knows to "upgrade" the request, before actually making the request to your server. There are browser extensions that might be able to handle this - but I suspect that installing something on the client is not desirable (or even possible).
There is the HTTP Strict Transport Security (HSTS) policy that instructs the client to always request the HTTPS version of the site in subsequent requests. However, this has some caveats which I can't see workarounds for in your case:
this requires the user to have already successfully requested the HTTPS version (at which point the
Strict-Transport-Securityresponse header is sent informing the client that all future requests to this host must be HTTPS).HSTS is for when the host uses only SSL. All HTTP (non-SSL) requests will be upgraded. There is no HTTP anymore. Which means that
http://example.com:2082/will not be accessible either. The browser will try to upgrade this request tohttps://example.com:2082/(yes, wrong port - see below) - this request will now break.As seen in the above point, HSTS does not really work with non standard ports (ie. ports other than 80/443). For non port 80 requests, the port number is preserved in the request. So, as stated above, port 2082 stays as port 2082. (Because the intention is that everything is now HTTPS.)
(Aside: I've not really studied the spec, but I wonder why the desired port couldn't be set as part of the initial Strict-Transport-Security response header, or have an option/flag that instructs the browser to direct requests back to the same port as the initial "successful" request? This would seem to get around points #2 and #3 above?)
So, the same conclusion as Simon, this does not seem to be possible.
Custom 400 Bad Request ...?
Just a thought... you could try defining a custom 400 ErrorDocument to have a more meaningful message and perhaps a link (or even JavaScript redirect?). However, this would need to be defined directly in the main server config to stand any chance of being triggered. (But it's not always possible to override some system generated Apache errors anyway.) And I would think you would need to specify an absolute URL in order to trigger an external redirect to another site (which will naturally lose the error status). Even if this "works", I'm not sure that it would necessarily be a good idea. (?)
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
