Nginx 服务器上的 Amazon ELB 和多域

Dan*_*ola 2 nginx load-balancing amazon-ec2 amazon-web-services amazon-elb

我有一个运行 Nginx 的 EC2 实例,其中包含多个域。我的配置是这样开始的:

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;
}
Run Code Online (Sandbox Code Playgroud)

我不确定在 ELB(亚马逊)上使用哪个 ping 路径,因为如果我尝试 HTTP,实例总是失败。如果我尝试 TCP(端口 80),则 ping 成功。我必须使用 HTTP,因为我想使用粘性。

有什么建议吗?谢谢,丹尼

Luk*_*kas 5

Serverfault 上的另一个答案告诉我们,ELB 只期望200 OK状态代码。
这是您的设置的问题,因为rewrite会导致3**状态代码。

为 ping 路径创建一个新位置,如下所示:

location = /elb-ping {
    return 200;
}
Run Code Online (Sandbox Code Playgroud)

然后确保使用www。为 ping 避免重定向

如果您无法将 ping 域更改为 www。:
您必须将重定向移动到 www。到一个server街区。
或者您在配置中定义静态 ping 目标。

简单的方法

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;
    }
Run Code Online (Sandbox Code Playgroud)

方式太复杂

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)
}
Run Code Online (Sandbox Code Playgroud)