๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์„œ๋ฒ„ ํ™˜๊ฒฝ

[CentOS 7] Nginx ์ปดํŒŒ์ผ ์„ค์น˜ ๋ฐ Tomcat ์—ฐ๋™ :: ๋งˆ์ด์ž๋ชฝ

by ๐ŸŒปโ™š 2018. 8. 26.

Nginx๋ž€?

Nginx(์—”์ง„ x๋ผ ์ฝ๋Š”๋‹ค)๋Š” ์›น ์„œ๋ฒ„ ์†Œํ”„ํŠธ์›จ์–ด๋กœ, ๊ฐ€๋ฒผ์›€๊ณผ ๋†’์€ ์„ฑ๋Šฅ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค. ์›น ์„œ๋ฒ„, ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ๋ฐ ๋ฉ”์ผ ํ”„๋ก์‹œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„๋‹ค.


2017๋…„ 10์›” ๊ธฐ์ค€์œผ๋กœ ์‹ค์งˆ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ์›น ์‚ฌ์ดํŠธ(active site)๋“ค์—์„œ ์“ฐ์ด๋Š” ์›น ์„œ๋ฒ„ ์†Œํ”„ํŠธ์›จ์–ด ์ˆœ์œ„๋Š” ์•„ํŒŒ์น˜(44.89%), ์—”์ง„์—‘์Šค(20.65%), ๊ตฌ๊ธ€ ์›น ์„œ๋ฒ„(7.86%), ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ IIS(7.32%)์ˆœ์ด๋‹ค. ์ด ์กฐ์‚ฌ์—์„œ ์ƒ์„ฑ์€ ๋˜์–ด์žˆ์œผ๋‚˜ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ์›น ์‚ฌ์ดํŠธ๋“ค์€ ๋ฐฐ์ œ๋˜์—ˆ์œผ๋ฉฐ ํŠนํžˆ MS์˜ ์ธํ„ฐ๋„ท ์ •๋ณด ์„œ๋น„์Šค(IIS)๋ฅผ ์„ค์น˜ํ•œ ์›น ์‚ฌ์ดํŠธ๋“ค์˜ ์ƒ๋‹น์ˆ˜๊ฐ€ ๋น„ํ™œ์„ฑ ์‚ฌ์ดํŠธ์˜€๋‹ค. ๊ทธ๋Ÿฐ ์‚ฌ์ดํŠธ๋“ค๋„ ํฌํ•จํ•˜๋ฉด MS IIS๊ฐ€ 1์œ„์ด๋‹ค. 2017๋…„ 6์›” ํ˜„์žฌ Nginx๋Š” ํ•œ๊ตญ ์ „์ฒด ๋“ฑ๋ก ๋„๋ฉ”์ธ ์ค‘ 24.73%๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.


Nginx๋Š” ์š”์ฒญ์— ์‘๋‹ตํ•˜๊ธฐ ์œ„ํ•ด ๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ด๊ฒƒ์€ ์•„ํŒŒ์น˜ HTTP ์„œ๋ฒ„์˜ ์Šค๋ ˆ๋“œ/ํ”„๋กœ์„ธ์Šค ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ๊ณผ๋Š” ๋Œ€์กฐ์ ์ด๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋Š” ์„œ๋ฒ„์— ๋งŽ์€ ๋ถ€ํ•˜๊ฐ€ ์ƒ๊ธธ ๊ฒฝ์šฐ์˜ ์„ฑ๋Šฅ์„ ์˜ˆ์ธกํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์ค€๋‹ค.

์ฐธ์กฐ : ์œ„ํ‚ค๋ฐฑ๊ณผ (https://ko.wikipedia.org/wiki/Nginx)





์•„ํŒŒ์น˜ ์›น์„œ๋ฒ„์™€ ๋งŽ์ด ๋น„๊ต๋˜๋Š” Nginx์ž…๋‹ˆ๋‹ค.

Nginx ์‚ฌ์šฉ ๋น„์œจ์ด ์ ์  ๋Š˜์–ด๋‚˜๊ณ  ์žˆ๋Š” ์ด์œ ๋Š” ๊ฐ€๋ณ๊ณ  ์†๋„๊ฐ€ ๋” ๋น ๋ฅธ ์ด์œ ๋„ ์žˆ๋Š”์ง€๋งŒ ๋ณด์•ˆ๋ฉด์—์„œ๋„ ์ด์ ์ด ์žˆ๊ธฐ ๋–„๋ฌธ์ž…๋‹ˆ๋‹ค.



1. Nginx ์„ค์น˜

ํ•„์ž๋Š” Nginx 1.13.8๋ฒ„์ „์„ ์„ค์น˜ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์ปดํŒŒ์ผ ์„ค์น˜๋ฅผ ์œ„ํ•ด tarํŒŒ์ผ์„ ๊ตฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ๋นจ๊ฐ„ ๋ฐ•์Šค ๋‚ด๋ถ€์— ์žˆ๋Š” ๋งํฌ ์•„๋ฌด๊ฑฐ๋‚˜ ์„ ํƒํ•ด์„œ ๋งํฌ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ•œ๋‹ค.

http://nginx.org/en/download.html





๋ณต์‚ฌํ•œ ์ฃผ์†Œ๋งํฌ๋ฅผ ๋ถ™์—ฌ๋„ฃ๊ณ  ์›ํ•˜๋Š” ๋ฒ„์ „์œผ๋กœ ์ˆซ์ž๋งŒ ๋ฐ”๊ฟ”์ค€๋‹ค.

wget๋ช…๋ น์œผ๋กœ ๋ฐ›์•„์ฃผ๊ณ  ํ’€์–ด์ค€๋‹ค.

1
2
3
[jamong@localhost ~]$ cd dev_util/
[jamong@localhost dev_util]$ wget http://nginx.org/download/nginx-1.13.8.tar.gz
[jamong@localhost dev_util]$ tar xvfz nginx-1.13.8.tar.gz
cs





Nginx์„ค์น˜๋ฅผ ์œ„ํ•ด ์„ค์น˜ํ•ด์ค˜์•ผํ•  ํŒจํ‚ค์ง€๋“ค์ด ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ํ•„์ž๋Š” CentOS๋ฅผ ์„ค์น˜ํ• ๋•Œ ์ธํ”„๋ผ ์„ค์น˜๋ฅผ ํ•ด์„œ ์ด๋ฏธ ์„ค์น˜๋˜์–ด์žˆ๋Š” ํŒจํ‚ค์ง€๋“ค์ด ๋งŽ๋‹ค.

์—†๋Š”๊ฒฝ์šฐ ์•„๋ž˜ ํŒจํ‚ค์ง€๋“ค์„ ์„ค์น˜ํ•œ๋‹ค. ์•„๋งˆ devel์ด ๋ถ™์€ ํŒจํ‚ค์ง€๋“ค๋งŒ ์„ค์น˜ํ•ด์ฃผ๋ฉด ๋ ๊ฒƒ์ด๋‹ค.

1
2
3
4
5
6
[jamong@localhost dev_util]$ sudo yum install openssl
[jamong@localhost dev_util]$ sudo yum install openssl-devel
[jamong@localhost dev_util]$ sudo yum install pcre
[jamong@localhost dev_util]$ sudo yum install pcre-devel
[jamong@localhost dev_util]$ sudo yum install zlib
[jamong@localhost dev_util]$ sudo yum install zlib-devel
cs




์ปดํŒŒ์ผ ์„ค์น˜๋ฅผ ํ•˜๊ธฐ์ „์—, configureํŒŒ์ผ์˜ ๊ธฐ๋ณธ ์„ค์น˜๊ฒฝ๋กœ๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค.

๊ทธ๋ƒฅ ์„ค์น˜ํ•ด๋„ ์ƒ๊ด€์—†์ง€๋งŒ, ํ•„์ž๋Š” ๋ชจ๋“  ์„ค์น˜ ํ”„๋กœ๊ทธ๋žจ์„ jamong๊ณ„์ •์˜ dev_util ํด๋”์•ˆ์— ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ๋ฐ”๊พผ๋‹ค.

1
2
3
4
[jamong@localhost dev_util]$ cd nginx-1.13.8/
[jamong@localhost nginx-1.13.8]$ ls
CHANGES  CHANGES.ru  LICENSE  README  auto  conf  configure  contrib  html  man  src
[jamong@localhost nginx-1.13.8]$ vi configure

cs





1
NGX_PREFIX=${NGX_PREFIX:-/usr/local/nginx}
cs

configure ํŒŒ์ผ์˜ ์œ„๋ถ€๋ถ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”์ค€๋‹ค.

1
NGX_PREFIX=${NGX_PREFIX:-/home/jamong/dev_util/nginx}
cs





์ด์ œ ์ปดํŒŒ์ผ ์„ค์น˜๋ฅผ ํ•ด๋ณด์ž.

1
2
3
4
[jamong@localhost nginx-1.13.8]$ ./configure \
> --user=jamong \
> --group=jamong \
> --with-openssl=/usr/bin
cs


error ์—†์ด ๋๋‚œ๋‹ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰์‹œ์ผœ nginx ํด๋”๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.


1
2
[jamong@localhost nginx-1.13.8]$ make
[jamong@localhost nginx-1.13.8]$ make install
cs




Nginx์˜ ๊ธฐ๋ณธ ํฌํŠธ๋Š” 80์ด๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐฉํ™”๋ฒฝ ํฌํŠธ๋ฅผ ๊ฐœ๋ฐฉํ•œ๋‹ค.

1
2
3
4
5
6
[jamong@localhost sbin]$ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[jamong@localhost sbin]$ sudo firewall-cmd --reload
success
[jamong@localhost sbin]$ sudo firewall-cmd --list-ports
22/tcp 3306/tcp 8080/tcp 80/tcp

cs




Nginx๋ฅผ ์‹คํ–‰ํ•ด๋ณด์ž.

1
2
3
[jamong@localhost nginx-1.13.8]$ cd ../nginx/sbin/
[jamong@localhost sbin]$ sudo ./nginx
[jamong@localhost sbin]$
cs




์›น์œผ๋กœ ์„œ๋ฒ„์˜ ip๋งŒ ์น˜๊ณ  ๋“ค์–ด๊ฐ€๋ฉด "welcome to nginx!"์™€ ๊ฐ™์€ ๋ฌธ๊ตฌ๊ฐ€ ๋œจ๋ฉด ์„ฑ๊ณต!

ํ•˜์ง€๋งŒ 403์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.

์„ค์ •ํŒŒ์ผ์—์„œ ์œ ์ €๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋‹ค.

1
2
[jamong@localhost sbin]$ cd /home/jamong/dev_util/nginx/conf/
[jamong@localhost conf]$ vi nginx.conf
cs


์ƒ๋‹จ์˜ user nobody ์ฃผ์„์„ ํ•ด์ œํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•œ๋‹ค.

1
user  jamong;
cs




์ €์žฅํ•˜๊ณ  Nginx๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•œ๋‹ค.

sbin ํด๋”๋กœ ๋“ค์–ด๊ฐ€์„œ ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•ด์ค€๋‹ค.

1
2
3
[jamong@localhost sbin]$ sudo ./nginx -s stop
[jamong@localhost sbin]$ sudo ./nginx
[jamong@localhost sbin]$
cs






๋‹ค์‹œ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„์˜ ip๋งŒ์œผ๋กœ ์ ‘๊ทผ์„ ํ•ด์คฌ์„๋•Œ ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์˜ค๋ฉด ์„ค์น˜ ์„ฑ๊ณต์ด๋‹ค.



2. Nginx Tomcat ์—ฐ๋™

์„ค์น˜๋Š” ๋๋‚ฌ๋‹ค. ์ด์ œ tomcat๊ณผ ์—ฐ๋™์‹œ์ผœ์ค˜์•ผ Nginx๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜๋ฏธ๊ฐ€ ์ƒ๊ธด๋‹ค.

Nginx์˜ ์„ค์ •ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

1
[jamong@localhost conf]$ vi /home/jamong/dev_util/nginx/conf/nginx.conf
cs





html์•ˆ์— tomcat์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 8080์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ํฌํŠธ๋ฅผ ๋งž์ถฐ์„œ ์ˆ˜์ •ํ•ด์ฃผ๊ณ 

 server๋ถ€๋ถ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ค€๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    upstream tomcat {
        ip_hash;
        server 127.0.0.1:8080;
    }
 
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            proxy_set_header    Host $http_host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto $scheme;
            proxy_set_header    X-NginX-Proxy true;
 
            proxy_pass http://tomcat;
            proxy_redirect      off;
            charset utf-8;
         }

cs





๋‹ค์‹œ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„์˜ ip๋งŒ์œผ๋กœ ์ ‘๊ทผ์„ ํ•ด์คฌ์„๋•Œ ์œ„์—์„œ ์ถœ๋ ฅ๋œ nginx welcome์ด ์•„๋‹Œ ํ†ฐ์บฃ ์ธ๋ฑ์Šค๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.




ํ•„์ž์˜ ๊ฒฝํ—˜์ƒ ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๊ณ ๋„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์€ ์‹œ๊ฐ„์„ ํˆฌ์žํ•œ ์ ์ด ์žˆ๋‹ค.

Nginx์—๋Ÿฌ ํŽ˜์ด์ง€๊ฐ€ ๋œจ๋Š” ๊ฒฝ์šฐ, ํ†ฐ์บฃ์ด ์‹คํ–‰๋˜์–ด ์žˆ๋Š”์ง€ ํ•œ๋ฒˆ ํ™•์ธํ•ด๋ณธ๋‹ค.

ํ†ฐ์บฃ์ด ์‹คํ–‰๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ, ์—๋ŸฌํŽ˜์ด์ง€๋กœ ์ด๋™๋œ๋‹ค.







๋Œ“๊ธ€