I have a catchall server configuration that gets all sub-domains and point them to the same PHP script. There are thousands of sub-domains and they are created dynamically.
The PHP script uses the HTTP_HOST to identify the sub-domain and fetch the content from the database accordingly to the sub-domain identified.
It works fine, but I am receiving this log from Nginx:
PHP message: PHP Notice: Undefined index: HTTP_HOST in
/usr/share/nginx/html/index.php on line 9
I've searched the web and found out that HTTP_HOST may not always be set. The PHP manual says:
'HTTP_HOST' = Contents of the Host: header from the current request, if there is one.
So I thought about using the variable SERVER_NAME instead of HTTP_HOST. But when I do it, SERVER_NAME does not contain the sub-domain part. It will always return what I write in the Nginx config, that is ~^(.+)$
in my case.
Is there a way to change Nginx config so that SERVER_NAME include the dynamic sub-domains? For example, to return something like sub1.mydomain.com, sub2.mydomain.com and so on.
Is there any solution for this problem?
My actual configuration is:
server {
listen 80 default_server;
server_name ~^(.+)$;
}