简体   繁体   English

在 AWS 中,我需要帮助以通过公共 ip 或公共 dns 拒绝 url 并仅允许通过域(CNAME)访问

[英]In AWS I need help for deny url by public ip or public dns and to allow only access by domain (CNAME)

I have aws ec2 with elastic ip and route53 with my domain and have nginx in server, this works fine but,我有带有弹性 ip 的 aws ec2 和带有我的域的 route53 并且在服务器中有 nginx,这工作正常但是,

i have seen how other websites work, amazon.com udemy.com.我见过其他网站是如何工作的,amazon.com udemy.com。 If you access directly by public ip or public dns you throw an error.如果您通过 public ip 或 public dns 直接访问,则会引发错误。 My question is how can I configure it to do the same.我的问题是如何配置它来做同样的事情。

example:例子:
browser url by domain: amazon.com = ok浏览器 url 按域:amazon.com = ok
browser url by public ip: 52.222.137.64 = 400-403 error.浏览器 url 由公共 ip:52.222.137.64 = 400-403 错误。
browser url by public dns: server-52-222-137-64.ams50.r.cloudfront.net = 400-403 error.浏览器 url 由公共 dns:server-52-222-137-64.ams50.Z4B43B0AEE35624CD95B910189B3DC20-403 错误

browser url by domain: example.com = ok浏览器 url 按域:example.com = ok
browser url by public ip: 124.34.32.245 = ok.浏览器 url 由公共 ip:124.34.32.245 = 好的。
browser url by public dns: ec2-124.34.32.245.eu-west-3.compute.amazonaws.com = ok.浏览器 url 由公共 dns:ec2-124.34.32.245.eu-west-3.compute.amazonaws.Z4D236D9A2D102C5ZFE6AD1C50DA4BEC5 确定。

Thanks all for your help.感谢你的帮助。

example is substitute for my domain, this is my config.示例代替我的域,这是我的配置。

server {
    listen                  8089 ssl http2;
    listen                  [::]:8089 ssl http2;
    server_name             example.com;
    root                    /var/www/example.com/public;

    # SSL
    ssl_certificate         /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/example.com/privkey.pem;
    #ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    ssl_dhparam             /etc/letsencrypt/ssl-dhparams.pem;

    # security
    include                 nginxconfig.io/security.conf;

    location / {
       proxy_set_header Accept-Encoding "";
        try_files $uri $uri/ /index.html;
    }

    # additional config
    include                 nginxconfig.io/general.conf;


}

server {
    listen      8080;
    listen      [::]:8080;
    server_name example.com;
    include     nginxconfig.io/letsencrypt.conf;

    location / {
       return 301 https://example.com$request_uri;
    }
}

I forgot to mention that I also use docker, I don't know if that will have something to do with it忘了说我也用docker,不知道会不会有关系

Try add this试试添加这个

if ($host !~* ^(www.example.com)) {
            return 444;

}

This will give 444 as response to all the request without your domain name.这将为没有您的域名的所有请求提供 444 作为响应。

what is the server name that is configured in your nginx conf? nginx conf 中配置的服务器名称是什么?

http://nginx.org/en/docs/http/server_names.html http://nginx.org/en/docs/http/server_names.html

If you put the actual name, you should be able to make it reject the request if the name isn't used如果您输入实际名称,如果未使用该名称,您应该能够使其拒绝请求

I came up with this solution before asking, but since I'm quite new, I don't know if it would be a good practice.在询问之前我想出了这个解决方案,但由于我很新,我不知道这是否是一个好习惯。 Adding to my config:添加到我的配置:

server {
   listen       8089;
   listen       [::]:8089;
  server_name my_public_ip or my public_dns => x.x.x.x;    

   location / {
        return 403;
   }
error_page 403 /403.html;
    error_page   501 =500  /50x.html;
    error_page   500 502 503 504  /50x.html;

  location /403.html {
    root      /usr/share/nginx/html;
   # allow all;
  }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

and so I can handle it as I want.所以我可以随心所欲地处理它。

Correct way to solve this problem in "AWS world" would be to use Application Load balancer with listener rules in front of your EC2 instance and place your actual server in Auto Scaling Group.在“AWS 世界”中解决此问题的正确方法是在您的 EC2 实例前面使用具有侦听器规则的应用程序负载均衡器,并将您的实际服务器放在 Auto Scaling 组中。

This provides a lot of other benefits:这提供了许多其他好处:

  • AWS SLA does not work if your workload is not able to load balance between at least 2 availability zone如果您的工作负载无法在至少 2 个可用区之间进行负载平衡,AWS SLA 将不起作用
  • it is simple to add AWS generator TLS certificate (extra bonus: it will auto re-new)添加 AWS 生成器 TLS 证书很简单(额外奖励:它将自动更新)
  • build in certain amount of DDoS protection内置一定数量的 DDoS 保护
  • auto scaling自动缩放
  • instance refresh实例刷新
  • failover故障转移

please note that in order for apex domain name to work, it would be best to migrate the actual domain to AWS Route53, or at least delegate controls to AWS.请注意,为了使顶级域名正常工作,最好将实际域迁移到 AWS Route53,或者至少将控制权委托给 AWS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM