Varnish+Nginx 配置----Nginx

Varnish+Nginx 配置----Nginx

分类: 缓存2013-04-17 14:56 1489人阅读 评论(0) 收藏 举报javascript

NginxVarnish缓存css

最近项目引入反向代理和缓存,熟悉了一下Squid、Apache、Valish、Nginx,根据项目实际进行选择,客观来讲,采用Linux系统部署最好,也没有什么难度,但实际状况必须采用Windows系统(本着方案要结合现实的原则,研究要以Windows平台为主)。
html


Nginx配置:前端

[plain] view plaincopyjava

  1. #user  nobody;  node

  2. worker_processes  1;  nginx

  3.   

  4. #error_log  logs/error.log;  后端

  5. #error_log  logs/error.log  notice;  数组

  6. #error_log  logs/error.log  info;  缓存

  7.   

  8. #pid        logs/nginx.pid;  

  9.   

  10.   

  11. events {  

  12.     worker_connections  1024;  

  13. }  

  14.   

  15.   

  16. http {  

  17.     include       mime.types;  

  18.     default_type  application/octet-stream;  

  19.   

  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  

  21.     #                  '$status $body_bytes_sent "$http_referer" '  

  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  

  23.   

  24.     #access_log  logs/access.log  main;  

  25.   

  26.     #::cst::  

  27.     send_timeout 300s;  

  28.     #server_names_hash_bucket_size 128;     #指定服务器名称哈希表的框大小  

  29.     #client_header_buffer_size 1k;         #设置读取客户端请求头部的缓冲容量。  

  30.     large_client_header_buffers 4 32k;     #以上两个是设定客户端请求的Header头缓冲区大小,对于,cookie内容较大的请求,应增大改值。(400或414错误)  

  31.     client_max_body_size 300m;                #容许客户端请求的最大单文件字节数  

  32.     client_body_buffer_size 128k;            #设置读取客户端请求正文的缓冲容量。缓冲大小默认等于两块内存页的大小,在x86平台、其余32位平台和x86-64平台,这个值是8K。在其余64位平台,这个值通常是16K。  

  33.       

  34.     proxy_connect_timeout 300s;              #nginx跟后端服务器链接超时时间(代理链接超时)  

  35.     proxy_read_timeout    300s;              #链接成功后,后端服务器响应时间(代理接收超时)  

  36.     proxy_send_timeout    300s;              #后端服务器数据回传时间(代理发送超时)  

  37.     proxy_ignore_client_abort on;           #不容许代理端主动关闭链接  

  38.     #::cst::end  

  39.   

  40.     sendfile       on;  

  41.     #tcp_nopush     on;  #开启或者关闭nginx在FreeBSD上使用TCP_NOPUSH套接字选项, 在Linux上使用TCP_CORK套接字选项。 选项仅在使用sendfile的时候才开启。  

  42.     keepalive_timeout  75s;  

  43.     #tcp_nodelay on;  #开启或关闭nginx使用TCP_NODELAY选项的功能。 这个选项仅在将链接转变为长链接的时候才被启用。  

  44.   

  45.   

  46.     #::cst::  

  47.     gzip  on;  

  48.     gzip_min_length  20;  

  49.     gzip_buffers     32 4k;  

  50.     gzip_http_version 1.0;  

  51.     gzip_proxied    any;  #enables compression for all proxied requests.前端是squid的状况下要加此参数,不然squid上不缓存gzip文件  

  52.     gzip_comp_level 6;  

  53.     gzip_types       text/plain application/x-javascript text/css application/xml;  

  54.     gzip_vary   off;   #Enables or disables emitting the “Vary: Accept-Encoding” response header field if the directives gzip, gzip_static, or gunzip are active.  

  55.   

  56.     upstream  tg_server {  

  57.         server 192.168.100.253:7573;  

  58.     }  

  59.   

  60.     server {  

  61.         listen       7579;  

  62.         server_name  192.168.100.253;  

  63.   

  64.         #charset koi8-r;  

  65.   

  66.         #access_log  logs/host.access.log  main;  

  67.   

  68.         #::cst::  

  69.         location ~ .*\.(bmp|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm|pdf)(.*|$)  

  70.         {  

  71.             #若是后端的服务器返回50二、50四、执行超时等错误,自动将请求转发到upstream负载均衡池中的另外一台服务器,实现故障转移。  

  72.             #proxy_next_upstream http_502 http_504 error timeout invalid_header;  

  73.   

  74.             #proxy_cache cache_tg_one;                  #进行缓存,使用Web缓存区cache_tg_one  

  75.             #proxy_cache_valid 200 304 12h;              #对不一样的HTTP状态码设置不一样的缓存时间  

  76.             #roxy_cache_valid 301 302 1m;  

  77.             #proxy_cache_valid any 1m;  

  78.             #proxy_cache_key $scheme$proxy_host$uri$is_args$args;  #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内  

  79.               

  80.             proxy_set_header  Host $host;  

  81.             proxy_set_header  X-Real-IP  $remote_addr;  

  82.             proxy_set_header X-Forwarded-For $remote_addr;  

  83.             #proxy_set_header Accept-Encoding '';   #请求头的值为空,那么这个请求头将不会传送给后端服务器  

  84.               

  85.             #proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。  

  86.   

  87.             #验证是否缓存  

  88.             add_header X-Cache "Hit from in253.ngnix-cache.jjcj.com";  

  89.               

  90.             proxy_pass http://tg_server;  

  91.             expires  7d;  

  92.         }  

  93.   

  94.         #扩展名以aspx|asmx|ashx结尾的动态应用程序不缓存。  

  95.         location ~ .*\.(aspx|asmx|ashx)(.*|$) {  

  96.             proxy_pass http://tg_server;  

  97.             proxy_set_header  Host $host;  

  98.             proxy_set_header X-Forwarded-For $remote_addr;  

  99.             proxy_set_header X-Real-IP $remote_addr;  

  100.             #proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。  

  101.             #验证是否缓存  

  102.             add_header X-Cache "Miss from in253.ngnix-cache.jjcj.com";  

  103.         }  

  104.         #::cst::end  

  105.   

  106.         location / {  

  107.             proxy_pass http://tg_server;  

  108.             #proxy_set_header  Host $host;  

  109.             proxy_set_header  X-Real-IP  $remote_addr;  

  110.             #proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  

  111.               

  112.         }  

  113.   

  114.         #error_page  404              /404.html;  

  115.   

  116.         # redirect server error pages to the static page /50x.html  

  117.         #  

  118.         error_page   500 502 503 504  /50x.html;  

  119.         location = /50x.html {  

  120.             root   html;  

  121.         }  

  122.     }  

  123. }