You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Robert Schulze <rs...@bytecamp.net> on 2008/04/04 11:59:08 UTC

what should be done in a vhost_db module?

Hi,

I'm currently working on a module to create virtualhosts on the fly out 
of a cdb-database file.

I looked at some module which do basically the same with other data 
sources. What I now wonder is, what else I have to do except for setting 
the r->filename and maybe some script-alias-foo.

In one module I saw the following:

8<--------------------------------------------------------------------

request_rec *top = (r->main)?r->main:r;

/* ... */

top->server->server_hostname = apr_pstrdup (top->pool, hostname);

r->parsed_uri.hostinfo = apr_pstrdup(r->pool,r->server->server_hostname);

r->parsed_uri.hostname = apr_pstrdup(r->pool,r->server->server_hostname);

8<--------------------------------------------------------------------

and thats exactly where I got some problems.
I sometimes noticed, that the virtual host name in /server-status/ 
contained only weird characters, which I think is a result of freeing a 
buffer which is currently in use.
I think the problem is the wrong pool for allocating space for the 
server_hostname.

I thought I fixed that with this solution:

8<--------------------------------------------------------------------

if(r->hostname!=NULL)
         {
         r->server->server_hostname = apr_pstrdup(r->connection->pool, 
r->hostname);
         r->parsed_uri.hostinfo = 
apr_pstrdup(r->pool,r->server->server_hostname);
         r->parsed_uri.hostname = 
apr_pstrdup(r->pool,r->server->server_hostname);
         }

8<--------------------------------------------------------------------

But that didn't fix the problem completely.

My question now: do I have to set server->server_hostname and if so, 
from which pool should I allocate the memory from?

with kind regards,

Robert Schulze

Re: what should be done in a vhost_db module?

Posted by Joe Lewis <jo...@joe-lewis.com>.
ed wrote:
> On Mon, 07 Apr 2008 10:12:18 +0200
> Robert Schulze <rs...@bytecamp.net> wrote:
>
>   
>>> Are you doing this to save memory?   
>>>       
>> This is one important fact - it saves a huge amount of memory with 
>> several thousands of virtualhosts.
>> One more interesting thing is, that apache does not need to be
>> restarted when adding domains :-)
>>     
>
> Apache doesn't have to be restarted when adding virtual domains, you
> can add the domains to the conf and send a USR1 to httpd and it will
> reload that without interrupting existing requests AFAIK.
>
>   

I've watched this thread with some interest, and some chuckling.  There 
are two distinct perspectives here, and I just thought I'd tackle both 
perspectives.

2 - some of us think that whatever Apache needs will be configured 
somewhere when apache loads, or we will create new virtual hosts on the 
fly.  This is an EXCELLENT suggestion when it comes to minor changes 
over a longer period of time.

1 - the original poster is probably doing what I have done, which is 
called a mass virtual host - there is no server record for each host, as 
there could be (literally) thousands of virtual hosts, many being 
created every minute, and also deleted.  These kinds of environments are 
typically found in web hosting companies or ISP's - a service geared to 
handle harsh dynamics.  Often, there is no other way to implement a 
hosting service in this situation than this form.

In Apache 2.0, there was a mass_virtual_host module.  In Apache 2.2, 
there is actually a mod_vhost_alias module - these modules simply 
overwrite some settings without creating new server_rec structures, such 
as setting the r->hostname (hostname on a per-request basis, using 
r->pool), a simple mapping method to a directory for the "document 
root", and some other pieces to ensure environment variables and tables 
are set for things like PHP and CGI.  They run quickly, and are designed 
for high volumes and high numbers of sites.

For the original poster, I suggest reviewing mod_vhost_alias.c in the 
apache source code modules/mappers/ directory - it should give you a 
great concept of what must be done to mimmick a configured server_rec 
without actually creating one.

Joe
-- 
Joseph Lewis <http://sharktooth.org/>
"Divide the fire, and you will sooner put it out." - Publius Syrus

Re: what should be done in a vhost_db module?

Posted by ed <ed...@s5h.net>.
On Mon, 07 Apr 2008 10:12:18 +0200
Robert Schulze <rs...@bytecamp.net> wrote:

> > Are you doing this to save memory?   
> 
> This is one important fact - it saves a huge amount of memory with 
> several thousands of virtualhosts.
> One more interesting thing is, that apache does not need to be
> restarted when adding domains :-)

Apache doesn't have to be restarted when adding virtual domains, you
can add the domains to the conf and send a USR1 to httpd and it will
reload that without interrupting existing requests AFAIK.

-- 
The 28.8 frame relay to the Verizon Switch is in a battle with the dark
side because of a trailing space in /etc/netgroups. The Empire is
hiring Bob Grant to scuttle the company. :: http://www.s5h.net/ ::
http://www.s5h.net/gpg.html

Re: what should be done in a vhost_db module?

Posted by Robert Schulze <rs...@bytecamp.net>.
Hi,

ed schrieb:
> 
> I'm not sure that I understand what you're doing here by creating
> virtual hosts on the fly,

I mean, on request basis (from a db).
So what I wanted to know is, whether there are any hints, what a 
mod_vhost_??? module is supposed to do (or set).

I'm now setting r->server->server_hostname by allocating space from 
r->server->process->pool so I hope I have fixed the memory issues.

> Are you doing this to save memory? 

This is one important fact - it saves a huge amount of memory with 
several thousands of virtualhosts.
One more interesting thing is, that apache does not need to be restarted 
when adding domains :-)

Rob

Re: what should be done in a vhost_db module?

Posted by ed <ed...@s5h.net>.
On Fri, 04 Apr 2008 11:59:08 +0200
Robert Schulze <rs...@bytecamp.net> wrote:

> Hi,
> 
> I'm currently working on a module to create virtualhosts on the fly
> out of a cdb-database file.
> 
> I looked at some module which do basically the same with other data 
> sources. What I now wonder is, what else I have to do except for
> setting the r->filename and maybe some script-alias-foo.
> 
> In one module I saw the following:
> 
> 8<--------------------------------------------------------------------
> 
> request_rec *top = (r->main)?r->main:r;
> 
> /* ... */
> 
> top->server->server_hostname = apr_pstrdup (top->pool, hostname);
> 
> r->parsed_uri.hostinfo =
> apr_pstrdup(r->pool,r->server->server_hostname);
> 
> r->parsed_uri.hostname =
> apr_pstrdup(r->pool,r->server->server_hostname);
> 
> 8<--------------------------------------------------------------------
> 
> and thats exactly where I got some problems.
> I sometimes noticed, that the virtual host name in /server-status/ 
> contained only weird characters, which I think is a result of freeing
> a buffer which is currently in use.
> I think the problem is the wrong pool for allocating space for the 
> server_hostname.
> 
> I thought I fixed that with this solution:
> 
> 8<--------------------------------------------------------------------
> 
> if(r->hostname!=NULL)
>          {
>          r->server->server_hostname =
> apr_pstrdup(r->connection->pool, r->hostname);
>          r->parsed_uri.hostinfo = 
> apr_pstrdup(r->pool,r->server->server_hostname);
>          r->parsed_uri.hostname = 
> apr_pstrdup(r->pool,r->server->server_hostname);
>          }
> 
> 8<--------------------------------------------------------------------
> 
> But that didn't fix the problem completely.
> 
> My question now: do I have to set server->server_hostname and if so, 
> from which pool should I allocate the memory from?
> 
> with kind regards,
> 
> Robert Schulze

I'm not sure that I understand what you're doing here by creating
virtual hosts on the fly, do you mean in real time to the database of
virtual hosts updating - that would perhaps be a lot of work that isn't
normally required during the request, why not signal the apache server
to reload it's config when the db changes?

Are you doing this to save memory? Maybe mod_safeinclude would be of
some use to you, which does populate virtual hosts from a DB.

-- 
The PRI to Alderan is strobing because of Brad White.
Tech Support is taking a percodan.
:: http://www.s5h.net/ :: http://www.s5h.net/gpg.html

Re: what should be done in a vhost_db module?

Posted by Joe Lewis <jo...@joe-lewis.com>.
Robert Schulze wrote:
> Tom Donovan schrieb:
>
> >
>> In a threaded mpm, the same r->server struct is used by multiple 
>> requests simultaneously.  It isn't safe to change anything in it on 
>> the fly.
>
> OK, thats a pity.
>
> I now tried to set several other variables ("Host" in r->headers_in, 
> r->hostname, etc.) but all those settings did not set the hostname of 
> the virtualhost. You can see that by inspecting /server-status/ and 
> have a look at accesslogs. There will always be the main servername 
> used instead of anything crafted from the module.
>
> In this way, this stuff is useless :-(
> Does anybody know, how to set the hostname for the virtualhost 
> correctly, so that other modules might gather the correct info from 
> the request?
>
>
> One more bad thing is, that one can't set the DocumentRoot without 
> (once again) playing with the core configuration. Simply setting
>
>     apr_table_set(r->subprocess_env, "DOCUMENT_ROOT","/foo/var");
>
> is not enough, since that value will magically become overwritten by 
> later work from apache :-(
>
>
> BTW: I looked at mod_vhost_alias, mod_vhost_dbi, mod_vhost_ldap, 
> mod_vhost-modules to get knowledge about how to write a module and so 
> on, so please point me to them :-)

The magic all happens in the :

ap_hook_translate_name

hook.  That is the function to examine to see what it does.

-- 
Joseph Lewis <http://sharktooth.org/>

Re: Multiple instances of same browser to same server-what happens?

Posted by Joe Lewis <jo...@joe-lewis.com>.
Souramita Sen wrote:
>  
>
> Hi,
>
>  
>
> Greetings!!
>
> I need to confirm the following.
>
>  
>
> When we connect to same server in mutiple browser windows (say IE) almost at
> the same time, how does the browser application distinguish the responses and
> display it in proper window? Is this based on some information which HTTP
> response header keeps for distinguishing the response? If so, what is that
> information?
>
>  
>
> Or, does the browser create different sockets to the same server at the same
> time. 
>
> If so, then based on distination client port number it knows which response
> corresponds to which request and display it properly.
>
>  
>
> Thanks in advance.
>
> Souramita.
>
>
>
> DISCLAIMER:
> This message (including attachment if any) is confidential and may be privileged. If you have received this message by mistake please notify the sender by return e-mail and delete this message from your system. Any unauthorized use or dissemination of this message in whole or in part is strictly prohibited.
> E-mail may contain viruses. Before opening attachments please check them for viruses and defects. While MindTree Limited (MindTree) has put in place checks to minimize the risks, MindTree will not be responsible for any viruses or defects or any forwarded attachments emanating either from within MindTree or outside.
> Please note that e-mails are susceptible to change and MindTree shall not be liable for any improper, untimely or incomplete transmission.
> MindTree reserves the right to monitor and review the content of all messages sent to or from MindTree e-mail address. Messages sent to or from this e-mail address may be stored on the MindTree e-mail system or else where.
>
>   

Souramita Sen;

Here is your reading assignment :

http://www.w3.org/Protocols/rfc2616/rfc2616.html
http://www.google.com/search?q=http+socket+programming
http://www.goldmark.org/jeff/stupid-disclaimers/

Any additional questions on how TCP sockets work should be asked with 
reference to apache module development, since this is what this list is 
for, not HTTP definitions.
-- 
Joseph Lewis <http://sharktooth.org/>


Multiple instances of same browser to same server-what happens?

Posted by Souramita Sen <So...@mindtree.com>.
 

Hi,

 

Greetings!!

I need to confirm the following.

 

When we connect to same server in mutiple browser windows (say IE) almost at
the same time, how does the browser application distinguish the responses and
display it in proper window? Is this based on some information which HTTP
response header keeps for distinguishing the response? If so, what is that
information?

 

Or, does the browser create different sockets to the same server at the same
time. 

If so, then based on distination client port number it knows which response
corresponds to which request and display it properly.

 

Thanks in advance.

Souramita.



DISCLAIMER:
This message (including attachment if any) is confidential and may be privileged. If you have received this message by mistake please notify the sender by return e-mail and delete this message from your system. Any unauthorized use or dissemination of this message in whole or in part is strictly prohibited.
E-mail may contain viruses. Before opening attachments please check them for viruses and defects. While MindTree Limited (MindTree) has put in place checks to minimize the risks, MindTree will not be responsible for any viruses or defects or any forwarded attachments emanating either from within MindTree or outside.
Please note that e-mails are susceptible to change and MindTree shall not be liable for any improper, untimely or incomplete transmission.
MindTree reserves the right to monitor and review the content of all messages sent to or from MindTree e-mail address. Messages sent to or from this e-mail address may be stored on the MindTree e-mail system or else where.

Re: what should be done in a vhost_db module?

Posted by Robert Schulze <rs...@bytecamp.net>.
Tom Donovan schrieb:

 >
> In a threaded mpm, the same r->server struct is used by multiple 
> requests simultaneously.  It isn't safe to change anything in it on the 
> fly.

OK, thats a pity.

I now tried to set several other variables ("Host" in r->headers_in, 
r->hostname, etc.) but all those settings did not set the hostname of 
the virtualhost. You can see that by inspecting /server-status/ and have 
a look at accesslogs. There will always be the main servername used 
instead of anything crafted from the module.

In this way, this stuff is useless :-(
Does anybody know, how to set the hostname for the virtualhost 
correctly, so that other modules might gather the correct info from the 
request?


One more bad thing is, that one can't set the DocumentRoot without (once 
again) playing with the core configuration. Simply setting

	apr_table_set(r->subprocess_env, "DOCUMENT_ROOT","/foo/var");

is not enough, since that value will magically become overwritten by 
later work from apache :-(


BTW: I looked at mod_vhost_alias, mod_vhost_dbi, mod_vhost_ldap, 
mod_vhost-modules to get knowledge about how to write a module and so 
on, so please point me to them :-)

with kind regards,
Robert Schulze

Re: what should be done in a vhost_db module?

Posted by Tom Donovan <do...@bellatlantic.net>.
Robert Schulze wrote:
> Hi,
> 
> I'm currently working on a module to create virtualhosts on the fly out 
> of a cdb-database file.
> 
...
> In one module I saw the following:
> 
> <--------------------------------------------------------------------
> 
> request_rec *top = (r->main)?r->main:r;
> 
> /* ... */
> 
> top->server->server_hostname = apr_pstrdup (top->pool, hostname);
> 
> r->parsed_uri.hostinfo = apr_pstrdup(r->pool,r->server->server_hostname);
> 
> r->parsed_uri.hostname = apr_pstrdup(r->pool,r->server->server_hostname);
> 
> <--------------------------------------------------------------------
> 
> and thats exactly where I got some problems.
> I sometimes noticed, that the virtual host name in /server-status/ 
> contained only weird characters, which I think is a result of freeing a 
> buffer which is currently in use.
> I think the problem is the wrong pool for allocating space for the 
> server_hostname.
> 

In a threaded mpm, the same r->server struct is used by multiple requests simultaneously.  It isn't 
safe to change anything in it on the fly.

It is usually good enough to look up a new root directory and use apr_filepath_merge() to set the 
actual filename in a "translate_name" hook, without trying to update the server_rec.

r->hostname has the requested hostname for any scripts which require this info.

Take a look at mod_vhost_alias, or mod_vhost_dbd at http://dbd-modules.googlecode.com for examples.

-tom-