SSL

从HTTP强制跳转到HTTPS

适用于Apache、Nginx、Tomcat、Lighthttp、Go、Traefik、Haproxy 、 Oraclehttp、IIS7、IIS8、IIS9等服务器环境

Apache

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

Nginx

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  return 301 https://$host$request_uri;
}

Tomcat

<Connector
  port="80"
  redirectPort="443" />

Lighttpd

$SERVER["socket"] == ":80" {
  url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
}

Go

go func() {
        redirectToHTTPS := func(w http.ResponseWriter, req *http.Request) {
            http.Redirect(w, req, "https://"+req.Host+req.RequestURI, http.StatusMovedPermanently)
        }
        srv := {{'&'}}http.Server{
            Handler:     http.HandlerFunc(redirectToHTTPS),
            ReadTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second,
        }
        log.Fatal(srv.ListenAndServe())
    }()

Traefik

[http.middlewares]
  [http.middlewares.redirect-to-https.redirectScheme]
    scheme = "https"
  [http.middlewares.hsts-header.headers]
    [http.middlewares.hsts-header.headers.customResponseHeaders]
      Strict-Transport-Security = "max-age=63072000"

Haproxy

redirect scheme https code 301 if !{ ssl_fc }

Oraclehttp

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

IIS7、IIS8、IIS9

安装rewrite模块,下载模块:

https://www.iis.net/downloads/microsoft/url-rewrite

mmc7.png mmc8.png