docker - docker代理路径中的nginx到子域

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

我要转发的所有请求:

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

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

可能值得一提的是,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;
}

最佳答案

感谢Ivan在此问题上为我指明了正确的方向。

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

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 - docker代理路径中的nginx到子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466891/

相关文章:

fedora - `docker run ubuntu:14.04/bin/echo`在Fedora 20上产生SELinux错误

process - Docker与普通的OS进程有何不同?

docker - 使用主机库的Docker

nginx - Nginx在上游响应日志中显示2种不同的状态

django、react js、nginx : deploy prosses

nginx - 将目录重写为参数

amazon-web-services - 在 docker 中将 AWS SAM Local 与 dynamodb 连接

nginx 服务器无法使用 service nginx start 重新启动

linux - Nginx 上游作为 url