Docker的Apache CORS标头

时间:2018-11-04 23:52:23

标签: angular apache docker cors virtualhost

在我的服务器上,我有两个Docker容器。一个是docker Registry,另一个是访问第一个容器端点的Angular应用。

Docker注册表已映射端口5000,并通过Apache的虚拟主机代理到域docker.mydomain.com。 Angular应用程序具有端口5002,并代理到dockerhub.mydomain.com

我为docker注册表设置了一个虚拟主机文件,如下所示:

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

另一个docker中的我的Angular应用程序通过以下配置提供了nginx图像:

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

现在,当我在dockerhub.mydomain.com上打开Angular应用程序并尝试从docker.mydomain.com调用API时,出现与CORS标头相关的错误:

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.

据我所知,Allow-Origin标头是否设置为*,这意味着它已设置为请求的原点,该原点设置为https://dockerhub.mydomain.com,但Allow-Origin设置为localhost:4200-什么问题可能在这里吗?

此外,Angular应用程序总是向docker.mydomain.com发出请求,而不向本地主机地址发出请求。

1 个答案:

答案 0 :(得分:0)

我将配置文件复制到/etc/nginx/nginx.conf,而不是将nginx.conf复制到/etc/nginx/conf.d/default.conf

然后我也将conf文件缩短了一点:

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

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

现在我可以在所有浏览器中正确设置CORS标头