使用 Elastic Beanstalk (Amazon Linux 2) 和 Nginx 时,您有两种解决方案:
扩展 Elastic Beanstalk 默认 nginx.conf
在您的源代码中创建一个名为 .platform/nginx/conf.d/redirections.conf 的文件,其中包含:
server {
server_name .elasticbeanstalk.com;
return 301 https://example.com$request_uri;
}
Nginx 文档:https://www.nginx.com/blog/creating-nginx-rewrite-rules/
(example.com 是您自己的域)
创建您自己的 nginx.conf 替换 Elastic Beanstalk 中的默认配置
- 通过使用 SSH (*) 连接到您的 Elastic Beanstalk EC2 实例,从原始
/etc/nginx/nginx.conf 复制内容
- 在您的源代码中创建一个名为
.platform/nginx/nginx.conf 的文件并粘贴内容
- 根据您的需要修改并添加:
server {
server_name .elasticbeanstalk.com;
return 301 https://example.com$request_uri;
}
您最终应该得到如下所示的 /etc/nginx/nginx.conf(取自 Amazon Linux 2,截至 2020 年 9 月 8 日):
# Elastic Beanstalk Nginx Configuration File
user nginx;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 32136;
events {
worker_connections 1024;
}
http {
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"';
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
listen 80 default_server;
access_log /var/log/nginx/access.log main;
client_header_timeout 60;
client_body_timeout 60;
keepalive_timeout 60;
gzip off;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Include the Elastic Beanstalk generated locations
include conf.d/elasticbeanstalk/*.conf;
}
# ADDED
server {
server_name .elasticbeanstalk.com;
return 301 https://example.com$request_uri;
}
}
有关 Nginx 配置的更多信息
同时,我还建议对您的 Nginx 配置进行其他修改。
将 www 重定向到根目录
将www.example.com 重定向到example.com 的示例。
# .platform/nginx/conf.d/redirections.conf
# https://stackoverflow.com/a/43089681
# https://tribulant.com/docs/hosting-domains/hosting/9867/redirecting-to-www-or-non-www/
# This can be done at the load balancer level but I prefer to do it here
# Test this with `curl --head https://www.example.com` and `curl --head http://www.example.com`
server {
server_name www.example.com;
return 301 https://example.com$request_uri;
}
先决条件:
HTTP 安全标头
为了安全起见,我建议设置这些 HTTP 标头:
# .platform/nginx/conf.d/security_headers.conf
# Remove Nginx version in error page and header
server_tokens off;
# Security headers thanks to https://observatory.mozilla.org/ and https://webpagetest.org/
# Inspired by https://www.mozilla.org/ HTTP headers
# https://gist.github.com/plentz/6737338
# https://github.com/GetPageSpeed/ngx_security_headers
add_header Content-Security-Policy "default-src 'self';
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
文件压缩(.js、.css、.html...)
您可以使用gzip on; 启用压缩。不幸的是you cannot extend the default nginx.conf to enable compression。您必须复制粘贴并修改原始 nginx.conf (.platform/nginx/nginx.conf)。
注意:您可以拥有自己的.platform/nginx/nginx.conf 并仍然使用.platform/nginx/conf.d/ 目录中的文件。
将 HTTP 重定向到 HTTPS
2 solutions:使用负载均衡器(Application Load Balancer)或自定义.platform/nginx/nginx.conf。
# .platform/nginx/nginx.conf
...
server {
listen 80 default_server;
...
# ADDED
# [AWS documentation - Configuring HTTP to HTTPS redirection](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html)
# https://github.com/awsdocs/elastic-beanstalk-samples/blob/9720e38e9da155752dce132a31d8e13a27364b83/configuration-files/aws-provided/security-configuration/https-redirect/nodejs/https-redirect-nodejs.config#L61
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
if ($http_x_forwarded_proto = "http") {
return 301 https://example.com$request_uri;
}
...
}
...
(*) 在您的 EC2 实例安全组中打开端口 22(类似于 *AWSEBSecurityGroup*)然后转到:
EC2 > 实例 > 连接 > EC2 实例连接(基于浏览器的 SSH 连接)