Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm running nginx version 1.1.19 and I have server directives configured to serve up different pages depending on whether someone hits my server with hostname domain.com (I show the main webpage) or whether they hit my server with anything else (I show a "No direct matches, but you still get this nice page" page).

fileA

server {
       listen 80;
       server_name domain.com;
       root /home/ubuntu/http_resources/apex/public;

       location / {
           index index.html;       
       }

fileB

server {

       listen 80;
       server_name "";          #Should catch everything else.

       root /home/ubuntu/http_resources/otherwise/public;

       location / {
            index index.html;       
       }

These two server block configurations are in two separate files symlinked to sites-enabled. This works as I expect it to.

  • domain.com -> Shows main webpage
  • dummy.domain.com -> Shows the "No direct matches..." page
  • www.domain.com -> Shows the "No direct matches..." page

Problem

I would like to redirect www.domain.com to domain.com, but have every other subdomain still hit the "No direct matches..." page. I create a file containing

fileC:

server {
       listen 80;
       server_name www.domain.com;
       rewrite ^ http://domain.com$request_uri? permanent;
}

and symlink the file to sites-enabled, thinking this will match www.domain.com and prevent www.domain.com from going to the web root of the server block in fileB.

This seems to start redirecting anything.domain.com to domain.com for a few minutes and domain.com still seems to operate. (Maybe due to caching or something). A few minutes later! my Amazon load balancer says that domain.com/index.html is no longer active on the application server (details: the load balancer checks the naked domain domain.com for index.html to determine whether traffic should go to the server; also there is only one application server behind the load balancer). Then the load balancer stops routing to the server since domin.com does not seem to be available, so then of course no domain.com related urls work in my browser since the load balancer does not point to anything.

I did try to run 'wget public_ip_of_server_behind_balancer/index.html and I get an index.html file that basically says

Site Closed Down
This site has been closed by the service provider.

Deleting the symlink to fileC and restarting nginx causes the load balancer to reconnect to the application server and all works as described originally. What in the world am I doing wrong?

share|improve this question
    
Enable debug and take a look at the error log to see what's going on. –  quanta Aug 13 '12 at 2:54

1 Answer 1

up vote 1 down vote accepted

I have no experience with Amazon load balancer. So, my answer will not address the problem you mentioned, but will hopefully help you to narrow down the issues.

For fileB, according to Nginx wiki on server_name, server_name "" means this server block will catch the requests without the "Host" header. So, I'd recommend to use of default_server in listen directive like this (as mentioned here)...

http {
  index index.html;

  server {
    listen 80 default_server;
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    access_log logs/default.access.log main;

    server_name_in_redirect off;

    root  /var/www/default/htdocs;
  }
}

For fileC, as you are doing permanent redirection, I'd recommend return directive, instead of return regex replacement permanent; directive and I'd remove the ? at the end of the URL too, like this...

server {
       listen 80;
       server_name www.domain.com;
       return 301 $scheme://domain.com$request_uri;
}

Hope this helps to some extend.

share|improve this answer
    
Just making the change to fileB you suggest solves my issue. Now domain.com is resolves normally, www.domain.com is redirected to domain.com, and anything else goes to a catch all page. I tried using your fileC, but it did not seem to redirect so I just kept my own, which now seems to work. Thanks soo much! I would give you more points if I could! –  dghubble Aug 13 '12 at 4:31
    
Interestingly, this suggests the use of server_name "" was interfering with resolving the apex domain domain.com. Even if that were happening though, my "No direct matches" page also uses an index.html page so the load balancer should have still let the server pass the health check. If anyone in the future has any theories, please comment as I am curious. Regardless, the issue has disappeared. Thanks. –  dghubble Aug 13 '12 at 4:39
    
@grepic Glad to know that the issue has disappeared. I would give you more points if I could! It looks like you can offer a bounty after 48 hours. I'm not sure about it, though. :) –  Pothi Aug 13 '12 at 4:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.