【发布时间】:2018-08-17 11:52:30
【问题描述】:
我正在尝试让我的网站只接受安全的 HTTPS 网址。
我有一个节点 Web 应用程序当前部署到 Elastic Beanstalk(运行状况 - 正常)。该网站此时运行良好。
我在我的 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。 所以我决定关注these instructions。
我更新了我的 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"
当我登录到实例时,我可以在 var/app/current 中看到我的部署文件和我的 node.js 索引文件
我浏览器中的 url 路径绝对正确。谁能告诉我为什么实例在 /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
}
【问题讨论】:
-
请包含您的整个 nginx 配置。它何时将请求转发到您的节点服务器?
标签: amazon-web-services nginx amazon-ec2 amazon-elastic-beanstalk