the*_*est 5 apache dns amazon-web-services amazon-route53 amazon-elastic-beanstalk
我有一个托管在 AWS Elastic Beanstalk 上的应用程序,它被分配了一个环境 URL,如下所示:
<my-appname>.<aws-region>.elasticbeanstalk.com
我还注册了一个域名,如下:
my-appname.com
在 AWS Route 53 中,我指出了A ALIASEBmy-appname.com环境:
my-appname.com>A ALIAS <my-appname>.<aws-region>.elasticbeanstalk.com
我通过我的注册商设置了 Route 53 域名服务器,以通过 Amazon 管理 DNS。
一切正常
我想了解如何确保对<my-appname>.<aws-region>.elasticbeanstalk.com>域的任何请求都到达301域my-appname.com。
我当前使用 ApacheRewriteRule将所有非 www 请求重定向到网站的 www 版本,在文件中使用以下内容.config:
<If "'%{HTTP_HOST}' !~ /^www\./">
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</If>
Run Code Online (Sandbox Code Playgroud)
HTTP_HOST简单地更改为是一个好的做法吗my-appname.com?
编辑:无论如何,这种方法似乎不起作用。不知道为什么?
使用 Elastic Beanstalk (Amazon Linux 2) 和 Nginx 时,您有两种解决方案:
.platform/nginx/conf.d/redirections.conf创建一个在源代码中命名的文件,其中包含:
server {
server_name .elasticbeanstalk.com;
return 301 https://example.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
Nginx 文档:https://www.nginx.com/blog/creating-nginx-rewrite-rules/
(example.com 是您自己的域名)
/etc/nginx/nginx.conf使用 SSH 连接到 Elastic Beanstalk EC2 实例,复制原始内容(*).platform/nginx/nginx.conf并粘贴内容server {
server_name .elasticbeanstalk.com;
return 301 https://example.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
您最终应该得到/etc/nginx/nginx.conf如下所示的结果(截至 2022 年 5 月 8 日取自 Amazon Linux 2):
# 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;
}
}
Run Code Online (Sandbox Code Playgroud)
同时,我还建议对 Nginx 配置进行其他修改。
将www.example.com重定向到 example.com 的示例。
# .platform/nginx/conf.d/redirections.conf
# /sf/answers/3016277701/
# 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;
}
Run Code Online (Sandbox Code Playgroud)
先决条件:
为了安全起见,我建议设置这些 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";
Run Code Online (Sandbox Code Playgroud)
您可以使用 启用压缩gzip on;。不幸的是,您无法扩展默认的 nginx.conf 来启用压缩。您必须复制粘贴并修改原始的 nginx.conf ( .platform/nginx/nginx.conf)。
注意:您可以拥有自己的文件.platform/nginx/nginx.conf并仍然使用.platform/nginx/conf.d/目录中的文件。
2 解决方案:使用负载均衡器(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;
}
...
}
...
Run Code Online (Sandbox Code Playgroud)
(*) 在您的 EC2 实例安全组中打开端口 22(类似于*AWSEBSecurityGroup*),然后转到:
EC2 > 实例 > 连接 > EC2 实例连接(基于浏览器的 SSH 连接)
我目前的理解是最好的方法是使用服务器级重写来解决该问题。示例(对于 Apache 服务器)如下:
Rewrite Engine On
# Catch requests to domains other than your primary (custom) domain
Rewrite Cond %{HTTP_HOST} !~ appname.tld
# Send those requests to the primary domain
RewriteRule (.*) http://www.appname.tld%{REQUEST_URI} [R=301, L]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2715 次 |
| 最近记录: |