【问题标题】:Nginx how to set proxy pass using map based on the host and locationNginx如何根据主机和位置使用地图设置代理通行证
【发布时间】:2020-10-04 12:49:37
【问题描述】:

目前我使用 map 指令动态设置代理传递。现在我想要一个特定的位置 /dcs/ 我想设置一个不同的代理通行证,而不是通过 map.conf 和 proxy_pass.map 提到的那个。有没有办法说为特定位置/dcs/使用不同的地图。

应该只使用 If 语句,例如如果主机是 dev-api.mysite.com,然后将 proxypass 设置为 http://abcdef.com。这是唯一的选择吗?

MySite.conf

server {
    listen 80;

    server_name ~^(api|dev-api|staging-api)\.mysite\.com$;

    location / {

        proxy_set_header Cookie "";
        add_header Strict-Transport-Security "max-age=0; includeSubDomains" always;
        proxy_hide_header 'Access-Control-Allow-Origin';
        add_header 'Access-Control-Allow-Origin' '*' always;

        include /etc/nginx/conf.d/headers.inc;
    }

    location /dcs/ {
        set $proxy_host $host;
        set $proxy_pass Set-different-host based of dev,staging,prod;
        proxy_set_header Cookie "";
        add_header Strict-Transport-Security "max-age=0; includeSubDomains" always;
        proxy_hide_header 'Access-Control-Allow-Origin';
        add_header 'Access-Control-Allow-Origin' '*' always;

        include /etc/nginx/conf.d/headers.inc;
    }

}

maps.conf

map $host $proxy_pass {
    hostnames;

    include /etc/nginx/conf.d/proxy_pass.map;
}

proxy_pass.map

# mysite.com

api.mysite.com                        myst.plat:8080;

staging-api.mysite.com                staging.myst.plat:8081;

dev-api.mysite.com                    dev.plat:8080;

headers.inc

proxy_pass $upstream_proto://$proxy_pass;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-Proto $real_scheme;
proxy_set_header X-Forwarded-Scheme $real_scheme;
proxy_set_header Host $proxy_host;
proxy_ssl_session_reuse on;
proxy_set_header X-Real-IP       $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-GeoIP-Country-Code $geoip_country_code;
proxy_set_header X-Site-Id $site_id;
add_header X-Cache-Time $date_gmt;
add_header X-Cache-Date $upstream_http_date;
add_header X-Proxy-Cache $upstream_cache_status;
# add_header X-Site-Id $site_id;

【问题讨论】:

    标签: nginx nginx-reverse-proxy nginx-location


    【解决方案1】:

    您可以使用多个链接的map 块:

    map $host $proxy_by_host {
        hostnames;
        include /etc/nginx/conf.d/proxy_pass.map;
    }
    
    map $uri $proxy_pass {
        ~^/dcs/    abcdef.com;
        default    $proxy_by_host;
    }
    

    更新

    看起来我有点误解了你的问题,根据请求 URI 使用两个不同的 map 块,使用类似

    map $host $proxy_by_host {
        hostnames;
        include /etc/nginx/conf.d/proxy_pass.map;
    }
    map $host $dcs_proxy_by_host {
        hostnames;
        include /etc/nginx/conf.d/dcs_proxy_pass.map;
    }
    map $uri $proxy_pass {
        ~^/dcs/    $dcs_proxy_by_host;
        default    $proxy_by_host;
    }
    

    这样,如果您的位置的所有其他参数都相同,则您只能对所有请求使用一个 location / { ... }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多