我正在尝试将我的所有 API 和 Web HTTP 请求重定向到 Swift Perfect 中的 HTTPS。我已将代码部署到 AWS。当我用谷歌搜索时,我得到的只是使用我没有使用的 ELB。是否有任何解决方法可以重定向到代码内的 https 端口?
1 回答
2
经过大量的研究和人们的指导,我想出了一个解决方案。发布它,以便其他人不必在 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)")
}
于 2017-06-16T20:17:58.250 回答