[英]In AWS I need help for deny url by public ip or public dns and to allow only access by domain (CNAME)
我有帶有彈性 ip 的 aws ec2 和帶有我的域的 route53 並且在服務器中有 nginx,這工作正常但是,
我見過其他網站是如何工作的,amazon.com udemy.com。 如果您通過 public ip 或 public dns 直接訪問,則會引發錯誤。 我的問題是如何配置它來做同樣的事情。
例子:
瀏覽器 url 按域:amazon.com = ok
瀏覽器 url 由公共 ip:52.222.137.64 = 400-403 錯誤。
瀏覽器 url 由公共 dns:server-52-222-137-64.ams50.Z4B43B0AEE35624CD95B910189B3DC20-403 錯誤
瀏覽器 url 按域:example.com = ok
瀏覽器 url 由公共 ip:124.34.32.245 = 好的。
瀏覽器 url 由公共 dns:ec2-124.34.32.245.eu-west-3.compute.amazonaws.Z4D236D9A2D102C5ZFE6AD1C50DA4BEC5 確定。
感謝你的幫助。
示例代替我的域,這是我的配置。
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;
}
}
忘了說我也用docker,不知道會不會有關系
試試添加這個
if ($host !~* ^(www.example.com)) {
return 444;
}
這將為沒有您的域名的所有請求提供 444 作為響應。
nginx conf 中配置的服務器名稱是什么?
http://nginx.org/en/docs/http/server_names.html
如果您輸入實際名稱,如果未使用該名稱,您應該能夠使其拒絕請求
在詢問之前我想出了這個解決方案,但由於我很新,我不知道這是否是一個好習慣。 添加到我的配置:
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;
}
}
所以我可以隨心所欲地處理它。
在“AWS 世界”中解決此問題的正確方法是在您的 EC2 實例前面使用具有偵聽器規則的應用程序負載均衡器,並將您的實際服務器放在 Auto Scaling 組中。
這提供了許多其他好處:
請注意,為了使頂級域名正常工作,最好將實際域遷移到 AWS Route53,或者至少將控制權委托給 AWS。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.