简体   繁体   中英

How to Enable CORS for Docker Apache httpd server?

I need to create an apache server to host my files and to get them by ajax. So, I'm using docker to deploy my server.

My docker image is httpd:2.4.

I deployed the server with the following command:

docker run -p 80:80 -dit --name file-server \
  -v /sources/docker/apache-server/www/:/usr/local/apache2/htdocs/ httpd:2.4

But when I want to make the request for ajax, this is the result:

XMLHttpRequest cannot load http://server/kml/example.kml . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

So, I want to follow the next steps How to Enable CORS for Apache httpd server? (Step-By-Step Process) . But I do not know how to add that command in the httpd.conf of the component. And I don't have the httpd.conf template to replace it with:

v /sources/docker/apache-server/my-httpd.conf:/usr/local/apache2/conf/httpd.conf

Please help me with this question.

You have that enter in shell-terminal with command docker exec -it nameContainer sh . write in terminal: su with those commands now you are root user in your Docker.

Now in terminal, you have than write a2enmod headers and restart your docker

The last command was for activate mod_header and just now you have that create a .htaccess in your project root file and write inside:

Header add Access-Control-Allow-Origin "*"

With that for me work fine and didn't need install apache in my machine

This has certainly already been addressed elsewhere and has many different solutions, however it's the first search hit for 'cors apache docker' so here's my 2 cents:

The best solution (because it is auditable and repeatable ) is to use the apache docker image as a base for your own image using the docker build command and a Dockerfile . I won't go in to that as that's not the direct question here, but the basic bits are the same.

Create an "extra" apache config file with the CORS header bits:

me@mymachine:~$ docker exec file-server bash -c 'printf "<Directory \"/var/www/html\">\n        Header set Access-Control-Allow-Origin \"*\"\n</Directory>\n" >> /usr/local/apache2/conf/httpd.conf'

me@mymachine:~$ docker exec file-server bash -c 'tail /usr/local/apache2/conf/extra/cors.conf'
<Directory "/var/www/html">
        Header set Access-Control-Allow-Origin "*"
</Directory> 

Restart apache to use this new config:

me@mymachine:~$ docker exec file-server bash -c 'apachectl -k restart'

NOTE: If you haven't configured a ServerName anywhere, then you might see this warning which is unrelated to the CORS configuration: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM