You are not logged in.

#1 2009-06-23 02:24:04

veek
Member
Registered: 2006-03-10
Posts: 167

Apache DNS: server restart does NOT reload resolv.conf

This is more of an FYI than anything else.

I'm curious if this is common knowledge because it caused me a lot of grief. I was getting name resolution errors from my php and python scripts under apache:

[Errno -3] Temporary failure in name resolution

I made a test php script that simply called gethostbyname('www.google.com') which is supposed to return google's IP address. The script would work from the command line but not under Apache. To increase my confusion the script would sometimes work under Apache and sometimes not.

The cause of this problem was that on my laptop I have the apache deamon load on boot, but depending on where I am I bring up my wireless connection later on and get an IP using dhcp. This meant dhcpcd would update resolv.conf with nameservers after apache was already running.

What tripped me up and caused me to spend an absurd amount of time on a trivial problem was an assumption. I thought restarting apache would cause it to reload resolv.conf, but it doesn't. You actually have to stop the server and then start it again. But wait isn't that the same thing?! No. No it's not. It didn't occur to me that there would be a difference between issuing a restart vs. stopping and then starting the server in separate steps. Turns out there's a big one.

I googled extensively and found several similar cases with no apparent solution...I'm assuming they had the same problem and eventually the same resolution.

Embarrassingly I think I encountered this problem at some point in the past and forgot about it....

The lesson: if you change resolv.conf don't use /etc/rc.d/httpd restart or apachectl restart. Stop the server first and then start it again using the stop and start commands.

Offline

#2 2009-06-23 06:19:15

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Apache DNS: server restart does NOT reload resolv.conf

That seems fishy to me.
Were you using a php opcode cacher (like eaccelerator or php-apc)?


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2009-06-23 10:33:50

veek
Member
Registered: 2006-03-10
Posts: 167

Re: Apache DNS: server restart does NOT reload resolv.conf

No wasn't using a php opcode cacher. Actually I first encountered the problem with a WSGI python proxy app, but there were more google hits with php so I just wrote a tiny php test script...what seems fishy to you?

My main point was 'httpd restart' doesn't cause the server to reload resolv.conf.

To verify I just checked using wireshark to watch the destination for DNS packets.

First I used the default resolv.conf that is written by dhcpcd. I accessed the test script through apache which causes it to generate a DNS query and send a DNS packet to the name server in resolv.conf. I could see this in wireshark.

Next I replaced the existing name server in resolv.conf with an OpenDNS name server and issued an 'httpd restart'. I reloaded the page with the php script and I could clearly see that the requests were still going to the old name server instead of the new OpenDNS server.

Finally I did a 'httpd stop' and 'httpd start' and then reloaded the page with the script. After that, DNS requests started going to the OpenDNS server instead of the old one.

So my conclusion is apache keeps name servers in memory through a restart, and only sources resolv.conf from a cold start. I don't think it's all that intuitive, but that's what seems to happen.

Edit: This is on apache 2.2.11.

Last edited by veek (2009-06-23 10:42:48)

Offline

#4 2009-06-23 16:35:19

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Apache DNS: server restart does NOT reload resolv.conf

apache itself doesn't 'source' the resolv.conf. A c library that is compiled into apache does (libc/glibc).
I believe that libc/glibc does indeed cache the resolv.conf values, so in that way you are absolutely right.

In the arch init script, 'httpd restart' actually runs apachectl restart. I _think_ apachectl restart is the same as apachectl graceful (based on the man page I have here), so that makes sense that you weren't seeing a behavior change (parent reloads conf, reaps children, then respawns them).

However, on other distributions, a 'restart' in the init scripts is sometimes an 'apachect stop (or graceful-stop); sleep; apachectl start' operation, with a separate target for 'graceful reload'. So..I guess the 'fishy' part to me was thinking about how the init script was written on the distros I use at work, instead of how the arch init script is written. In this case, you are completely right. The arch initscipt appears to effectively do a 'graceful' when you tell it to restart, instead of doing a stop, sleep, start.

I retract my 'fishy'. big_smile

Last edited by cactus (2009-06-23 16:35:38)


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2009-06-24 00:39:13

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: Apache DNS: server restart does NOT reload resolv.conf

use the nscd daemon wink Simply start it, it watches for changes at some databases files (see manual page). You don't need to restart anymore

/etc/rc.d/nscd start or put at /etc/rc.conf wink

Good Luck

Offline

Board footer

Powered by FluxBB