How to allow specific IP access in Nginx?

Why does Apache2 serve a subdomain when given an IP?

  • I have a domain hosted on a VPS running CentOS5. Whenever I enter http://my-vps-ip in my browser, I get redirected to http://subdomain.mysite.com. My limited Google-fu is no match for this particular problem, so I'm hoping the folks at AskMeFi can tell me how I should setup Apache so that http://my-vps-ip goes to http://mysite.com and not the subdomain? Extracts from the httpd.conf file:ServerName mysite.com...UseCanonicalName Offconf file defining the subdomain:<virtualhost *:80> ServerName subdomain.mysite.com DocumentRoot /var/www/subdomain ErrorLog /var/log/httpd/subdomain_mysite_com-error.log CustomLog /var/log/httpd/subdomain_mysite-access.log combined <directory> Options Indexes FollowSymLinks AllowOverride AuthConfig FileInfo Indexes Limit Order allow,deny Allow from all </directory> < /virtualhost> Also, FWIW - currently the rDNS lookup on my VPS IP goes to ns1.mysite.com. Many thanks!

  • Answer:

    If you have a name-based virtualhost listening on all IPs, as you do with <virtualhost *:80>, the first virtualhost stanza will be the default, handling any queries which don't match ServerNames from other virtualhost stanzas. Which is to say that what you think is your main server configuration in httpd.conf isn't getting used, except to set up default values, some of which are being overridden by the corresponding values in virtualhost stanzas. If you want mysite.com to be the default, you'll probably have to set up a virtualhost stanza for it, too, and make sure it appears before the subdomain.mysite.com virtualhost stanza. If you're doing this with files in conf.d, numbering them is one way to accomplish that: 00_mysite.com.conf, 10_subdomain.mysite.com.conf, etc. See the "Main host goes away" section at http://httpd.apache.org/docs/2.2/vhosts/name-based.html for more.

your mildly obsessive average geek at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

hades - I already have the following virtual host entries setup at the end of my primary httpd.conf:#Virtual Host for main server<VirtualHost *:80>ServerName www.mysite.comServerAdmin [email protected] /var/www/htmlErrorLog /var/log/httpd/mysite_com-error.logCustomLog /var/log/httpd/mysite-access.log combined</VirtualHost>#Virtual Host to handle non www requests<VirtualHost *:80>ServerName mysite.comServerAdmin [email protected] /var/www/htmlErrorLog /var/log/httpd/mysite_com-error.logCustomLog /var/log/httpd/mysite-access.log combined</VirtualHost>Am I correct in understanding that the entries in my virtual hosts conf file is overriding these entries? So if I were to move these to the top of my virtual hosts conf file - Apache should stop serving subdomain.mysite.com for the VPS IP?

your mildly obsessive average geek

Yes, that's the normal setup.

ArkhanJG

Also, you can http://httpd.apache.org/docs/2.2/vhosts/examples.html#default so that you don't have to pay attention to the order of your vhosts.

rhizome

There's probably a line in the middle of your main httpd.conf which reads:Include conf.d/*.confIf your subdomain is being configured from a file in conf.d, it's actually coming before those stanzas at the end of httpd.conf, because of the include statement's position. Like rhizome said, you can use _default_, or you can put your main server configs in conf.d files lexically earlier than your subdomain config files. Also, you don't need two stanzas for www.mysite.com and mysite.com; you can putServerAlias mysite.comin the first stanza and get rid of the second.

hades

I tried adding the _default_ option to the Virtual Host entry in the httpd.conf but I think the "Include conf.d/*.conf line" is still taking precedence. In any case, I moved the Virtual Host entry for mysite.com to the same conf file as the sub-domain and added the _default_ option. After that, it started working! hades - thanks for the tip on the ServerAlias.

your mildly obsessive average geek

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.