Nginx 보안 강화하기
1.도메인이 아닌 IP로 들어오는 요청 막기
server {
listen 80 default_server;
return 444;
}
server {
listen 443 default_server ssl;
ssl_certificate /etc/letsencrypt/live/[domain]/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/[domain]/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 444;
}
conf.d안에 Security.conf라는 파일을 만들어 위의 내용을 입력하고 nginx를 reload해줍니다.
2. 특정 IP 제한
1. Nginx 로그 확인
Nginx 로그를 보면 다양한 해외 IP에서 취약점 공격을 해오는 것을 볼 수 있다.
2. IP 차단
deny 152.42.193.70;
deny [ip];
Nginx의 conf.d 디렉토리 안에 ip-ban.conf를 만들어 차단하고 싶은 IP를 차단합니다.
3.차단 확인
2번의 파일에 본인의 IP를 넣어 접속시 403 Forbidden이 뜨는지 확인 할 수 있습니다.
3. 해외 IP차단하기
1. 한국 IP 다운하기
https://www.ip2location.com/free/visitor-blocker
Block Visitors by Country | IP2Location
IP Address Geolocation to Country, City, Region, Latitude, Longitude, ZIP Code, ISP, Domain, Time Zone, Area Code, Mobile Data, Usage Type, Elevation and so on.
www.ip2location.com
위 사이트에 들어가 Country를 한국으로 output Format을 Nginx allow로 변경하고 다운받아줍니다.
2.
(1) 변경전
# -------------------------------------------------------
# Free IP2Location Firewall List by Country
# Source: https://www.ip2location.com/free/visitor-blocker
# Last Generated: 06 Apr 2024 11:45:18 GMT
# [Important] Please update this list every month
# -------------------------------------------------------
location / {
allow 1.11.0.0/16;
allow 1.16.0.0/18;
...
deny all;
}
(2) 변경후
# -------------------------------------------------------
# Free IP2Location Firewall List by Country
# Source: https://www.ip2location.com/free/visitor-blocker
# Last Generated: 06 Apr 2024 11:45:18 GMT
# [Important] Please update this list every month
# -------------------------------------------------------
allow 1.11.0.0/16;
allow 1.16.0.0/18;
...
deny all;
위의 파일을 다운받아 txt 파일을 conf.d 파일로 바꾸고 위의 특정 IP 제한차단 방법처럼 location을 지우고 Nginx의 conf.d 디렉토리에 넣으면 해외 IP차단이 완료됩니다. IP는 유동적이므로 1달마다 업데이트하라고 권고되어있습니다.
3.확인
vpn 사이트에 들어가 도메인을 입력하면 403 Forbidden이 뜨는것을 확인 할 수 있습니다.