Home Nginx Multi-Domain 설정 및 SSL
Post
Cancel

Nginx Multi-Domain 설정 및 SSL

1. Install Nginx for CentOS 7.x


CentOS 에서 yum 설치를 하려면 Repository를 설정 해야 하나, 여기에서는 rpm 파일을 받아서 설치하는 방법으로 하겠습니다. 그리고 현재 설치는 root로 진행 되고 홈디렉터리는 /root 입니다.

  • nginx의 패키지 저장소는
    1
    
    https://nginx.org/packages/
    
  • 아래와 같이 다양한 OS의 패키지들이 있습니다.
    1
    2
    3
    4
    5
    6
    7
    8
    
    aix/
    alpine/
    centos/
    debian/
    keys/
    rhel/
    sles/
    ubuntu/
    
  • 우리는 CentOS 7.x 을 사용 해야 하므로 아래 URL에서 최신 Package를 받아 오겠습니다.
    1
    
    https://nginx.org/packages/centos/7/x86_64/RPMS/
    
  • 다운로드 받아 rpm 설치 진행 과정
1
2
3
4
5
6
7
8
9
10
11
# pwd
/root
# mkdir tmp
# pwd
/root/tmp
# curl http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm -o nginx-1.20.2-1.el7.ngx.x86_64.rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  790k  100  790k    0     0   347k      0  0:00:02  0:00:02 --:--:--  347k

# rpm -Uvh nginx-1.20.2-1.el7.ngx.x86_64.rpm
  • 패키지 설치 디렉터리
    1
    2
    3
    
    /usr/sbin/nginx
    /usr/share/nginx/html
    /etc/nginx/
    
1
2
3
4
# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

# systemctl start nginx

2. 두개의 도메인을 Reverse-Proxy 설정으로 한대의 서버에서 운영 방법

host1.test.com, host2.test.com 두개의 도메인을 설정하도록 하겠습니다.

host1_test_com.conf 파일 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server {
        listen 80;
        listen [::]:80;
        root /usr/share/nginx/html;

        server_name host1.test.com;
        return 308 https://host1.test.com$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name host1.test.com;

        ssl_certificate /etc/nginx/ssl/host1/server.crt;
        ssl_certificate_key /etc/nginx/ssl/host1/server.key;

        location / {
                proxy_pass http://10.200.101.185:8080;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_redirect off;
        }
}

host2_test_com.conf 파일 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server {
        listen 80;
        listen [::]:80;
        root /usr/share/nginx/html;

        server_name host2.test.com;
        return 308 https://host2.test.com$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name host2.test.com;

        ssl_certificate /etc/nginx/ssl/host2/server.crt;
        ssl_certificate_key /etc/nginx/ssl/host2/server.key;

        location / {
                proxy_pass http://10.200.101.186:8080;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_redirect off;
        }
}

설정 파일 적용

1
2
3
4
5
# cp host1_test_com.conf /etc/nginx/conf.d/
# cp host2_test_com.conf /etc/nginx/conf.d/
# cd /etc/nginx/conf.d/
# mv default.conf default.conf-org
# systemctl restart nginx

server - https redirect

1
2
3
4
5
server {
    listen 80;
    server_name host1.test.com;
    return 308 https://host1.test.com$request_uri;
  }

server 명령어를 통해 http 요청을 https 로 redirecting 시키는 용도로 사용했습니다. return 통해 HTTP Status Code(308)와 URL(https://host1.test.com$request_uri) 넘겨주는데 상태 코드는 308로 할 것을 추천드립니다.

HTTP Status Code 301, 302, 303, 307, 308 등 3xx 상태는 url redirect 용도로 사용하는데 차이점은 307, 308 은 HTTP Method를 유지를 해줍니다. 301, 302, 303 등은 항상 redirect시 HTTP Method를 GET으로 변경하여 전송합니다.

This post is licensed under CC BY 4.0 by the author.