Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Fix Apache directory lists on dynamic IP sites UNIX
Like many people I use a dynamic dns service for the webserver on my cable modem. Unfortunately I noticed that when accessing the web site from outside my LAN, only exact URLs generated a correct web page. For example http://mysite.dnsalias.com:8080/aa would get a error, but http://mysite.dnsalias.com:8080/aa/index.html would get the expected page. On my LAN, the results were the expected page, or if the page wasn't there a directory listing.

I finally traced the problem to the webserver returning a redirect to the browser to 172.16.1.2 (the LAN ip of my webserver) instead of the IP address of my router or the IP name from the dynamic DNS service. The internal IP address obviously doesn't work outside my LAN.

The fix is edit the apache config file at /etc/httpd/httpd.conf, and search for the section that contains:
# ServerName allows you to set a host name which is...
# your server if it's different than the one the...
# "www" instead of the host's real name).
Once you've found this section, add the line:
ServerName mysite.dnsalias.com
Replace mysite.dnsalias.com with the DNS name you have mapped to your webserver via the dynamic DNS service). Save and restart apache and your webserver should work as expected.
    •    
  • Currently 4.00 / 5
  You rated: 4 / 5 (3 votes cast)
 
[3,612 views]  

Fix Apache directory lists on dynamic IP sites | 5 comments | Create New Account
Click here to return to the 'Fix Apache directory lists on dynamic IP sites' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
hostname
Authored by: synathome on Nov 11, '02 07:41:17AM

I can't really say whether or not this will work for you (I don't have a LAN with NAT), but you might try setting the hostname from the command line ("sudo hostname mysite.dnsalias.com") to achieve the same result. Making the change at this "central point" might mean that other apps or utilities can take advantage of it (e.g., I used it for ssh-keygen).

If you use a utility such as DNSUpdate, you should be able to add the command to its StartupItem (/Library/StartupItems/DNSUpdate/DNSUpdate):

[...]

ConsoleMessage "Starting DNS Update"

nohup /usr/local/sbin/dnsupdate daemon > /var/log/dnsupdate.log 2>&1 &
/bin/chmod 664 /var/log/dnsupdate.log
/usr/bin/chgrp admin /var/log/dnsupdate.log

ConsoleMessage "Setting hostname"

hostname mysite.dnsalias.com

[...]

There is probably a much better place to store the hostname than in the startup item. I have also considered adding this functionality to the DNSUpdate utility (thank you for making it open source, Julien) but a.) it would require a UI change so that the user could decide which of h/er incoming hostnames would be used as the outgoing one, and b.) it may be a bad idea for some reason I don't know about.

Thoughts, anyone?



[ Reply to This | # ]
Actually, you want 'UseCanonicalName'
Authored by: mithras on Nov 11, '02 07:53:26AM

Your solution works, but is suboptimal. When you change the ServerName, anytime you try to access your site from within your home LAN, you'll be redirected to use the external hostname.

Instead of changing the ServerName, search for the line containing the word 'UseCanonicalName'. As explained in httpd.conf, this controls whether the webserver uses the ServerName to construct URLs for redirects or rewrites. For no good reason, that is set to 'On' by default.

Set UseCanonicalName to 'Off', and relative URLs or directory rewrites will use whatever hostname you accessed the site with. That way you can access it via the LAN or via the Internet.

Hope that makes some sense.



[ Reply to This | # ]
Actually, you want 'UseCanonicalName'
Authored by: Anonymous on Nov 11, '02 09:16:33AM

Yes, this is one of my standard modifications every time I do an OS X install.

Really though, in a consumer OS, UseCanonicalName should be set to Off by default.



[ Reply to This | # ]
Actually, you want 'UseCanonicalName'
Authored by: jchi on Nov 11, '02 01:03:09PM

That is a better solution. Thanks!



[ Reply to This | # ]
This tip may not be necessary...
Authored by: gxw on Nov 11, '02 09:30:30AM

I am in a similar situation; running a web server behind a NAT box and using dyndns to get ot my site. I did not have to do the above to get to my site at all.
All I had to do was:
1. Make a web page (obviously)
2. Start Apache on OSX
3. Open a hole in the NAT box.
To get to my site just enter the dyndns name from any external machine (gxw.mine.nu)[1].

1. Available by appointment only (I don't leave the NAT box open except if someone needs to get something from my web site).



[ Reply to This | # ]