一 cdn内容分发网络
软件安装
Varnish安装:
官网:http://varnish-cache.org/
软件下载:https://developer.aliyun.com/mirror/
# yum install -y
varnish-4.0.5-1.el7.x86_64.rpm
varnish-libs-4.0.5-1.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm
二 varnish
varnish工作原理
1 软件安装
varnish-4.0.5-1.el7.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm
yum install -y *
2 修改配置文件
cd /etc/varnish/
vim default.vcl
###############
#
This is an example VCL file for Varnish.
#
It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
Marker to tell the VCL compiler that this VCL has been adapted to the
new 4.0 format.
vcl 4.0;
Default backend definition. Set this to point to your content server.
backend web1 {
.host = "172.25.5.2";
.port = "80";
}
backend web2 {
.host = "172.25.5.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web1;
#return(pass);
}
elseif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
#return(pass);
}
else {
return(synth(404,"Not in cache"));
}
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
sub vcl_backend_response {
Happens after we have read the response headers from the backend.
#
Here you clean the response headers, removing silly Set-Cookie headers
and other mistakes your backend does.
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
3 修改端口:
vim varnish.params
VARNISH_LISTEN_PORT=80
## server2 /server3
4 修改apache默认发布目录
cd /var/www/html
cat index.html
server2:www.westos.org
server3: bbs.westos.org
## 物理机
修改本地解析文件
vim /etc/hosts
172.25.7.1 www.westos.org bbs.westos.org westos.org
测试:
curl www.westos.org -I
## server4安装nginx
编辑文件vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream westos {
server 172.25.5.3:80;
server 172.25.5.2:80;
}
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos;
}
}
}
启动nginx
## server1
#
This is an example VCL file for Varnish.
#
It does not do anything by default, delegating control to the
builtin VCL. The builtin VCL is called when there is no explicit
return statement.
#
See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
Marker to tell the VCL compiler that this VCL has been adapted to the
new 4.0 format.
vcl 4.0;
Default backend definition. Set this to point to your content server.
#backend web1 {
.host = "172.25.5.2";
.port = "80";
#}
backend web2 {
.host = "172.25.5.3";
.port = "80";
}
backend web3 {
.host = "172.25.5.4";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web3;
return(pass);
}
elseif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
#return(pass);
}
else {
return(synth(404,"Not in cache"));
}
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
测试:
curl www.westos.org
三 nginx+varnish
## server3:
安装编译nginx
systemctl stop httpd 关闭apache
tar zxf nginx-1.20.1.tar.gz
cd nginx-1.20.1/
yum install pcre-devel gcc openssl-devel -y 安装依赖插件
./configure --with-http_realip_module --with-http_ssl_module
make -j2
make install
cd /usr/local/nginx/
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
2 编辑nginx文件
cd conf
vim nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
set_real_ip_from 172.25.5.0/24;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3 启动nginx
nginx -t 语法检测
nginx 启动应用
4 测试:
物理机
curl www.westos.org
5查看日志
cd ..
cat /usr/local/nginx/log/access.log
172.25.5.250 - - [13/Jul/2021:14:19:34 +0800] "GET / HTTP/1.0" 200 612 "-" "curl/7.61.1"