Re: How to determine the "right" vhost in name based vhosting
Hello Sorin, Thank you very much. I finally found out the course of the strange results I am getting. I used the request_rec to get the conn_rec. In this connection record there's a field called base_server, which I used as the source for the server_hostname. request_rec *r; conn_rec *c; server_rec *server; c = r->connection; server = c->base_server; My misunderstanding was that this leads to the main vhost I am using. But it actually leads to the default server of the current ip:port combination. Now I use: server = r->server; This gives the expected result. Everything is fine now ;-). I can't tell you why I used the more complicated way to retrieve a server_rec. Probably just used some example code from somewhere. In the beginning everything seemed alright since we always had just one vhost under one IP. The problem showed up when we had a second vhost running. Another misunderstanding solved now. Thank you for your time (and patience). With kind regards .. -- Sitepark Gesellschaft für Informationsmanagement mbH Rothenburg 14-16, 48143 Münster Telefon: +49 251 482655-0, Telefax: +49 251 482655-55 http://www.sitepark.com http://www.facebook.com/sitepark Geschäftsführer: Thorsten Liebold Amtsgericht Münster, HRB 5017
Re: How to determine the "right" vhost in name based vhosting
On 2013-09-24 13:04, Christoph Gröver wrote: Hello Sorin, I suppose you use the server field of the request_rec structure and not some stored server_rec that was passed to you in post_config or somewhere else. Definitely. I have adopted this from some other module and didn't know there was another way to obtain a server_rec structure. So I should be looking for a better way to find the right structure. Thank you very much. This sounds as if it will be the right way. I fear there's a misunderstanding here: The right way to get the server_rec is, in my opinion, from the request_rec structure, i.e. I think you should use req->server->server_hostname. So, given that you already do this, it is puzzling for me why you don't get the result that you want. Apache sets the req->server pointer to the right server_rec structure after it has parsed the request headers. (It cannot guess correctly before it parses the Host header.) So make sure you check req->server _after_ apache has initialised it to the right server_rec. Apache sets it in the ap_read_request method. Almost all of the callbacks provided to the module developers are called _after_ ap_read_request, so you should be ok. I think only the create_connection callback is run before ap_read_request. As a "poor man's debugger" technique you could write a post_config callback. The last argument of the post_config callback is the head of the list of server_recs. You could traverse the list and log to a file the server_hostname of all server_recs in the list. Just to check that you have the right number of server_recs and that they are correctly initialised. Sorin Apache keeps a linked list of server_rec structures. The head of the list is the server_rec of the whole apache server. The rest of the list contains one server_rec structure per vhost. For each request apache picks the right server_rec from the list according to the Host header and sets r->server to point to the picked object. This information will also help. Thank you. Also make sure that your request really arrives in the vhost you intended. Typically I check this by logging to different files (see the CustomLog directive) in each vhost. This is actually the case. I receive the requests in the right vhost. I have separate logfiles for each vhost. Thanks for your answers. I guess I will be able to solve the issue with these informations. With kind regards,
Re: How to determine the "right" vhost in name based vhosting
Hello Sorin, > I suppose you use the server field of the request_rec structure and > not some stored server_rec that was passed to you in post_config or > somewhere else. Definitely. I have adopted this from some other module and didn't know there was another way to obtain a server_rec structure. So I should be looking for a better way to find the right structure. Thank you very much. This sounds as if it will be the right way. > > Apache keeps a linked list of server_rec structures. The head of the > list is the server_rec of the whole apache server. The rest of the > list contains one server_rec structure per vhost. For each request > apache picks the right server_rec from the list according to the Host > header and sets r->server to point to the picked object. This information will also help. Thank you. > > Also make sure that your request really arrives in the vhost you > intended. Typically I check this by logging to different files (see > the CustomLog directive) in each vhost. This is actually the case. I receive the requests in the right vhost. I have separate logfiles for each vhost. Thanks for your answers. I guess I will be able to solve the issue with these informations. With kind regards, -- Sitepark Gesellschaft für Informationsmanagement mbH Rothenburg 14-16, 48143 Münster Telefon: +49 251 482655-0, Telefax: +49 251 482655-55 http://www.sitepark.com http://www.facebook.com/sitepark Geschäftsführer: Thorsten Liebold Amtsgericht Münster, HRB 5017
Re: How to determine the "right" vhost in name based vhosting
On Sep 24, 2013 5:40 AM, "Christoph Gröver" wrote: > > > Hello list, Hello Sorin, > > I tested several different Apaches (2.4.x and 2.2.x) and they never did > the wanted or expected. > > If I configure more than one VHost only the first one is returned by > the server->server_hostname structure.> The one of the second vhost that is configured as a "ServerName" seems > to be impossible to determine? > > Is there any other way to find the hostname? When you check, are you in the middle of a request that's mapped to the 2nd virtual host? In what phase do you inspect the value? If you define a logfile in your 2nd vhost, does it accumulate entries?
Re: How to determine the "right" vhost in name based vhosting
On 2013-09-24 11:38, Christoph Gröver wrote: Hello list, Hello Sorin, I tested several different Apaches (2.4.x and 2.2.x) and they never did the wanted or expected. If I configure more than one VHost only the first one is returned by the server->server_hostname structure. The one of the second vhost that is configured as a "ServerName" seems to be impossible to determine? Is there any other way to find the hostname? I suppose you use the server field of the request_rec structure and not some stored server_rec that was passed to you in post_config or somewhere else. Apache keeps a linked list of server_rec structures. The head of the list is the server_rec of the whole apache server. The rest of the list contains one server_rec structure per vhost. For each request apache picks the right server_rec from the list according to the Host header and sets r->server to point to the picked object. Also make sure that your request really arrives in the vhost you intended. Typically I check this by logging to different files (see the CustomLog directive) in each vhost. Regards, Sorin
Re: How to determine the "right" vhost in name based vhosting
Hello list, Hello Sorin, I tested several different Apaches (2.4.x and 2.2.x) and they never did the wanted or expected. If I configure more than one VHost only the first one is returned by the server->server_hostname structure. The one of the second vhost that is configured as a "ServerName" seems to be impossible to determine? Is there any other way to find the hostname? Greetings -- Sitepark Gesellschaft für Informationsmanagement mbH Rothenburg 14-16, 48143 Münster Telefon: +49 251 482655-0, Telefax: +49 251 482655-55 http://www.sitepark.com http://www.facebook.com/sitepark Geschäftsführer: Thorsten Liebold Amtsgericht Münster, HRB 5017
Re: How to determine the "right" vhost in name based vhosting
Hello Sorin, > > I've tested this setup in 2.4.6 and r->server->server_hostname > contains what you want. Thanks for your answer. Well, at least with Apache 2.2.15 I recognize a different behaviour. The server->server_hostname points to the first vhost that is configured for an IP:Port combination, not to the vhost configured in the Apache ServerName option. In my example the result is always main.domain.tld, and never www.domain.tld I will try a few other apache versions to verify this further. Thank you, Greetings -- Sitepark Gesellschaft für Informationsmanagement mbH Rothenburg 14-16, 48143 Münster Telefon: +49 251 482655-0, Telefax: +49 251 482655-55 http://www.sitepark.com http://www.facebook.com/sitepark Geschäftsführer: Thorsten Liebold Amtsgericht Münster, HRB 5017
Re: How to determine the "right" vhost in name based vhosting
On 2013-09-19 16:39, Christoph Gröver wrote: Hello, We usually use name based virtualhosts with something like the following configuration: NameVirtualHost IP:80 ServerName main.domain.tld ServerAlias alias.domain.tld .. ServerName www.domain.tld ServerAlias alt.domain.tld .. I've tested this setup in 2.4.6 and r->server->server_hostname contains what you want. Regards, Sorin Now I'm looking for a function which reliable returns the host main.domain.tld if the first vhost is used (even if it is used as alias.domain.tld) and returns www.domain.tld if the second one is used (even if under the name alt.domain.tld). I know of two ways to do this: 1. ap_get_server_name This returns the right hostname if "UseCanonicalName" is set. But returns just the Host:-Header if it is off - which is the default. 2. server_rec structure The element server->server_hostname always returns the first vhost available for an ip address. So even if I use www.domain.tld it returns main.domain.tld So the first option depends on "UseCanonicalName", the second does something else - which is not what I want. Any other ways of doing this? Or is there just the solution to force "UseCanonicalName" to "on" and otherwise it won't work. Can anybody enlighten me as to how this should be done? Thank you, Greetings
How to determine the "right" vhost in name based vhosting
Hello, We usually use name based virtualhosts with something like the following configuration: NameVirtualHost IP:80 ServerName main.domain.tld ServerAlias alias.domain.tld .. ServerName www.domain.tld ServerAlias alt.domain.tld .. Now I'm looking for a function which reliable returns the host main.domain.tld if the first vhost is used (even if it is used as alias.domain.tld) and returns www.domain.tld if the second one is used (even if under the name alt.domain.tld). I know of two ways to do this: 1. ap_get_server_name This returns the right hostname if "UseCanonicalName" is set. But returns just the Host:-Header if it is off - which is the default. 2. server_rec structure The element server->server_hostname always returns the first vhost available for an ip address. So even if I use www.domain.tld it returns main.domain.tld So the first option depends on "UseCanonicalName", the second does something else - which is not what I want. Any other ways of doing this? Or is there just the solution to force "UseCanonicalName" to "on" and otherwise it won't work. Can anybody enlighten me as to how this should be done? Thank you, Greetings -- Sitepark Gesellschaft für Informationsmanagement mbH Rothenburg 14-16, 48143 Münster Telefon: +49 251 482655-0, Telefax: +49 251 482655-55 http://www.sitepark.com http://www.facebook.com/sitepark Geschäftsführer: Thorsten Liebold Amtsgericht Münster, HRB 5017