[Study] Sub Domain, Redirection ์ ์šฉ

nginx์—์„œ Sub Domain, Redirection ์ ์šฉ

ํ•ต์‹ฌ ๋‚ด์šฉ

  • nginx ์„ค์ •์œผ๋กœ Dev, Prod๋กœ ์‹œ์ž‘ํ•˜๋Š” ์„œ๋ธŒ ๋„๋ฉ”์ธ์„ ๋งŒ๋“ ๋‹ค.
  • nginx ์„ค์ •์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ URL์— IP ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ๋„๋ฉ”์ธ ์ฃผ์†Œ๋กœ ๋ฐ”๊ฟ”์ฃผ๋„๋ก ๋ฆฌ๋‹ค์ด๋ ‰์…˜์„ ์ ์šฉํ•ด๋ณด์ž.

Sub Domain ๊ตฌํ˜„

๊ฐ€๋น„์•„ ์„ค์ •

๋„๋ฉ”์ธ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋น„์•„์—์„œ ๊ตฌ์ž…ํ–ˆ์œผ๋ฏ€๋กœ, ๊ฐ€๋น„์•„๋กœ ์ด๋™ํ•œ๋‹ค.
๊ฐ€๋น„์•„๋กœ ์ด๋™ ํ›„ ๋กœ๊ทธ์ธ์„ ํ•˜๋ฉด, ํŽ˜์ด์ง€ ํ•˜๋‹จ์— ๊ตฌ์ž…ํ–ˆ๋˜ ๋„๋ฉ”์ธ ๋ชฉ๋ก์ด ๋ณด์ด๊ณ , ์—ฌ๊ธฐ์„œ ์„œ๋ธŒ ๋„๋ฉ”์ธ์„ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ๋„๋ฉ”์ธ์˜ ๊ด€๋ฆฌํˆด ํด๋ฆญ ํ›„ DNS ๋ ˆ์ฝ”๋“œ ์„ค์ • ์˜†์— ์„ค์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค.
DNS ์„ค์ •์—๋„ ๋‚ด๊ฐ€ ๊ตฌ์ž…ํ•œ ๋„๋ฉ”์ธ ๋ชฉ๋ก์ด ๋ณด์ด๊ณ  ํ•ด๋‹น ๋„๋ฉ”์ธ์˜ ์„ค์ • ๋ฒ„ํŠผ์„ ํด๋ฆญ ํ›„ ๋ ˆ์ฝ”๋“œ ์ˆ˜์ • ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค€๋‹ค.

์ตœ์ดˆ IP ์ฃผ์†Œ์— ๋Œ€ํ•œ ๋„๋ฉ”์ธ ๋“ฑ๋ก ํ›„์— ๋‹ค๋ฅธ ์„ค์ •์„ ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ํƒ€์ž… A๋งŒ ๋“ฑ๋ก๋ผ์žˆ์„ ๊ฒƒ์ด๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ค์ •์„ ํ•ด์ค€๋‹ค.

1

์ตœ์ดˆ๋กœ ๋“ฑ๋กํ–ˆ๋˜ A ํƒ€์ž…์˜ ๊ฐ’์—๋Š” ์„œ๋ฒ„์˜ IP ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ–ˆ์—ˆ์ง€๋งŒ, ์ถ”๊ฐ€ํ•œ CNAME์—์„œ๋Š” ๋„๋ฉ”์ธ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ๊ณ , ํ˜ธ์ŠคํŠธ์— ์„œ๋ธŒ ๋„๋ฉ”์ธ์œผ๋กœ ์„ค์ •ํ•  ์•ž ์ฃผ์†Œ๋ฅผ ๊ธฐ์ž…ํ•ด ์ค€๋‹ค.

์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด nginx๋กœ ์ ‘์†ํ•ด ์ค€๋‹ค.

nginx ์„ค์ •

๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„๋กœ ์ ‘์†ํ•ด ์ค€ ํ›„ nginx์˜ ์„ค์ • ํŒŒ์ผ์— ์„œ๋ธŒ ๋„๋ฉ”์ธ ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

$ sudo vim /etc/nginx/sites-available/default

์„ค์ • ํŒŒ์ผ์„ ํŽธ์ง‘๊ธฐ๋กœ ์—ฐ ๋’ค ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„œ๋ธŒ ๋„๋ฉ”์ธ ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์ž.

server {

        root /var/www/html/Dev;

        index phpinfo.php index.html index.htm index.nginx-debian.html;
    	    server_name Dev.vividstudy.shop;


        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        		   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
}

server {

        root /var/www/html/Prod;

        index phpinfo.php index.html index.htm index.nginx-debian.html;
    	    server_name Prod.vividstudy.shop;


        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        		   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
}

vividstudy.shop ์ด๋ผ๋Š” ๋„๋ฉ”์ธ ๊ธฐ์ค€์œผ๋กœ Dev, Prod ์„œ๋ธŒ ๋„๋ฉ”์ธ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๋Š” ์„ค์ •์ด๋‹ค.
์ตœ์ดˆ์˜ ํŽ˜์ด์ง€๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ /var/www/html/Prod, /var/www/html/Dev ๊ฒฝ๋กœ์— ๊ฐ๊ฐ phpinfo.php๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ค€๋‹ค.
phpinfo.php๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

// Prod Sub Domain Index Page
<?php echo "Prod Page!!"; ?>


// Dev Sub Domain Index Page
<?php echo "Dev Page!!"; ?>

sub ๋„๋ฉ”์ธ ์ ‘์†

2

3

๊ฐ๊ฐ์˜ ์ฃผ์†Œ์— ์ž˜ ์ ‘์†๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Redirection(IP to Domain) ์ ์šฉ

nginx ์„ค์ •

๋ฆฌ๋‹ค์ด๋ ‰์…˜์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์œ„์—์„œ ํŽธ์ง‘๊ธฐ๋กœ ์—ด์—ˆ๋˜ nginx ์„ค์ • ํŒŒ์ผ์„ ๋‹ค์‹œ ์—ด์–ด์ฃผ์ž.

$ sudo vim /etc/nginx/sites-available/default

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

4

server_name ๋’ค์—” IP์ฃผ์†Œ;๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์ž.
IP ์ฃผ์†Œ์˜ 80๋ฒˆ ํฌํŠธ๋กœ ์ ‘์†ํ•˜๋ฉด https๊ฐ€ ๋ถ™์€ ๋„๋ฉ”์ธ ์ฃผ์†Œ๋กœ ๋ณด๋‚ด๋‹ฌ๋ผ๋Š” ์„ค์ •์ด๋‹ค.

301 : 301์€ URL Redirection ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ์ด๋‹ค.

ํ™•์ธ

IP ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•ด๋ณด์ž.

5

IP ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด https ํ”„๋กœํ† ์ฝœ์˜ ๋„๋ฉ”์ธ ์ฃผ์†Œ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.