0

我正在使用puma和nginx在AWS上運行rails 3.2應用程序。我在負載均衡器(ELB)上終止我的HTTPS。我如何將www.mydomain.com重定向到mydomain.com?我的配置下面不起作用。在nginx和puma配置中刪除www子域

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子域。

回答

0

這是一個推測性答案,因爲我不使用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; 
} 
+0

我試過了。仍然不起作用。我們的ELB可以同時使用http和https,但是我們可以使用rails將它們全部重定向到https。 –