Nginx代理varnish的那些姿势

开发 架构
以前做网站的时候遇到了网站的访问量很大,而导致后端处理程序响应超时而导致的一些问题。当时采用的架构是nginx+php-fastcgi,同事想到了用nginx-proxycache来做页面缓存,效果也还行。下面我想介绍一下varnish的使用技巧

以前做网站的时候遇到了网站的访问量很大,而导致后端处理程序响应超时而导致的一些问题。当时采用的架构是nginx+php-fastcgi,同事想到了用nginx-proxycache来做页面缓存,效果也还行。下面我想介绍一下varnish的使用技巧

准备

varnish严格来说是可以当作一个代理服务器的软件,直接将HTTP请求转发到php-cgi,然后交给php处理,varnish会获取经过php处理后的数据,***返回给浏览器。如图

 

 

但是,现在php-fastcgi已经被逐渐淘汰了,也就是说我们一般情况下不会使用php-fastcgi,那么我们不能直接将varnish与php组合,因为php-fpm的交互方式为socket,而不再是监听本机的9000端口

所以我们必须找一个的媒介,连接varnish和php-fpm,nginx可以扮演这个媒介,如下图:

 

那么问题来了,根据研究发现,varnish处理http请求不如nginx那么高效。所以如果我们让nginx做前锋,这样就更***了。那我们需要怎么才能达到这个目的呢,下面我们来整理一下流程

 

下面就来实现一下图三的架构吧。

事先需要准备nginx,varnish,php-fpm,php这些软件,OS是ubuntu,所有软件都可以用apt-get install来安装,不了解包名全称的话可以先apt-get update,更新一下源,然后再用apt-cache search xxx来查找软件包名

安装完varnish后,可以使用service varnish回车,查看可操作选项* Usage: /etc/init.d/varnish {start|stop|restart|reload|force-reload|configtest},一般安装完毕后,系统会自动启动varnish的,nginx也是一样,便不赘述了

配置

安装完所需的软件后,下面需要配置这些软件,来实现这个架构

nginx部分

vi /etc/nginx/nginx.conf 

  1. http { 
  2.     ## proxy global setting 
  3.     proxy_connect_timeout 5; 
  4.     proxy_read_timeout 60; 
  5.     proxy_send_timeout 5; 
  6.     proxy_buffer_size 16k; 
  7.     proxy_buffers 4 64k; 
  8.     proxy_busy_buffers_size 128k; 
  9.     ##END 
  10.     ## cache proxy pass 
  11.     upstream cache { 
  12.             server  127.0.0.1:6081; 
  13.     } 
  14.     ##END 
  15.     ## php proxy pass 
  16.     upstream php {  
  17.             server  127.0.0.1:8080; 
  18.     } 
  19.     ##END 
  20.     # Basic Settings 
  21.     sendfile on
  22.     tcp_nopush on
  23.     tcp_nodelay on
  24.     keepalive_timeout 65; 
  25.     types_hash_max_size 2048; 
  26.     server_tokens off
  27.     #depend on nginx-extras 需要安装nginx-extras才能定义Server 
  28.     more_set_headers 'Server: Bird-shark'
  29.     # server_names_hash_bucket_size 64; 
  30.     # server_name_in_redirect off
  31.     include /etc/nginx/mime.types; 
  32.     default_type application/octet-stream; 
  33.     ## 
  34.     # SSL Settings 
  35.     ## 
  36.     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 
  37.     ssl_prefer_server_ciphers on
  38.     ## 
  39.     # Logging Settings 
  40.     ## 
  41.     access_log /var/log/nginx/access.log; 
  42.     error_log /var/log/nginx/error.log; 
  43.     ## 
  44.     # Gzip Settings 
  45.     ## 
  46.     gzip on
  47.     gzip_disable "msie6"
  48.     gzip_vary on
  49.     gzip_proxied any
  50.     gzip_comp_level 6; 
  51.     gzip_buffers 16 8k; 
  52.     gzip_http_version 1.1; 
  53.     gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; 
  54.     ## 
  55.     # Virtual Host Configs 
  56.     ## 
  57.     include /etc/nginx/conf.d/*.conf; 
  58.     include /etc/nginx/sites-enabled/*;   
  59.  

varnish部分

vi /etc/varnish/default.vcl 

  1. server { 
  2.     listen 80 default_server; 
  3.     listen [::]:80 default_server; 
  4.     index index.html index.htm index.php; 
  5.     server_name localhost; 
  6.     location ~ .*\.(gif|jpg|png|css|js|flv|ico|swf|html)$ { 
  7.         proxy_set_header Host $host; 
  8.         proxy_set_header X-Real-IP $remote_addr; 
  9.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  10.         proxy_pass http://cache; 
  11.     } 
  12.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  13.     # 
  14.     location / { 
  15.         proxy_pass http://php; 
  16.         proxy_set_header Host $host; 
  17.         proxy_set_header X-Real-IP $remote_addr; 
  18.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  19.         proxy_pass_header Server; 
  20.     } 
  21. server { 
  22.     listen 8080; 
  23.     root /var/www/html; 
  24.     index  index.html index.htm index.php; 
  25.     location / { 
  26.         if (!-e $request_filename){ 
  27.             rewrite  ^(.*)$  /index.php?s=$1  last
  28.             break; 
  29.         } 
  30.         try_files $uri $uri/ =404; 
  31.     } 
  32.     location ~ ^(.+\.php)(.*)$ { 
  33.         fastcgi_pass unix:/var/run/php5-fpm.sock; 
  34.         fastcgi_intercept_errors on
  35.         fastcgi_buffers 8 128k; 
  36.         fastcgi_index  index.php; 
  37.         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
  38.         include        fastcgi_params; 
  39.     } 
  40.  

测试&分析

1. 在不使用缓存模块的情况下

vi /etc/nginx/sites-available/default

  1. #location ~ .*\.(gif|jpg|png|css|js|flv|ico|swf|html)$ { 
  2. #    proxy_set_header Host $host; 
  3. #    proxy_set_header X-Real-IP $remote_addr; 
  4. #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  5. #    proxy_pass http://cache; 
  6. #}  

先用chrome浏览器访问查看请求头

 

我们再使用curl,在服务器上执行以下命令

  1. curl -k -v 'http://192.168.99.1/Public/Home/images/t_navigation_logo.png' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh,en;q=0.8,zh-CN;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' --compressed 

发现有输出内容。

然后,反选disable cache

 

然后在服务器上执行以下命令

  1. curl  -k -v 'http://192.168.99.1/Public/Home/images/t_navigation_logo.png' -H 'If-None-Match: "57c6b733-1962"' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh,en;q=0.8,zh-CN;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT' --compressed 

发现只返回了头部信息,然而没有内容返回

然后我们比较两个命令 发现区别就在-H 'Pragma: no-cache'和 -H 'If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT' -H 'If-None-Match: "57c6b733-1962"'

57c6b733-1962这串字符对应的是服务器响应给浏览器的ETag部分的内容,然后我们修改一下部分的内容

  1. curl  -k -v 'http://192.168.99.1/Public/Home/images/t_navigation_logo.png' -H 'If-None-Match: "57c6b733-1234"' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: zh,en;q=0.8,zh-CN;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Connection: keep-alive' -H 'If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT' --compressed 

在服务器端执行一下。发现有内容返回,所以这个ETag相当于token,它不是由nginx随便生成的,且跟请求链接应是一一对应的,用来标识缓存的,当服务器返回的状态为304的时候,这时候我们浏览器会直接找到本地的缓存数据

2. 在使用缓存模块的情况下

vi /etc/nginx/sites-available/default

  1. location ~ .*\.(gif|jpg|png|css|js|flv|ico|swf|html)$ { 
  2.     proxy_set_header Host $host; 
  3.     proxy_set_header X-Real-IP $remote_addr; 
  4.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  5.     proxy_pass http://cache; 
  6.  

用浏览器查看响应头

发现X-Cache:MISS from 192.168.99.1.这表示缓存未***,然后我们刷新X-Cache:HIT from 192.168.99.1,这时候发现已经***了。

对于已经***的资源文件,我们如果将其删除会出现什么效果呢,答案是,其依然可以访问,除非重启或者将缓存清除

但是对PURGE显然是不对外公开的,以下是服务器端用curl清除varnish缓存的命令

  1. curl -v -k -X PURGE http://localhost/Public/Home/css/t_navigation.css 

结语

varnish是一款内存类型的缓存软件,而非nginx扩展proxy_cache那种物理缓存类型的软件,存取速度比较快,但是也有弊端,重启后所有缓存得重写。不管怎么说,什么架子都适用的场景,要想满足业务需求还是得捣鼓透彻,而我也只是将我想到的给实现出来,毕竟资源和精力都是有限的,也就随便玩玩,诸位看客看看就好,别太认真,知道怎么回事儿就行。

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2012-04-02 17:46:08

缓存对比

2013-05-17 11:40:26

CDNCDN技术

2018-11-12 12:17:00

2019-09-10 12:59:45

2023-01-30 22:10:12

BeanSpring容器

2018-01-16 08:57:24

Nginx规则实用

2015-06-25 18:54:17

varnish降级系统

2020-10-22 08:05:46

Nginx

2017-12-18 12:04:02

Nginx代理均衡

2019-06-19 15:34:39

Nginx反向代理负载均衡

2021-04-27 09:45:33

Nginx日志运维

2022-07-01 07:33:24

nginx反向代理测试

2017-09-06 10:14:29

Nginx TCPmail邮件

2012-11-05 10:33:40

IBMdw

2010-03-29 17:56:20

Nginx反向代理

2014-04-29 14:54:48

Nginx反向代理

2020-08-06 08:23:24

Nginx反向代理Web安全

2023-09-13 07:16:31

Ngnix代理服务器

2020-07-28 15:10:34

Nginx反向代理负载均衡

2010-03-30 18:26:07

Nginx Web服务
点赞
收藏

51CTO技术栈公众号