【问题标题】:Remove www subdomain in nginx and puma config删除 nginx 和 puma 配置中的 www 子域
【发布时间】:2016-03-07 09:16:49
【问题描述】:

我正在使用 puma 和 nginx 在 AWS 上运行 rails 3.2 应用程序。我在负载均衡器 (ELB) 上终止了我的 HTTPS。如何将 www.mydomain.com 重定向到 mydomain.com?我下面的配置不起作用。

user  nginx;
worker_processes  auto;
worker_rlimit_nofile 100000;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections 1024;
    multi_accept on;
}

http {
    include         /etc/nginx/mime.types;
    default_type    application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;

    keepalive_timeout   65;
    keepalive_requests 200;
    reset_timedout_connection on;
    types_hash_max_size 2048;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.

    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

    upstream myapp {
        # Path to Puma SOCK file
        server unix:/var/www/nginx-default/myapp/tmp/sockets/puma.sock fail_timeout=0;
    }

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

    server {
        listen       80;
        server_name  my-app.com;
        root         /var/www/nginx-default/myapp/public;

        #charset koi8-r;

        # set client body size (upload size)
        client_max_body_size 100M;

        location / {
           proxy_pass http://myapp; # match the name of upstream directive which is defined above
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        # We don't need .ht files with nginx.
        location ~ /\.ht {
            deny all;
        }

        open_file_cache max=1000 inactive=20s;
        open_file_cache_valid    30s;
        open_file_cache_min_uses 2;
        open_file_cache_errors   on;
    }

}

如果我删除

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

并使用

server {
    listen       80;
    server_name  localhost;
    root         /var/www/nginx-default/myapp/public;

    #...
}

它可以工作,但不处理 www 子域。

【问题讨论】:

    标签: ruby-on-rails amazon-web-services nginx ruby-on-rails-3.2 puma


    【解决方案1】:

    这是一个推测性的答案,因为我不使用 AWS 和 ELB。但是,在重定向中使用 $scheme 会导致 HTTPS 被重定向到 HTTP:

    如果您在 ELB 不接受 HTTP,则应仅重定向到 HTTPS,其中:

    server {
        listen      80;
        server_name www.my-app.com;
        return 301  https://my-app.com$request_uri;
    }
    

    但是,如果您在 ELB 上接受 HTTP 或 HTTPS,您应该能够使用 ELB 插入的 X-Forwarded-Proto 标头。试试这个:

    server {
        listen      80;
        server_name www.my-app.com;
        return 301  $http_x_forwarded_proto://my-app.com$request_uri;
    }
    

    【讨论】:

    • 我都试过了。还是不行。我们的 ELB 可以同时使用 http 和 https,但我们使用 rails 将它们全部重定向到 https。
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多