簡體   English   中英

將Amazon EC2 Node.js實例重定向到HTTPS

[英]Redirecting amazon ec2 nodejs instance to HTTPS

我正在Amazon EC2實例上運行節點/快速應用程序,沒有負載均衡器,免費套餐。 我正在嘗試將所有內容重定向到HTTPS。 到目前為止,我所做的一切都是通過EB CLI(eb部署,eb ssh等)進行的。

我從letsencrypt(certbot)獲得了免費證書,並且按照本教程中的說明設置了nginx.conf。 我可以訪問應用程序URL的http和https版本。 http檢索了我的nodejs應用程序,但是https返回了默認的nginx html頁面(來自/ usr / share / nginx / html)。

我想僅在HTTPS上獲取我的nodejs應用程序,並將所有HTTP請求重定向到HTTPS。

我的nginx.conf如下:

# 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

}

要重新路由端口,可以在EC2實例中添加iptables路由,例如:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 443

*確保在EC2安全組中,入站HTTP端口80 source =“ Anywhere”。

要查看iptables路由條目,請運行:

須藤iptables -t nat -L

如果需要刪除路由條目(第一行),請運行:

須藤iptables -t nat -D PREROUTING 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM