成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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技術

2019-09-10 12:59:45

2018-11-12 12:17:00

2023-01-30 22:10:12

BeanSpring容器

2024-07-22 15:34:25

2018-01-16 08:57:24

Nginx規則實用

2015-06-25 18:54:17

varnish降級系統

2020-10-22 08:05:46

Nginx

2019-06-19 15:34:39

Nginx反向代理負載均衡

2012-11-05 10:33:40

IBMdw

2017-12-18 12:04:02

Nginx代理均衡

2021-04-27 09:45:33

Nginx日志運維

2017-09-06 10:14:29

Nginx TCPmail郵件

2022-07-01 07:33:24

nginx反向代理測試

2010-03-29 17:56:20

Nginx反向代理

2020-08-06 08:23:24

Nginx反向代理Web安全

2023-09-13 07:16:31

Ngnix代理服務器

2014-04-29 14:54:48

Nginx反向代理

2015-06-25 17:28:44

免費代理網絡安全
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产中文在线 | 国产免费一区二区三区 | 亚洲精久 | a级片www| 91久久精品日日躁夜夜躁欧美 | 久久久国产一区 | 久久黄色精品视频 | 91久久久久久久久 | 国产超碰人人爽人人做人人爱 | 一本一道久久a久久精品综合蜜臀 | 香蕉久久久久久 | 日本不卡免费新一二三区 | 就操在线| 欧美日韩一区在线观看 | 久久狠狠| 国产重口老太伦 | 亚洲综合在 | 拍戏被cao翻了h承欢 | 一区二区在线免费观看 | 91精品国产色综合久久不卡98 | 另类专区亚洲 | 久久精品一区二区三区四区 | 日韩一区二区三区在线视频 | 久久国产精品免费视频 | 天天色天天色 | 亚洲精选一区二区 | 国产线视频精品免费观看视频 | 国产亚洲一区二区精品 | 精品久久一区 | 热久久免费视频 | 成人福利视频网站 | 一区欧美 | 青青草精品 | 免费一看一级毛片 | 久久se精品一区精品二区 | 久久精品亚洲欧美日韩精品中文字幕 | 男女羞羞视频在线观看 | 日韩在线视频一区 | 欧美电影在线观看网站 | 久久久久久国产精品久久 | www视频在线观看 |