Apache CORS headers for docker

南楼画角 提交于 2021-01-28 05:32:31

问题


On my server I have two docker containers. One is docker Registry, and the other is Angular app that access first containers endpoint.

Docker registry has mapped port 5000 and is proxied through Apache's virtual host to domain docker.mydomain.com. Angular app has port 5002 and is proxied to dockerhub.mydomain.com

I set up a virtual host file for my docker registry like this:

<VirtualHost *:80>
        ServerName docker.mydomain.com
        Redirect permanent / https://docker.mydomain.com/
</VirtualHost>

<VirtualHost *:443>

        ServerName mydomain.com
        ServerAlias docker.mydomain.com
        ServerAdmin admin@mydomain.com

        # Headers required for docker registry to work
        Header set Host "docker.mydomain.com"
        Header always set "Docker-Distribution-Api-Version" "registry/2.0"
        Header onsuccess set "Docker-Distribution-Api-Version" "registry/2.0"
        RequestHeader set X-forwarded-Proto "https"

        # Settings CORS headers
        Header always set Access-Control-Allow-Origin "*"
        Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept"
        Header always set Vary "Origin, Access-Control-Request-Headers, Access-Control-Request-Method"

        # SSL settings
        SSLEngine On
        SSLCertificateFile "/etc/letsencrypt/live/docker.mydomain.com/cert.pem"
        SSLCertificateKeyFile "/etc/letsencrypt/live/docker.mydomain.com/privkey.pem"
        SSLCertificateChainFile "/etc/letsencrypt/live/docker.mydomain.com/chain.pem"

        # Reply to all OPTIONS requests with 200
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]

        ProxyPreserveHost On
        ProxyRequests Off

        # Proxy request to localhost:5000 and check authentication
        <Location "/">
                ProxyPass http://localhost:5000/
                ProxyPassReverse http://localhost:5000/
                Order deny,allow
                Allow from all
                AuthName "Registry Authentication"
                AuthType basic
                AuthUserFile "/opt/docker-registry-htpasswd/.registry-htpasswd"
                # Allow reading to all users, but pushing images only for certain user
                <Limit GET HEAD>
                        Require valid-user
                </Limit>
                <Limit POST PUT DELETE PATCH>
                        Require user myuser
                </Limit>
        </Location>

        ErrorLog ${APACHE_LOG_DIR}/docker-registry-error.log
        LogLevel warn

</VirtualHost>

My Angular app that is in another docker is served with nginx image with following configuration:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen 4200;
        server_name  localhost;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}

Now, when I open my Angular app at dockerhub.mydomain.com and it tries to call API from docker.mydomain.com I get error related with CORS headers:

Access to XMLHttpRequest at 'https://docker.mydomain.com/v2' from 
origin 'https://dockerhub.mydomain.com' has been blocked by CORS policy: 
The 'Access-Control-Allow-Origin' header has a value 'http://localhost:4200' that is 
not equal to the supplied origin.

As far as I know if Allow-Origin header is set to * it means that it is set to the request's Origin, which is set to https://dockerhub.mydomain.com but instead Allow-Origin is set to localhost:4200 - What could the problem here be?

Also, the Angular app is always making requests to docker.mydomain.com and never to localhost address.


回答1:


Instead of copying nginx.conf to /etc/nginx/nginx.conf I copied the configuration file to /etc/nginx/conf.d/default.conf

Then I also shortened conf file a bit:

server {
    listen 80;
    server_name  localhost;
    root   /usr/share/nginx/html;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

Now I get properly set CORS headers in all browsers



来源:https://stackoverflow.com/questions/53146649/apache-cors-headers-for-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!