개발/ETC
[nginx] 들어오는 IP를 제한하고 싶을 때
쩨이엠
2024. 3. 6. 15:24
728x90
반응형
보통 테스트를 할 때엔 도메인을 연결해서 기존 도메인은 점검중을 띄우고 QA용의 도메인으로 접속해서 테스트를 진행하는데
이번엔 외부 연동이 많아 도메인을 변경하기에 이슈가 있었다
해서 찾아보니 nginx에서 IP 제한을 할 수 있어서 이번 테스트는 그렇게 진행하였다
기존 nginx-ssl.conf
location / {
add_header Cache-Control "no-store, no-cache, must-revalidate";
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
# Keepalive connection to upstream
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
# proxy_set_header Host $host;
limit_except GET POST HEAD OPTIONS {
deny all;
}
}
프록시 세팅이 되어있다
이 곳에 접속을 허용하지 않은 IP가 들어오면 503을 리턴하게 하고 allowd ip를 세팅해준다 (사실 매우 간단한 편)
// 이 부분 추가
map $remote_addr $allowed_ip {
default 0;
10.202.0.0/16 1;
...
}
location / {
// 이 부분 추가
if ($allowed_ip = 0) {
return 503;
}
add_header Cache-Control "no-store, no-cache, must-revalidate";
...
}
접속하는 ip를 매핑해준 후 매핑한 ip가 아닌 다른 ip로 들어오면 503을 return 해준다
끝!
728x90
반응형