2016-07-17 13 views
0

私はノード/エクスプレスアプリケーションをamazon ec2インスタンス上で実行しています。ロードバランサ、フリー層はありません。私はすべてをHTTPSにリダイレクトしようとしています。今まで私がやったことはすべてEB CLI(eb deploy、eb sshなど)を介してでした。amazon EC2 nodejsインスタンスをHTTPSにリダイレクト

私はletsencrypt(certbot)から無料の証明書を取得しました。このtutorialで説明されているようにnginx.confを設定しました。私は、httpとhttpsバージョンの両方のアプリURLにアクセスできます。 httpは自分のnodejsアプリケーションを取得しますが、httpsはデフォルトのnginx htmlページ(/ usr/share/nginx/htmlから)を返します。

nodejsアプリケーションをHTTPSでのみ取得し、すべてのHTTP要求をHTTPSにリダイレクトしたいとします。

# Elastic Beanstalk managed configuration file 
# Some configuration of nginx can be by placing files in /etc/nginx/conf.d 
# using Configuration Files. 
# http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html 
# 
# Modifications of nginx.conf can be performed using container_commands to modify the staged version 
# located in /tmp/deployment/config/etc#nginx#nginx.conf 

# Elastic_Beanstalk 
# For more information on configuration, see: 
# * Official English Documentation: http://nginx.org/en/docs/ 
# * Official Russian Documentation: http://nginx.org/ru/docs/ 

user nginx; 
worker_processes auto; 

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

pid  /var/run/nginx.pid; 


events { 
    worker_connections 1024; 
} 

http { 

    port_in_redirect off; 
    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; 

    keepalive_timeout 65; 
# Elastic Beanstalk Modification(EB_INCLUDE) 

    log_format healthd '$msec"$uri"' 
         '$status"$request_time"$upstream_response_time"' 
         '$http_x_forwarded_for'; 
    server { 
    listen 80; 
    server_name localhost; 
    location/{ 
     # Redirect any http requests to https 
     if ($http_x_forwarded_proto != 'https') { 
     rewrite^https://$host$request_uri? permanent; 
     } 
    } 
    } 

    server { 
     listen  443 ssl; 
     listen  [::]:443 ssl; 
     server_name localhost; 

     ssl_certificate "/etc/letsencrypt/live/domain/fullchain.pem"; 
     ssl_certificate_key "/etc/letsencrypt/live/domain/privkey.pem"; 
     # It is *strongly* recommended to generate unique DH parameters 
     # Generate them with: openssl dhparam -out /etc/pki/nginx/dhparams.pem 2048 
     #ssl_dhparam "/etc/pki/nginx/dhparams.pem"; 
     ssl_session_cache shared:SSL:1m; 
     ssl_session_timeout 10m; 
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
     ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP; 
     ssl_prefer_server_ciphers on; 

     # Load configuration files for the default server block. 
     include /etc/nginx/default.d/*.conf; 

     error_page 404 /404.html; 
      location = /40x.html { 
     } 

     error_page 500 502 503 504 /50x.html; 
      location = /50x.html { 
     } 
    } 


include /etc/nginx/conf.d/*.conf; 
# End Modification 

} 

答えて

0

ポートを再ルーティングするには、たとえば、あなたのEC2インスタンスにルーティングのiptablesを追加することができます:次のように

私nginx.confがある

sudoのiptablesの-t NATを - PRERTOUTING -p tcp --dport 80 -j REDIRECT - ポート443

* EC2セキュリティグループでは、受信HTTPポート80のsource = "Anywhere"であることを確認してください。

は実行、ルーティングエントリのiptablesを表示するには、次の

sudoのiptablesの-tのnat -L

あなたはルーティングエントリ(最初の行)を削除する必要がある場合は、次のコマンドを実行します

sudo iptables -t nat -D PREROUTING 1

関連する問題