大家,nginx本身其实是做web服务器的,但后来nginx通过各种第三方模块的支持,提供了更多的功能。本次案例就是nginx做后端apache的反向代理和缓存服务。

下图为nginx反向代理和缓存服务的工作原理。

首先,来自互联网的请求发送到nginx反向代理服务器这里,nginx通过proxy_pass代理后端的服务器、通过upstream做到对后端web服务器的将康检查、负载分配等。
后端的apache收到,nginx代理转发的request,进行处理,并返回response
nginx代理收到response,做一些处理后,通过proxy_cache和fastcgi_cache进行缓存,然后发给请求的客户端。
##本次案例还使用了一个sticky模块用以解决session同步的问题


nginx反向代理与缓存服务_第1张图片

实验环境:

nginx反向代理:192.168.44.131

源码包:nginx-1.14.0.tar.gz、ngx_cache_purge-2.3.tar.gz、nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz

apache1:192.168.44.137

apache2:192.168.44.138

后端apache1和apache2的配置

yum -y install httpd
systemctl enable httpd
systemctl start httpd
firewall-cmd --add-port=8/tcp --permanent
firewall-cmd --reload
setenforce 0

为测试效果,两个apache的网页做的是不一样的,如下

nginx反向代理与缓存服务_第2张图片

nginx反向代理与缓存服务_第3张图片

nginx上的配置(注意你需要修改的地方是##注释处的配置

1、安装nginx

使用tar解压源码包
tar zxf nginx-1.14.0.tar.gz
tar zxfngx_cache_purge-2.3.tar.gz
tar zxf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz

安装依赖包
yum -y install gcc gcc-c++ make libtool pcre-devel pcre zlib-devel zlib openssl-devel openssl

创建运行用户
groupadd -r www                        ##你的用户
useradd -r -g www -s /sbin/nologin www            ##你的组

安装
cd nginx-1.14.0/
 ./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module --with-http_realip_module --with-http_ssl_module \
--with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-pcre --add-module=../ngx_cache_purge-2.3 --with-http_flv_module \
--add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42                    ##注意这里配置的时候组和用户写你自己的
##配置根据自己所需可以选择安装那些模块
##(--add-module=../ngx_cache_purge-2.3 --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42,
这两个模块是我们源码包解压的模块,分别是用来清除缓存和解决session同步问题的负载均衡调度方案)
make 
make install

安装后的简单处理
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
mkdir -p /var/tmp/nginx/client
nginx -t
##nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
##nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
##出现上面两行表示主配置文件的语法和测试是没有问题的。
nginx
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
setenforce 0

##以下是我的nginx.conf的配置(注意配置文件中的缩进)
user www;            ##你的用户
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log logs/error.log;
worker_rlimit_nofile 10240;
pid logs/nginx.pid;
events {
	use epoll;
	worker_connections 4096;
}
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"'
					'"$upstream_cache_status"';
	access_log logs/access.log main;
	server_tokens off;
	sendfile on;
	keepalive_timeout 65;
	gzip on;
	gzip_comp_level 6;
	gzip_http_version 1.1;
	gzip_proxied any;
	gzip_min_length 1k;
	gzip_buffers 16 8k;
	gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
	gzip_vary on;
	client_max_body_size 10m;
	client_body_buffer_size 128k;
	proxy_connect_timeout 75;
	proxy_send_timeout 75;
	proxy_read_timeout 75;
	proxy_buffer_size 4k;
	proxy_buffers 4 32k;
	proxy_busy_buffers_size 64k;
	proxy_temp_file_write_size 64k;
	proxy_buffering on;
	proxy_temp_path /usr/local/nginx/proxy_temp;
	proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m
	max_size=1000m inactive=600m max_size=2g;
	upstream backend {
		sticky;
		server 192.168.44.137:80 weight=1 max_fails=2 fail_timeout=10s;            
		server 192.168.44.138:80 weight=1 max_fails=2 fail_timeout=10s;                ##你的web服务器地址
	}
	server {
		listen 80;
		server_name localhost;
		charset utf-8;
		location ~/purge(/.*) {
			allow 127.0.0.1;
			allow 192.168.44.0/24;            允许的清除缓存的网段
			deny all;
			proxy_cache_purge my-cache $host$1$is_args$args;
		}
		location / {
			index index.php index.html index.htm;
			proxy_pass http://backend;
			proxy_redirect off;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
		}
		location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)(.*) {
			proxy_pass http://backend;
			proxy_redirect off;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
			proxy_cache my-cache;
			add_header Nginx-Cache $upstream_cache_status;
			proxy_cache_valid 200 304 301 302 8h;
			proxy_cache_valid 404 1m;
			proxy_cache_valid any 1d;
			proxy_cache_key $host$uri$is_args$args;
			expires 30d;
		}
		location /nginx_status {
			stub_status on;
			access_log off;
			allow 192.168.44.0/24;            ##允许查看nginx状态的网段
			deny all;
		}
	}
}

##改完配置文件记得重新加载nginx
nginx -s reload

访问http://192.168.44.131(nginx代理的地址)

nginx反向代理与缓存服务_第4张图片

刷新几次,通过F12进入谷歌的开发者工具,查看网络-图片的header,可以看到nginx-cache为hit(命中),这样就说明我们的nginx缓存服务成功了。

nginx反向代理与缓存服务_第5张图片

现在我们down掉apache1,再次访问http://192.168.44.131

systemctl stop httpd

nginx反向代理与缓存服务_第6张图片

发现服务没有中断,而是访问到了apache2上面的网页。表示我们的upstream的健康检查生效。