Calrip 개발 블로그

Centos7 Proxy 서버 구축 (Part.1 기본 설정) 본문

Server

Centos7 Proxy 서버 구축 (Part.1 기본 설정)

칼립 2020. 4. 9. 17:01

1. Proxy의 개념

프록시 서버(Proxy Server)는 두 개의 엔드포인트 장치 사이에서 중간 장치 역할을 하는 서버입니다. 클라이언트에서 서버로 파일 또는 웹 페이지 리소스를 요청하면 이 요청은 프록시 서버로 먼저 전송됩니다. 그런 다음 프록시 서버는 요청을 대상 서버로 보내고 서버가 보낸 리소스를 받습니다. 프록시 서버가 리소스를 가져오면 리소스를 클라이언트로 다시 전달 합니다.

 

프록시 서버를 이용하면 리소스를 캐시 할 수 있습니다. 프록시 서버에서 웹 사이트에 자주 액세스하는 경우 프록시 서버가 캐시에 웹 사이트의 내용을 가지고있기 때문에 더 나은 웹 페이지 접근을 제공 할 수 있습니다. 프록시 서버는 보다 나은 보안, 관리 제어 및 캐싱 서비스를 사용할 수 있도록 제공합니다.

 

2. 개발 환경

CentOS Linux release 7.2.1511

Squid 3.5.20

 

3. Squid 설치

Squid 패키지를 설치하기 전에 시스템 및 패키지를 업데이트하는 것이 좋습니다.

이미 업데이트가 되어있으면 이 과정을 생략하셔도 됩니다.

yum -y update

 

다음 명령어를 사용하여 Squid 패키지를 설치합니다.

yum -y install squid

Squid를 설치하고 다음 명령어를 사용하여 Squid 서비스를 실행할 수 있습니다.

systemctl start squid

서버 부팅 시 Squid를 자동으로 시작하려면 다음 명령어를 사용합니다.

systemctl enable squid

Squid 서비스 상태는 다음 명령어를 사용하여 확인할 수 있습니다.

systemctl status squid

Squid 서비스가 정상적으로 실행이 되면 아래와 유사하게 출력이 됩니다.

● squid.service - Squid caching proxy
   Loaded: loaded (/usr/lib/systemd/system/squid.service; disabled; vendor preset: disabled)
   Active: active (running) since ********
  Process: 12656 ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF (code=exited, status=0/SUCCESS)
  Process: 12650 ExecStartPre=/usr/libexec/squid/cache_swap.sh (code=exited, status=0/SUCCESS)
 Main PID: 12659 (squid)
   CGroup: /system.slice/squid.service
           ├─12659 /usr/sbin/squid -f /etc/squid/squid.conf
           ├─12661 (squid-1) -f /etc/squid/squid.conf
           └─12662 (logfile-daemon) /var/log/squid/access.log

 

3. Squid 설정

Squid 서비스를 설정하기 위해서는 /etc/squid/squid.conf 파일을 편집하여서 설정할 수 있습니다.

다음 명령어를 사용하여 Squid 설정 파일을 편집할 수 있습니다.

vi /etc/squid/squid.conf

vi 편집기에서 줄번호를 표시하려면 다음 명령어를 사용하여 줄번호를 표시할 수 있습니다.

:set number

최초 Squid 설정 파일 구성은 아래와 같습니다.

      1 #
      2 # Recommended minimum configuration:
      3 #
      4
      5 # Example rule allowing access from your local networks.
      6 # Adapt to list your (internal) IP networks from where browsing
      7 # should be allowed
      8 acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
      9 acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
     10 acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
     11 acl localnet src fc00::/7       # RFC 4193 local private network range
     12 acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
     13
     14 acl SSL_ports port 443
     15 acl Safe_ports port 80          # http
     16 acl Safe_ports port 21          # ftp
     17 acl Safe_ports port 443         # https
     18 acl Safe_ports port 70          # gopher
     19 acl Safe_ports port 210         # wais
     20 acl Safe_ports port 1025-65535  # unregistered ports
     21 acl Safe_ports port 280         # http-mgmt
     22 acl Safe_ports port 488         # gss-http
     23 acl Safe_ports port 591         # filemaker
     24 acl Safe_ports port 777         # multiling http
     25 acl CONNECT method CONNECT
     26
     27 #
     28 # Recommended minimum Access Permission configuration:
     29 #
     30 # Deny requests to certain unsafe ports
     31 http_access deny !Safe_ports
     32
     33 # Deny CONNECT to other than secure SSL ports
     34 http_access deny CONNECT !SSL_ports
     35
     36 # Only allow cachemgr access from localhost
     37 http_access allow localhost manager
     38 http_access deny manager
     39
     40 # We strongly recommend the following be uncommented to protect innocent
     41 # web applications running on the proxy server who think the only
     42 # one who can access services on "localhost" is a local user
     43 #http_access deny to_localhost
     44
     45 #
     46 # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
     47 #
     48
     49 # Example rule allowing access from your local networks.
     50 # Adapt localnet in the ACL section to list your (internal) IP networks
     51 # from where browsing should be allowed
     52 http_access allow localnet
     53 http_access allow localhost
     54
     55 # And finally deny all other access to this proxy
     56 http_access deny all
     57
     58 # Squid normally listens to port 3128
     59 http_port 3128
     60
     61 # Uncomment and adjust the following to add a disk cache directory.
     62 #cache_dir ufs /var/spool/squid 100 16 256
     63
     64 # Leave coredumps in the first cache dir
     65 coredump_dir /var/spool/squid
     66
     67 #
     68 # Add any of your own refresh_pattern entries above these.
     69 #
     70 refresh_pattern ^ftp:           1440    20%     10080
     71 refresh_pattern ^gopher:        1440    0%      1440
     72 refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
     73 refresh_pattern .               0       20%     4320

 

프록시 서버 설정 적용

 

squid.conf 파일 설정을 통해 Squid 서비스 설정을 적용하려면 아래와 같은 명령어를 실행하여 Squid 서비스를 재시작 한 후에 적용이 됩니다.

systemctl restart squid

 

프록시 접근 허용 설정

 

프록시 서버를 접근 허용을 위하여 37,38 번째 라인 설정을 주석 처리 합니다.

37 #http_access allow manager localhost
38 #http_access deny manager

그리고 모든 사용자 접근 허용을 위해서 56번째 "http_access deny all" 모두 거부에서 모두 허용으로 수정 합니다.

56 http_access allow all

 

프록시 서버를 통해 인터넷을 사용할 수 있도록 IP 주소 허용

 

프록시 서버를 통해 인터넷을 사용할 수 있는 IP 주소 범위를 설정합니다.

12번 라인 밑에 허용할 IP주소 목록을 작성합니다.

acl localnet src 10.20.30.0/24

위와 같이 허용할 IP주소 목록을 작성하면, 10.20.30.1 ~ 10.29.30.255 사이의 IP 주소 범위를 허용하게 됩니다.

 8 acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
 9 acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
 10 acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
 11 acl localnet src fc00::/7       # RFC 4193 local private network range
 12 acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machine
 13 acl localnet src 10.20.30.0/24

 

HTTP 연결을 위한 특정 포트 허용

 

Squid는 적은 수의 포트만 허용 설정이 되어있어서 해당 포트를 통한 연결만 허용합니다. 기본적으로 허용되는 포트는 다음과 같습니다.

 15 acl SSL_ports port 443
 16 acl Safe_ports port 80          # http
 17 acl Safe_ports port 21          # ftp
 18 acl Safe_ports port 443         # https
 19 acl Safe_ports port 70          # gopher
 20 acl Safe_ports port 210         # wais
 21 acl Safe_ports port 1025-65535  # unregistered ports
 22 acl Safe_ports port 280         # http-mgmt
 23 acl Safe_ports port 488         # gss-http
 24 acl Safe_ports port 591         # filemaker
 25 acl Safe_ports port 777         # multiling http

위에 나열되지 않은 포트는 프록시 서버를 통해 액세스되지 않습니다. 특정 포트로 액세스 허용을 하려면 아래와 같이 추가 할 수 있습니다. 예를들어 26번라인에 아래의 설정값을 추가해 보겠습니다.

acl Safe_ports port 105
 15 acl SSL_ports port 443
 16 acl Safe_ports port 80          # http
 17 acl Safe_ports port 21          # ftp
 18 acl Safe_ports port 443         # https
 19 acl Safe_ports port 70          # gopher
 20 acl Safe_ports port 210         # wais
 21 acl Safe_ports port 1025-65535  # unregistered ports
 22 acl Safe_ports port 280         # http-mgmt
 23 acl Safe_ports port 488         # gss-http
 24 acl Safe_ports port 591         # filemaker
 25 acl Safe_ports port 777         # multiling http
 26 acl Safe_ports port 105

 

Squid 사용자 인증 사용

 

프록시 서버에 연결하기 전에 사용자 인증을 사용하려면 Squid 프락시에서 사용 가능한 기본 인증 기능을 사용하여 사용자를 인증할 수 있습니다.

 

먼저 암호화된 비밀번호 파일을 만드는 데 사용할 httpd-tools 도구를 다음 명령어를 사용하여 설치합니다.

yum -y install httpd-tools

설치가 완료되면 새 파일을 만들고 Squid 서비스에 대한 소유권을 적용합니다. 다음 명령어를 사용하여 적용합니다.

touch /etc/squid/passwd && chown squid /etc/squid/passwd

그리고 htpasswd를 사용하여 새 사용자를 추가할 수 있습니다.

proxy_test라는 아이디를 가진 사용자를 추가해보겠습니다.

htpasswd /etc/squid/passwd proxy_test

위의 명령어를 실행하면 아래와 같이 proxy_test 사용자의 비밀번호를 입력하여 사용자 계정을 생성합니다.

New password:
Re-type new password:
Adding password for user proxy_test

정상적으로 사용자가 추가가 완료되면 "Adding password for user proxy_test"라는 메시지를 출력합니다.

 

사용자 추가가 완료되었으면 Squid 설정 파일을 열어서 액세스 제어 목록을 설정합니다.

vi /etc/squid/squid.conf

Squid 설정파일을 열어서 28번째 라인에 아래와 같은 행을 추가합니다.

 auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
 auth_param basic children 5
 auth_param basic realm Squid Basic Authentication
 auth_param basic credentialsttl 2 hours
 acl auth_users proxy_auth REQUIRED
 http_access allow auth_users

위의 설정을 모두 완료하면 프록시 서버를 사용하려고 하면 사용자 인증을 요청합니다. 사용자 이름과 비밀번호를 입력하면 프록시 서버를 사용 할 수 있습니다.

 

Squid 프록시 서버 접속 포트 변경

 

Squid 프록시 서버에 연결하기 위해 사용하는 포트를 변경할 수 있습니다.

연결 포트를 변경하기 위해 Squid 설정 파일을 열어서 67번째 라인의 연결 포트를 설정합니다.

vi /etc/squid/squid.conf
66 # Squid normally listens to port 3128
67 http_port 3128

Squid 기본 설정 포트는 3128 포트로 설정되어있습니다. 3128 포트에서 다른 서비스에서 사용하고 있지 않은 포트로 설정합니다. 

 

3128 포트에서 3048 포트로 아래와 같이 변경해보겠습니다.

66 # Squid normally listens to port 3128
67 http_port 3048

 

프록시 포트 사용시 주의할 점

 

프록시 서버 연결시 사용하는 포트가 닫혀있으면 정상적으로 프록시 서버로 접속되지 않습니다. 그래서 firewall 명령어를 사용하여 프록시 서버 포트로 사용할 포트를 열어주어야 합니다.

아래와 같은 명령어로 프록시 서버로 사용할 3048 포트로 접속을 허용합니다.

firewall-cmd --zone=public --permanent --add-port=3048/tcp

포트가 성공적으로 허용되면 "success" 메시지가 출력되게 됩니다.

포트 접속 허용 후 적용을 위하여 firewall을 재시작합니다. 재시작 명령어는 아래와 같습니다.

firewall-cmd --reload

이렇게 하여서 3048 포트에 대해서 접속을 허용하여 프록시 서버의 3048 포트로 접속을 할 수 있게 되었습니다.

 

4. 마무리

Centos7 시스템에서 Squid 서비스를 사용하여 프록시 서버 세팅을 하는 방법을 진행 하였습니다. 프록시 서버에 대한 기본적인 설정 방법을 설명하였으며, 추후 추가적인 설정 방법은 이후 포스팅에서 이어나갈 예정입니다.

'Server' 카테고리의 다른 글

Window10 에서 프록시 서버 접속 방법  (0) 2020.04.09