Apache CORS headers for docker



在我的服务器上,我有两个docker容器。一个是DockerRegistry,另一个是访问第一个容器端点的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>

我的Angular应用程序在另一个docker中提供了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.myddomain.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标头设置为*,则意味着它被设置为请求的Originhttps://dockerhub.mydomain.com但是Allow Origin设置为localhost:4200-这可能是什么问题?

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

我没有将nginx.conf复制到/etc/nginx/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标题

最新更新