Cách cài đặt Squid Proxy trên CentOS

Squid là gì?

Squid là một phần mềm Proxy Server dùng cho việc lưu trữ cache và lọc nội dung web. Squid hỗ trợ các giao thức HTTP, HTTPS, FTP,… giúp bạn tiết kiệm băng thông và tăng tốc độ truy cập web cho nhóm người dùng trong công ty. Nguyên tắc hoạt động của Squid là khi người dùng truy vấn đến 1 website thông qua Squid thì Squid sẽ kiểm tra xem nội dung trang web đó có được lưu trữ và có thay đổi không? Nếu đã có lưu trữ và không thay đổi thì Squid sẽ trả luôn nội dung đó cho người dùng mà không cần phải truy vấn đến địa chỉ website kia nữa. Điều này giúp quá trình duyệt web nhanh hơn rất nhiều.

Cách cài đặt Squid Proxy trên CentOS - proxy squid - Webmasters Tools Phát triển website
Squid Logo

Cách cài đặt Squid Proxy trên CentOS

Điều kiện để bạn có thể cài đặt Squid là bạn cần có VPS/Server sử dụng hệ điều hành CentOS, khuyến cáo bạn nên cài đặt Squid trên Server đặt tại các trung tâm dữ liệu tại quốc gia của bạn (ví dụ : VDC, FPT, hoặc Viettel) nhằm tận dụng ưu thế về hạ tầng đường truyền của các trung tâm này.

Để cài đặt Squid Proxy trên CentOS bạn thực hiện lệnh sau :

sudo yum install -y squid

Cấu hình để Squid tự động chạy khi Server khởi động

sudo chkconfig squid on

Cấu hình Squid Proxy

Thông tin cấu hình cho Squid sẽ được chứa trong file “/etc/squid/squid.conf“. Bạn khởi động Squid bằng lệnh sau :

sudo service squid start

Mặc định Squid sẽ chạy trên port 3128, nếu bạn muốn Squid chạy trên port khác thì mở file “/etc/squid/squid.conf” và tìm dòng sau để thay đổi “http_port 3128

Thêm dòng sau “acl localnet src 173.252.120.68” vào file “/etc/squid/squid.conf” để cho phép chỉ địa chỉ ip 173.252.120.68 sử dụng Squid làm Proxy

Tuy nhiên, đa số các bạn đều sử dụng IP động ở nhà nên cách này khó khả thi, do đó xTraffic sẽ hướng dẫn thêm bạn cách cấu hình Squid Proxy bắt buộc phải nhập username & pass để sử dụng.

Giả định mình sẽ setup username là “user_xtraffic”. Các bạn chạy lệnh sau :

sudo yum install -y httpd-tools
sudo touch /etc/squid/squid_passwd
sudo chown squid /etc/squid/squid_passwd
sudo htpasswd /etc/squid/squid_passwd user_xtraffic

Sau đó thêm vào đầu file “/etc/squid/squid.conf” nội dung sau :

auth_param basic program /usr/lib64/squid/ncsa_auth /etc/squid/squid_passwd
acl ncsa_users proxy_auth REQUIRED
http_access allow ncsa_users

Ngoài ra, để sử dụng Squid làm Proxy mà không muốn bị phát hiện ra là bạn dùng proxy thì bạn thêm vào cuối file “/etc/squid/squid.conf” nội dung sau :

via off
forwarded_for off
request_header_access Allow allow all 
request_header_access Authorization allow all 
request_header_access WWW-Authenticate allow all 
request_header_access Proxy-Authorization allow all 
request_header_access Proxy-Authenticate allow all 
request_header_access Cache-Control allow all 
request_header_access Content-Encoding allow all 
request_header_access Content-Length allow all 
request_header_access Content-Type allow all 
request_header_access Date allow all 
request_header_access Expires allow all 
request_header_access Host allow all 
request_header_access If-Modified-Since allow all 
request_header_access Last-Modified allow all 
request_header_access Location allow all 
request_header_access Pragma allow all 
request_header_access Accept allow all 
request_header_access Accept-Charset allow all 
request_header_access Accept-Encoding allow all 
request_header_access Accept-Language allow all 
request_header_access Content-Language allow all 
request_header_access Mime-Version allow all 
request_header_access Retry-After allow all 
request_header_access Title allow all 
request_header_access Connection allow all 
request_header_access Proxy-Connection allow all 
request_header_access User-Agent allow all 
request_header_access Cookie allow all 
request_header_access All deny all

Sau đó khởi động lại Squid để các cấu hình trên có hiệu lực :

service squid restart

Mở trình duyệt ra và cấu hình proxy theo thông tin bạn đã setup. Kiểm tra xem Squid Proxy có hoạt động không bằng cách truy cập vào website https://www.iplocation.net/

Leave a Comment