【问题标题】:nginx in docker proxy path to subdomaindocker代理路径中的nginx到子域
【发布时间】:2019-10-27 18:50:10
【问题描述】:

我想要的是转发所有请求:

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

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

可能值得一提的是,nginx 是在 Docker 容器中运行的。

我正在尝试使用下面的位置块来完成,但它失败了:

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;
}

【问题讨论】:

  • 你错过了~ 签名:location ~ ^/api/(.*)$ {
  • 尝试使用proxy_set_header Host api.domain.com; 将主机标头更改为新的api.domain.com
  • 而且@Zac 也是对的,我忘了HTTP Host 标头也应该更改...
  • @IvanShatsky 仍然一无所获,我收到了 404 响应。添加您的两个建议。
  • @Zac 还是一无所有

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


【解决方案1】:

感谢 Ivan 为我指明了正确的方向。

Docker 容器内的解析器应使用位于 127.0.0.11 的 Docker 嵌入式 DNS 服务器,并关闭 ipv6 指令:

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;
    }

}

【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2018-10-03
    • 2017-10-10
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多