在 Amazon EC2 上将 HTTP URL 重写为 HTTPS 时出现问题



我试图让我的网站只接受安全的HTTPS网址。

我有一个节点 Web 应用程序当前部署到 Elastic Beanstalk (Health - OK)。此时网站运行良好。

我在 EC2 实例前面添加了一个负载均衡器(添加了 ssl 证书) 侦听器的配置如下:

Load Balancer Protocol: HTTPS Port: 443 - Instance Protocol HTTP: Instance Port: 80
Load Balancer Protocol: HTTP Port: 80 - Instance Protocol HTTP: Instance Port: 80

显然,EC2 负载均衡器不支持 HTTP 重定向到 HTTPS。 所以我决定遵循这些说明。

我更新了我的nginx.conf,并在HTTP部分添加了以下内容:

server {
listen         80;
server_name    mysite.com;
if ($http_x_forwarded_proto = 'http') {            
return 301 https://$server_name$request_uri
}
}

我重新启动了实例,它当前仍在服务中。当我尝试访问 url 路径时,重定向有效,但所有页面都给出 404 错误。

我检查了实例上的日志,发现以下内容:

2018/03/09 00:02:27 [error] 3533#0: *57 open() "/usr/share/nginx/html/channel/create" failed (2: No such file or directory), client: 172.31.57.30, server: mysite.com, request: "GET /channel/create HTTP/1.1", host: "mysite.com"
2018/03/09 00:02:28 [error] 3533#0: *57 open() "/usr/share/nginx/html/channel/create" failed (2: No such file or directory), client: 172.31.57.30, server: mysite.com, request: "GET /channel/create HTTP/1.1", host: "mysite.com"

当我登录到实例时,我可以使用我的节点.js索引文件在var/app/current中看到我的部署文件

我的浏览器中的网址路径绝对正确。谁能告诉我为什么实例在/usr/share/nginx/html/中查找文件?

/usr/share/nginx/html/目前只有默认的 Nginx 文件。

# Elastic Beanstalk Managed
# 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;
server {
listen         80;
server_name    mysite.com;
if ($http_x_forwarded_proto = 'http') {            
return 301 https://$server_name$request_uri;
}
}
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';
include /etc/nginx/conf.d/*.conf;
# End Modification
}

您必须通过此处记录的 elasticbeanstalk 过程编辑 nginx 配置,通过 beanstalk 部署时,任何编程语言都是一样的。

这在更改我的节点索引的位置后有效.js。将 Nginx.conf 更新为以下位置:

location / {
root /var/app/current;
}

最新更新