在nginx反向代理后面查询外部API时遇到问题

时间:2020-10-12 00:11:13

标签: nginx-reverse-proxy vapor

我有一个蒸气Web应用程序在nginx之后反向代理。该应用程序需要进行外部API调用。该API调用可以在本地托管(没有nginx)下进行良好的测试,但是在Nginx后面的VPS上发出请求时,出现了502错误。

nginx错误:

upstream prematurely closed connection while reading response header from upstream, client: XX.XXX.XXX.XX, server: myserver.com, request: "GET /auth/substat/XXXXX HTTP/1.1", upstream: "http://127.0.0.1:8080/auth/substat/XXXXX", host: "myserver.com"

nginx相关配置:

server {
    server_name myserver.com;
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myserver.com;/fullchain.pem; # manage$
    ssl_certificate_key /etc/letsencrypt/live/myserver.com;/privkey.pem; # mana$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

vapor API代码:

func subscriptionStatus(_ req: Request) throws -> EventLoopFuture<StatusResponse> {
        var response : StatusResponse = StatusResponse(response: .failure, subscribed: .no)
        let id = req.parameters.get("id")!
        let key =  Environment.get("REV_CAT_SECRET")!
        let query = "https://api.revenuecat.com/v1/subscribers/\(id)"
        let headers = HTTPHeaders([("authorization", "Bearer \(key)"),("Content-Type","application/json"),("x-platform", "stripe")])
        return try req.client.get(URI(string:query),headers: headers).map { res in
            if let obj = try? res.content.decode(RevCatResponse.self) {
                let annual = obj.subscriber.subscriptions.myannualproduct?.expiresDate
                let monthly = obj.subscriber.subscriptions.mymonthlyproduct?.expiresDate
                if annual != nil && annual! > Date() {
                    response = StatusResponse(response: .success, subscribed: .yes)
                } else if monthly != nil && monthly! > Date() {
                    response = StatusResponse(response: .success, subscribed: .yes)
                } else {
                    response = StatusResponse(response: .success, subscribed: .no)
                }
            } else {
                response = StatusResponse(response: .failure, subscribed: .no)
            }
        }.map {
            return response
        }
    }

我想念什么?预先感谢!

0 个答案:

没有答案