Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

nginx in docker proxy path to subdomain

What I want is to forward all requests made for:

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

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

Probably worth mentioning that nginx is running in a Docker container.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...