NGINX proxy_pass相同协议(http / https)

问题描述 投票:5回答:1

我的NGINX配置文件中有一节使用proxy_pass将API流量重定向到上游服务器。我在一个location块中有servers,它同时提供http和https请求:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name mysite.local;    # Valid TLD in production

然后我有location块来定义我的API网关:

    location /api/v1/json {
        # Various proxy config directives
        proxy_pass http://upstream;

我的问题是:是否可以删除http://并根据协议将请求传递给我的上游服务器而不拆分我的server块?像HTML / JavaScript //mysite.local请求的东西。

nginx
1个回答
10
投票

你可以使用$scheme变量:

location /api/v1/json {
    # Various proxy config directives
    proxy_pass $scheme://your-host
}

来自docs

$scheme
request scheme, "http" or "https"

然后,它将使用与原始请求相同的协议。

© www.soinside.com 2019 - 2024. All rights reserved.