首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将amazon ec2 nodejs实例重定向到HTTPS

问将amazon ec2 nodejs实例重定向到HTTPS
EN

Stack Overflow用户
提问于 2016-07-17 10:52:39
回答 1查看 253关注 0票数 0

我正在一个亚马逊ec2实例上运行一个节点/快递应用程序,没有负载均衡器,免费层。我试着把所有的东西都转到HTTPS。到目前为止,我所做的一切都是通过EB CLI完成的(eb部署、eb ssh等等)。

我从letsencrypt (certbot)获得了一个免费证书,我已经设置了nginx.conf,如本教程中所解释的那样。我能够访问应用程序URL的http和https版本。http检索我的nodejs应用程序,但是https返回默认的nginx页面(从/usr/share/nginx/ html )。

我只想在HTTPS上获得我的nodejs应用程序,并将所有HTTP请求重定向到HTTPS。

我的nginx.conf如下:

代码语言:javascript
复制
# 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

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-09 06:04:12

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

sudo iptables -t nat -A PREROUTING -p tcp -dport 80 -j重定向到端口443

*确保在EC2安全组中,入站HTTP端口80源= "Anywhere“。

若要查看iptables路由条目,请运行:

数多页表-t nat -L

如果需要删除路由条目(第一行),请运行:

数多页表-t nat -D PREROUTING 1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38420444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档