问题原文
原帖链接 · Del · CC BY-SA 4.0
I am running a django app on AWS elastic beanstalk and I'm getting spammed by bots trying to scan for vulnerabilities. It results in a flood of errors such as:
(Where xx.xxx.xx.xx is my ec2 instance's ip address.)
DisallowedHost at //www/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php
Invalid HTTP_HOST header: 'xx.xxx.xx.xx'. You may need to add 'xx.xxx.xxx.xx' to ALLOWED_HOSTS.
My legitimate users only access the site using the domain name. I've been trying to figure out how to modify my nginx configuration to block all connections that aren't addressed to *.mydomain.com or mydomain.com.
I dynamically add and remove subdomains as needed so I do a wildcard for the subdomain.
AWS Elastic beanstalk generates the following default config file for me:
/etc/nginx/nginx.conf
#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 32788;
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 $
# Include the Elastic Beanstalk generated locations
include conf.d/elasticbeanstalk/*.conf;
}
}
Then I extend it with this file:
.platform\nginx\conf.d\elasticbeanstalk\00_application.conf
location / {
set $redirect 0;
if ($http_x_forwarded_proto != "https") {
set $redirect 1;
}
if ($redirect = 1) {
return 301 https://$host$request_uri;
}
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
client_max_body_size 2000M;
}
location = /health-check.html {
set $redirect 0;
if ($http_x_forwarded_proto != "https") {
set $redirect 1;
}
if ($http_user_agent ~* "ELB-HealthChecker") {
set $redirect 0;
return 204;
}
if ($redirect = 1) {
return 301 https://$host$request_uri;
}
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
The purpose of that override file is so that nginx redirects http to https and responds to ELB health checks.
I'm not overly familiar with nginx or elastic beanstalk, but from what I could gather when researching this problem is that I need to have my default server connection return 444 and then have a separate server block with server_name set to my domain.
Is this the correct way to handle this problem and will it work with wildcard subdomains?
Thank you
已采纳解答
原帖链接 · Tero Kilkanen · CC BY-SA 4.0
It seems like your only virtual host is the one with default_server attribute. It means that if no matching virtual host is found, that block is used to serve the request.
To properly handle your case, you need to have:
serverblock withdefault_serverin thelistendirective. This block should only havereturn 404;orreturn 444;. You might want to turn offaccess_login this block too.serverblock withserver_name example.com *.example.com;. This virtual host should contain your actual application.
Note, this is how things are configured when one has complete control of nginx configuration. I don't know if Elastic Beanstalk has some features to automatically generate configuration files in this fashion.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
