NGINX 如何使用 SSL 设置多个位置

NGINX 如何使用 SSL 设置多个位置

我正在使用具有多个位置的 nginx 将请求定向到我的网站和我的 api。我使用 certbot 生成 SSL 证书,它会自动将行添加到 nginx 配置以配置 SSL。

这些文件从 html 文件夹提供时没有任何问题,但是,对“redacted.org/api”的任何 Web 请求都返回 502 Bad Gateway。就好像 SSL 没有为“/api”位置配置,但为“/”位置配置了。我需要在我的配置(如下)中更改什么才能使其正常工作?

server {
 listen 80;

 server_name redacted.org www.redacted.org;
 location /api {
    proxy_pass http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
 }
 location / {
    root /var/www/html;
 }

 listen 443 ssl; # managed by Certbot
 ssl_certificate /etc/letsencrypt/live/redacted.org/fullchain.pem; # managed by Certbot
 ssl_certificate_key /etc/letsencrypt/live/redacted.org/privkey.pem; # managed by Certbot
 include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

 if ($scheme != "https") {
    return 301 https://$host$request_uri;
 } # managed by Certbot

}

在 Digital Ocean Ubuntu 16.04.2 盒子上运行

答案1

从评论中移出

上游有问题。上游关闭时会显示错误 502。因此需要检查上游并修复它。

相关内容