Problem with the API and CORS

Hello,

I've had problems with Cors[here] before.
Fortunately, I was able to get help here [solution answer: here]. Unfortunately, I am now facing the same problem: It is the same structure, only now the URL that the API queries is no longer a real domain [like domain.tld] but a local domain [like pihole.myhome]. if I enter the local url in the solution approach, I still get the Cors error.

Is it possible that the whole thing does not work with local domains/http domains? How can I solve this?

As I said last time, I'm not an expert in Apache config, but in a quick test using apache in a different server (not Pi-hole) I was able to retrieve the environment variable using a local domain name, even without a tld (server01 in my test).

We can try to see how PHP is receiving (or not receiving) this value adding a simple file to your Pi-hole directory. I'm considering your apache2 config is already using the same SetEnv CORS_HOSTS from the previous solution and you are using the default port (80).


In the command line, execute the following command to create a new file:

echo "<?php phpinfo(); ?>" > /var/www/html/admin/info.php

Then, on your browser, open the page http://pi.hole/admin/info.php.
On this page, search for Apache Environment section.

Do you see CORS_HOSTS in the table? Is the value correct?

My setup is pihole as a docker container. I have apache in the same docker instance in another container, both can communicate with each other via a docker network.
The local domain is trying to load the pihole api data from apache, apache proxyed the request via http to the pihole container. [the detour via apache is necessary because otherwise there could be problems with other containers in my setup]

So far, so good.

/var/www/html/admin/info.php does not show me explicitly to apache [because the pihole image uses lighttp?], but I could find two entries to cors:
image
image

As of now, the following is set in my apache configuration, based on your previous solution:

<VirtualHost hostname:80>
   ...
   SetEnv CORS_HOSTS local.domain
   ...
</VirtualHost>

Does that help? If not, I can also try other configurations.

I don't know how the php in the pihole container should know about the cors set in the apache container. I should have mentioned at the beginning that I have the whole thing in docker containers in the meantime, sorry.

As far as I know, this is not possible.
You never said you are running Pi-hole in a docker container.


I don't know how to help you.

Maybe you could try a different method using a macvlan network (Pi-hole container will have its own IP), or using a custom lighttpd config, without involving an apache container.

No no, it's not about the connection from Pihole to Apache. it's already there. it's just about pihole.
when i call apache in the browser i get the json data of the api displayed. only my html page [behind the local domain] cannot load it because cors prevents it.

I just wanted to show you my current setup, it has little to do with Docker. Cors is still the problem.

I have just found the following in the Pihole container log:

  [i] Added ENV to php:
                    ...
                    "CORS_HOSTS" => "",
                    ...

It comes up when starting the container, I think that's what we're looking for, right?

I have the solution. I just had to add CORS_HOSTS: 'local.domain' as env to the container and, it works.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.