Nginx는 서브 도메인을 설정하여 도메인 입력시 해당 주소로 redirect 시켜주는 기능이 존재한다.
예를 들어 jenkins port가 8080이라고 하자.
만약 서브 도메인이 없다면 입력시마다 ${protocol}:${your_domain}:8080으로 접속을 해야한다.
각 페이지마다 포트를 기억하지 않는다면 상당히 비효율적이다.
하지만 Nginx에 서브도메인명으로 설정파일을 생성해주면 ${protocol}:${sub_domain}:${your_domain} 으로 접속이 가능하다.
나의 경우 jenkins를 서브도메인명으로 생성해보겠다.
우선 "/etc/nginx/sites-available"에 서브 도메인 설정을 해준다.
cd /etc/nginx/sites-available
# 나의 경우 jenkins
vi jenkins
# 서브도메인 추가
server {
server_name ${sub domain} www.${sub domain};
access_log /var/log/nginx/jenkins/access.log;
error_log /var/log/nginx/jenkins/error.log;
location / { # location 블록
include /etc/nginx/proxy_params;
proxy_pass ${real ip}:${real port};
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/${sub domain}/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/${sub 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
}
server {
if ($host = ${sub domain}) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name ${sub domain} www.${sub domain};
return 404; # managed by Certbot
}
해당 설정은 다음과 같다.
- sub domain 으로 http 요청이 오면 https로 이동한다.
- https + sub domain으로 요청이 오면 proxy를 적용한다.
- proxy에 설정한 real 주소로 넘김
- 실제 해당 주소로 요청
해당 설정후 sub domain으로 접속시 설정한 포트로 접속을 하게 된다.
이제 https://mydomain:8080이 아니라 https://jenkins.mydomain만 입력해도 젠킨스 화면으로 연결이 된다.
다음과 같이 jenkins 연결이 정상적으로 되고 있음을 확인할 수 있다.
'Infra' 카테고리의 다른 글
NAT(Network Address Translation)란 ? (0) | 2024.06.23 |
---|---|
TURN 서버 (0) | 2024.06.23 |
[리눅스]hosts 경로 및 hosts등록 이유 (0) | 2024.05.19 |
AWS - 로드밸런서와 타겟그룹 (0) | 2024.05.12 |
[Infra]오토스케일링과 로드밸런싱의 차이점 (0) | 2024.04.28 |