docker代理路径中的nginx到子域 [英] nginx in docker proxy path to subdomain

查看:60
本文介绍了docker代理路径中的nginx到子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转发的所有请求:

www.domain.com/ api /whaterver/comes/next to-> api .domain.com/whatever/comes/next

www.domain.com/api/whaterver/comes/next to -> api.domain.com/whatever/comes/next

原因是避免www.domain.com的浏览器CORS请求api.domain.com

The reason is to avoid browser CORS for www.domain.com requesting to api.domain.com

也许值得一提的是,nginx在Docker容器中运行.

我正在尝试完成下面的位置限制,但失败了:

I am trying to accomplish with the location block below, but it fails:

server {
listen 8443 ssl;
server_name domain.com www.domain.com;
index index.php index.html;
root /var/www/base/public;

location ~ ^/api/(.*)$ {
    proxy_set_header Host api.domain.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass https://api.domain.com/$1;
}

ssl_certificate /etc/nginx/ssl/nginx.cert;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_timeout         5m;
ssl_protocols               SSLv2 SSLv3 TLSv1;
ssl_ciphers                 HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers   on;

include /etc/nginx/conf.d/common.conf;
}

推荐答案

感谢伊万(Ivan)向我指出了正确的方向.

Thanks to Ivan for pointing me to the right direction on this one.

Docker容器中的解析器应使用ipv6指令关闭的127.0.0.11上的Docker嵌入式DNS服务器:

The resolver inside a Docker container should use the Docker embedded DNS server at 127.0.0.11 with the ipv6 directive switched off:

server {
    listen 8443 ssl;
    server_name domain.com www.domain.com;
    index index.php index.html;
    root /var/www/base/public;

    location ~ ^/api/(.*)$ {
        resolver 127.0.0.11 ipv6=off;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_pass https://api.domain.com/$1;
    }

}

这篇关于docker代理路径中的nginx到子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆