웹사이트 검색

CentOS 8에 Apache/Nginx용 Varnish Cache 6 설치


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • 센트OS 8
  • 센트OS 7

이 페이지에서

  1. 전제 조건
  2. 시작하기\n
  3. 니스 설치
  4. 니스 구성
  5. Varnish와 함께 작동하도록 Nginx 설치 및 구성\n
  6. Varnish와 함께 작동하도록 Apache 설치 및 구성\n
  7. 바니시 명령줄 작업
  8. 결론

Varnish Cache는 오픈 소스 소프트웨어로 제공되는 웹 애플리케이션의 속도를 높이는 데 사용되는 강력한 역방향 HTTP 프록시입니다. Varnish는 정적 및 동적 콘텐츠를 모두 캐시합니다. 웹 서버 백엔드에 도달하기 전에 모든 인바운드 요청을 처리합니다. 웹 브라우저와 Apache 또는 Nginx 웹 서버 사이에 위치합니다. Varnish 캐시는 들어오는 모든 페이지 요청을 메모리에 저장하므로 웹 서버는 동일한 웹 페이지를 반복해서 생성할 필요가 없습니다.

이 게시물에서는 CentOS 8에서 Apache 및 Nginx로 Varnish 캐시를 설치하는 방법을 보여줍니다.

전제 조건

  • CentOS 8을 실행하는 서버.\n
  • 서버에 루트 암호가 구성되어 있습니다.\n

시작하기

시작하기 전에 시스템 패키지를 최신 버전으로 업데이트하는 것이 좋습니다. 다음 명령을 실행하여 업데이트할 수 있습니다.

dnf update -y

모든 패키지가 업데이트되면 다음 단계로 진행할 수 있습니다.

바니시 설치

기본적으로 Varnish 패키지는 CentOS 8 기본 리포지토리에 포함되어 있습니다. 다음 명령을 실행하여 설치할 수 있습니다.

dnf install varnish -y

Varnish를 설치한 후 Varnish 기본 구성 파일을 편집하고 Nginx 또는 Apache 웹 서버 지원 주소를 정의합니다.

nano /etc/varnish/default.vcl

다음 줄을 변경합니다.

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

완료되면 파일을 저장하고 닫습니다.

바니시 구성

기본적으로 Varnish는 포트 6081에서 수신합니다. 따라서 포트 80에서 수신하도록 Varnish를 구성해야 합니다. varnish.service 파일을 편집하여 구성할 수 있습니다.

nano /lib/systemd/system/varnish.service

다음 줄을 찾으십시오.

ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

다음 줄로 교체했습니다.

ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

파일을 저장하고 닫은 다음 systemd 데몬을 다시 로드하여 변경 사항을 적용합니다.

systemctl daemon-reload

그런 다음 Varnish 서비스를 시작하고 다음 명령을 사용하여 시스템 재부팅 시 시작되도록 활성화합니다.

systemctl start varnish
systemctl enable varnish

다음 명령을 사용하여 광택제 상태를 확인할 수도 있습니다.

systemctl status varnish

다음과 같은 결과가 표시되어야 합니다.

? varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-03-12 04:17:32 EST; 4s ago
  Process: 6391 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 6392 (varnishd)
    Tasks: 217
   Memory: 99.5M
   CGroup: /system.slice/varnish.service
           ??6392 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
           ??6402 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

Mar 12 04:17:32 masternode systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Mar 12 04:17:32 masternode varnishd[6391]: Debug: Version: varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7
Mar 12 04:17:32 masternode varnishd[6391]: Debug: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Mar 12 04:17:32 masternode varnishd[6392]: Version: varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7
Mar 12 04:17:32 masternode varnishd[6392]: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Mar 12 04:17:32 masternode varnishd[6391]: Debug: Child (6402) Started
Mar 12 04:17:32 masternode varnishd[6392]: Child (6402) Started
Mar 12 04:17:32 masternode varnishd[6392]: Child (6402) said Child starts
Mar 12 04:17:32 masternode systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

이 시점에서 Varnish가 시작되고 포트 80에서 수신 대기합니다. 다음 명령으로 확인할 수 있습니다.

ss -antpl | grep varnishd

다음과 같은 결과가 표시되어야 합니다.

LISTEN    0         10               127.0.0.1:34977            0.0.0.0:*        users:(("varnishd",pid=1532,fd=11))                                            
LISTEN    0         128                0.0.0.0:80               0.0.0.0:*        users:(("cache-main",pid=1542,fd=6),("varnishd",pid=1532,fd=6))                
LISTEN    0         128                   [::]:80                  [::]:*        users:(("cache-main",pid=1542,fd=7),("varnishd",pid=1532,fd=7))                
LISTEN    0         10                   [::1]:34909               [::]:*        users:(("varnishd",pid=1532,fd=10))                                            

이 시점에서 Varnish가 설치되고 구성됩니다. 이제 다음 단계를 진행할 수 있습니다.

Varnish와 함께 작동하도록 Nginx 설치 및 구성

먼저 서버에 Nginx 패키지를 설치해야 합니다. 다음 명령으로 설치할 수 있습니다.

dnf install nginx -y

설치가 완료되면 Nginx 수신 포트를 80에서 8080으로 변경해야 합니다. Nginx 기본 구성 파일을 편집하여 변경할 수 있습니다.

nano /etc/nginx/nginx.conf

다음 줄을 변경합니다.

        listen       8080 default_server;
        listen       [::]:8080 default_server;

파일을 저장하고 닫은 후 다음 명령을 사용하여 구문 오류가 있는지 Nginx를 확인합니다.

nginx -t

다음과 같은 결과가 표시되어야 합니다.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

다음으로 Nginx 서비스를 시작하고 시스템 재부팅 시 시작되도록 활성화합니다.

systemctl start nginx
systemctl enable nginx

다음 명령을 사용하여 Nginx 수신 포트를 확인할 수도 있습니다.

ss -antpl | grep 8080

다음과 같은 결과가 표시되어야 합니다.

LISTEN    0         128                0.0.0.0:8080             0.0.0.0:*        users:(("nginx",pid=5569,fd=8),("nginx",pid=5568,fd=8))                        
LISTEN    0         128                   [::]:8080                [::]:*        users:(("nginx",pid=5569,fd=9),("nginx",pid=5568,fd=9))                        

이 시점에서 Varnish 캐시가 설치되고 포트 80에서 들어오는 모든 요청을 포트 8080에서 수신 대기 중인 Nginx 웹 서버로 전달하도록 구성됩니다.

다음 명령을 사용하여 Varnish 캐시가 작동하는지 여부를 확인할 수 있습니다.

curl -I http://localhost

다음과 같은 결과가 표시되어야 합니다.

HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Fri, 12 Mar 2021 09:18:32 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
ETag: "5d9bab28-fd9"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

Varnish와 함께 작동하도록 Apache 설치 및 구성

먼저 시스템에 Apache 웹 서버 패키지를 설치해야 합니다. 다음 명령으로 설치할 수 있습니다.

dnf install httpd -y

Apache를 설치한 후 Apache 기본 구성 파일을 편집하고 수신 포트를 80에서 8080으로 변경합니다.

nano /etc/httpd/conf/httpd.conf

다음 줄을 변경합니다.

Listen 8080

파일을 저장하고 닫은 다음 Apache 서비스를 시작하고 다음 명령을 사용하여 시스템 재부팅 시 시작되도록 활성화합니다.

systemctl start httpd
systemctl enable httpd

다음 명령을 사용하여 Apache 수신 포트를 확인할 수도 있습니다.

ss -antpl | grep httpd

다음과 같은 결과가 표시되어야 합니다.

LISTEN    0         128                      *:8080                   *:*        users:(("httpd",pid=7556,fd=4),("httpd",pid=7555,fd=4),("httpd",pid=7554,fd=4),("httpd",pid=7552,fd=4))

이 시점에서 Varnish 캐시가 설치되고 포트 80에서 들어오는 모든 요청을 포트 8080에서 수신 대기 중인 Apache 웹 서버로 전달하도록 구성됩니다.

다음 명령을 사용하여 Varnish 캐시가 작동하는지 여부를 확인할 수 있습니다.

curl -I http://localhost

다음과 같은 결과가 표시되어야 합니다.

Date: Fri, 12 Mar 2021 09:21:14 GMT
Server: Apache/2.4.37 (centos)
Content-Location: index.html.zh-CN
Vary: negotiate,accept-language
TCN: choice
Last-Modified: Fri, 14 Jun 2019 03:37:43 GMT
ETag: "fa6-58b405e7d6fc0;5bd5365602e05"
Accept-Ranges: bytes
Content-Length: 4006
Content-Type: text/html; charset=UTF-8
Content-Language: zh-cn
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Connection: keep-alive

광택 명령줄 작업

Varnish는 Varnish를 모니터링하고 제어하는 데 도움이 되는 몇 가지 유용한 유틸리티와 함께 제공됩니다.

varnishadm은 구성된 백엔드를 나열하는 데 사용되는 명령줄 유틸리티입니다.

varnishadm

다음 출력이 표시되어야 합니다.

200        
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7

Type 'help' for command list.
Type 'quit' to close CLI session.

이제 다음 명령을 실행하여 모든 백엔드를 나열합니다.

varnish> backend.list

다음 출력이 표시되어야 합니다.

200        
Backend name                   Admin      Probe                Last updated
boot.default                   probe      Healthy (no probe)   Sat, 13 Mar 2021 03:00:02 GMT

바니시로그

varnishlog 유틸리티는 모든 사용자 요청의 로그를 표시하는 데 사용됩니다.

varnishlog

다음과 같은 결과가 표시되어야 합니다.

*   << BeReq    >> 6         
-   Begin          bereq 5 fetch
-   VCL_use        boot
-   Timestamp      Start: 1615604631.507187 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /
-   BereqProtocol  HTTP/1.1
-   BereqHeader    Host: 69.87.218.214
-   BereqHeader    Upgrade-Insecure-Requests: 1
-   BereqHeader    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
-   BereqHeader    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
-   BereqHeader    Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
-   BereqHeader    X-Forwarded-For: 106.213.193.241
-   BereqHeader    Accept-Encoding: gzip
-   BereqHeader    X-Varnish: 6
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   BackendOpen    31 boot.default 127.0.0.1 8080 127.0.0.1 58860
-   BackendStart   127.0.0.1 8080
-   Timestamp      Bereq: 1615604631.507458 0.000270 0.000270
-   Timestamp      Beresp: 1615604631.507844 0.000657 0.000386
-   BerespProtocol HTTP/1.1
-   BerespStatus   200
-   BerespReason   OK
-   BerespHeader   Server: nginx/1.14.1
-   BerespHeader   Date: Sat, 13 Mar 2021 03:03:51 GMT
-   BerespHeader   Content-Type: text/html
-   BerespHeader   Content-Length: 4057
-   BerespHeader   Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT

바니시스타트

varnishstat 명령은 메모리 내 통계, 스토리지, 생성된 스레드 및 삭제된 개체에 대한 정보를 제공합니다.

varnishstat

다음 화면이 표시됩니다.

바니시탑

varnishtop 명령은 varnishd 공유 메모리 로그를 읽고 가장 일반적으로 발생하는 로그 항목의 지속적으로 업데이트된 목록을 제공합니다.

varnishtop

다음 화면이 표시됩니다.

광택제

Varnishhist는 니스 로그를 구문 분석하고 처리하여 마지막 n 요청을 인쇄합니다.

varnishhist

다음 화면이 표시됩니다.

결론

위 가이드에서는 Apache 및 Nginx 웹 서버로 Varnish 캐시를 설치하고 구성하는 방법을 배웠습니다. 이제 프로덕션 환경에서 Varnish 캐시를 구현하고 웹 사이트 성능을 높일 수 있습니다. 궁금한 점이 있으면 언제든지 문의해 주세요.