在没有 ELB 的情况下将所有 HTTP 重定向到 HTTPS
Redirect all HTTP to HTTPS without ELB
我正在尝试将我的所有 API 和 Web HTTP 请求重定向到 Swift 完美的 HTTPS。我已将代码部署到 AWS 中。当我用谷歌搜索时,我得到的只是使用我没有使用的 ELB。是否有任何解决方法可以重定向到代码内部的 https 端口?
经过大量研究和人员指导,我找到了解决方案。发布它以便其他人不必在 Perfect
上花费时间
这里是将所有 HTTP 重定向到 HTTPS 的解决方案
let mainDomain = "www.<your domain>.com(or anything)"
var nonSecureRoutes = Routes()
nonSecureRoutes.add(method: .get, uri: "/**", handler: {
request, response in
response.setHeader(.location, value: "https://\(request.header(.host) ?? mainDomain)\(request.uri)")
.completed(status: .movedPermanently)
})
let certPath = "/cert/path.pem"
let keyPath = "key/path.pem"
var certVerifyMode = OpenSSLVerifyMode()
certVerifyMode = .sslVerifyPeer
do {
try HTTPServer.launch(
.server(name: mainDomain, port: 80, routes: nonSecureRoutes),
.secureServer(TLSConfiguration(certPath: certPath, keyPath: keyPath, certVerifyMode: certVerifyMode), name: mainDomain, port: 443, routes: routes))
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}
我正在尝试将我的所有 API 和 Web HTTP 请求重定向到 Swift 完美的 HTTPS。我已将代码部署到 AWS 中。当我用谷歌搜索时,我得到的只是使用我没有使用的 ELB。是否有任何解决方法可以重定向到代码内部的 https 端口?
经过大量研究和人员指导,我找到了解决方案。发布它以便其他人不必在 Perfect
上花费时间这里是将所有 HTTP 重定向到 HTTPS 的解决方案
let mainDomain = "www.<your domain>.com(or anything)"
var nonSecureRoutes = Routes()
nonSecureRoutes.add(method: .get, uri: "/**", handler: {
request, response in
response.setHeader(.location, value: "https://\(request.header(.host) ?? mainDomain)\(request.uri)")
.completed(status: .movedPermanently)
})
let certPath = "/cert/path.pem"
let keyPath = "key/path.pem"
var certVerifyMode = OpenSSLVerifyMode()
certVerifyMode = .sslVerifyPeer
do {
try HTTPServer.launch(
.server(name: mainDomain, port: 80, routes: nonSecureRoutes),
.secureServer(TLSConfiguration(certPath: certPath, keyPath: keyPath, certVerifyMode: certVerifyMode), name: mainDomain, port: 443, routes: routes))
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}