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 have an EC2 instance running Nginx with several domains. My config starts like this:

server {
    listen 80;
    #disable_symlinks off;
    server_name _; #allow few domains

    #Adding 'www.' to url in case it doesen't
    if ($host !~* ^www\.) {
       rewrite ^(.*)$ http://www.$host$1 permanent;
    }

  location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.php;
  }
    index index.html index.php;
}

I'm not sure which ping path to use on the ELB (amazon) because if i try HTTP the instance always fails. If i try TCP (port 80) the ping succeed. I have to use HTTP because i want to use stickiness.

Any advice? thanks, Danny

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Another answer on Serverfault tells us, that the ELB expects nothing else than a 200 OK status code.
This is a problem for your setup, because rewrite will cause 3** status codes.

Create a new location for the ping path like this:

location = /elb-ping {
    return 200;
}

Then make sure to use www. for the ping to avoid the redirect

If you can't change the ping domain to www. :
You will have to move the redirect to www. to a server block.
Or you define a static ping target in your config.

Easy way

server {
    listen 81; # What about using a different port for ELB ping?
    server_name _; # Match all if the listen port is unique,
    #server_name elb-instance-name; # Or match a specific name.

    return 200; # Not much magic.
}

server {
    listen 80;
    #disable_symlinks off;
    server_name _; #allow few domains

    #Adding 'www.' to url in case it doesen't
    if ($host !~* ^www\.) {
        rewrite ^(.*)$ http://www.$host$1 permanent;
    }

    location / {
        if (!-e $request_filename){
            rewrite ^(.*)$ /index.php;
        }
        index index.html index.php;
    }

Way too complex way

server {
    listen 80;
    # match hostnames NOT being www.*
    # Unfortunately matches only names with length >= 4
    server_name ~^(?P<domain>[^w]{3}[^\.].*)$;
    location / {
        rewrite ^(.*)$ $scheme://www.$domain$1; # rewrite to www.*
    }

    location = /elb-ping {
        return 200;
    }
}

server {
    listen 80;
    server_name www.*; # match hostnames starting with www.*

    ## YOUR EXISTING CONFIG HERE (without the if block and elb-ping location)
}
share|improve this answer
    
Wow...i think this is the way to go but i dont follow all the way, can you please elaborate or give an example on the "/elb-ping"? Thx!! –  Danny Valariola Mar 10 '13 at 18:46
1  
When you add "/elb-ping" as a location, "www.domain.com/elb-ping" will be your ping path. The = modifier on the location block makes sure no other (wildcard/regex) block overrides it. The "www." is reuired because the URL will else be rewritten - which would then cause a 301 Moved Permanently status code. –  Lukas Mar 10 '13 at 18:50
    
I see, i thing though...according to Amazon guys in order to check the ping i should issue something like this: http://<instance>/pingpath. this means that the instance name always appear as the host which will cause the ping path to be irrelevant since my ping path will be based on the domain name. –  Danny Valariola Mar 10 '13 at 19:51
    
@DannyValariola: you mean you can't tell the ELB to ping to the www.* domain? See my update for this –  Lukas Mar 10 '13 at 22:37
    
i dont think you can tell the ELB to ping a domain since the default ping is http://<instance_name> then the path...since i have multiple domains on this machine i can't put the domain name, but i will give a try to your configuration!! thx! –  Danny Valariola Mar 11 '13 at 20:01

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.