大网数据广告活动
服务器运维问答

Strip www with NGINX behind AWS ELB

· 52CE 我爱测

I'm trying to remove the www from my request urls but when I add the following to my nginx config my AWS ELB health check fails. server { listen 80; server_name www.example.com; return 301 $scheme://example.com$request_uri; } Any…

问题原文

原帖链接 · jwerre · CC BY-SA 3.0

I'm trying to remove the www from my request urls but when I add the following to my nginx config my AWS ELB health check fails.

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

Any idea what could be causing the issue? This is the rest of my config:

server {

    listen 80;

    server_name .example.com;
    root /var/www/static;
    index index.html;

    try_files $uri.html $uri $uri/ =404;

    charset UTF-8;

    error_log /var/log/error.log;
    access_log /var/log/access.log;

    # ELB test route
    location /test {
        add_header X-Robots-Tag noindex;
        default_type text/plain; 
        return 200 'success';
    }

    # Redirect any request http request that come from AWS ELB to https
    if ($http_x_forwarded_proto = "http") {
        return 301 https://$server_name$request_uri;
    }
}

Here is my Health Check setup:

已采纳解答

原帖链接 · Joseph Montanaro · CC BY-SA 3.0

From the Amazon docs:

To ensure that your load balancer can scale properly, verify that each subnet for your load balancer has a CIDR block with at least a /27 bitmask (for example, 10.0.0.0/27) and has at least 8 free IP addresses. Your load balancer uses these IP addresses to establish connections with the instances.

This makes it sound like the ELB health check is made via AWS private IP rather than domain name. From what I know of Amazon, that's how they prefer to do things when they can (for obvious performance reasons, no need for DNS lookup, can be handled by closer routers, etc.)

If that's the case, then the host header (unless it's blank, I'm not 100% sure how host headers work when you're making a request to a bare IP) will be the private AWS address of the EC2 instance (I'm assuming it's EC2) running the webserver. This impacts the way Nginx chooses what virtual server should handle the request.

Since Nginx has two server blocks, it has to decide which one gets any given request. The process by which it chooses server blocks (details here) is as follows:

  • First it looks for servers listening on the port on which the request came in. In this case, both servers are listening on the same port, so this doesn't help any.

  • Then it looks at the host header of the request, and looks for servers with a server_name that matches the host header. In this case, none of them will match, because the host header is the private AWS IP assigned to the instance (or blank, as I said I'm not sure.)

  • Finally, if neither of those yields a match, it just goes with whichever server is the default. In the absence of an explicit default declaration, the server which is defined first in the site config is chosen as the default.

If your redirect server comes before the main server config (as I would imagine it does) then this is probably your problem. You need to do one of three things:

  • Specify that you want the other server (the main server) to be the default by adding default_server to the listen directive, i.e. listen 80 default_server;

  • Switch the location /test block to the other server block, or

  • Reverse the order in which the server blocks are defined.

The first option seems like the way to go, since it makes sense for the default_server directive to be explicit rather than implicit, and it makes sense for the main server to be the default rather than the you-had-one-job redirect server.

If I'm correct in my hypothesizing, that should solve it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。