Varnish + nginx configuration -- varnish

Source: Internet
Author: User
Tags varnish

Recently, reverse proxy and cache have been introduced in the project. You are familiar with squid, Apache, valish, and nginx. You can choose based on the actual situation of the project. Objectively speaking, it is best to deploy a Linux system without any difficulty, but the actual situation must adopt the Windows system (based on the principle of combining the solution with the reality, the study should focus on the Windows platform ).


One varnish startup:

e:cd e:\varnish\binset PATH=%CD%;%PATH%varnishd -a :7575 -T :11212 -f /etc/varnish_cst_cfg.vcl -s file,E:/var/varnish/tg/cache/jjtg,1024M -p thread_pool_max=5000 -p thread_pool_min=100 REM pause

Ii. varnish Configuration:

# This is a basic VCL configuration file for varnish. see the VCL (7) # man page for details on VCL syntax and semantics. # default backend definition. set this to point to your content # server. backend tgweb {. host = "192.168.45.45 ";. port = "7574 ";. connect_timeout = 20 s ;. first_byte_timeout = 20 s ;. between_bytes_timeout = 20 s;} # cache refresh rules # ACL purgeallow {## refreshing only on the local machine # "localhost" ;#}# below is Commented-out copy of the default VCL logic. if you # redefine any of these subroutines, the built-in logic will be # appended to your code. sub vcl_recv {# determine the Request Host and jump to the corresponding backend server if (req. HTTP. host ~ "^ (. *) (Tg.stockjyb.com: 7575) ") {set req. backend = tgweb;} else {error 408 "hostname not found";} # Grace cache expired and still stored # If the backend is healthy, only the grace 5S, if the backend is unhealthy, grace 1 m. # Here, the 5 s objective is to improve the throughput during high concurrency; # The goal of 1 m is to continue the service for a period of time after the backend fails, it is expected that the backend will not be suspended for too long... If (req. backend. healthy) {set req. grace = 5S;} else {set req. grace = 1 m ;}# refresh cache processing # If (req. request = "purge") {# If (! Client. ip ~ Purgeallow) {# error 405 "not allowed. ";#}## convert to hit or miss processing # Return (lookup) ;#}# remove some cookies in specific formats if (req. URL ~ "^ (. *)\. (JPG | PNG | GIF | JPEG | FLV | BMP | GZ | tgz | bz2 | TBZ | JS | CSS | HTML | htm) ($ | \?) ") {# Remove the cookie so that it can be cached to varnish unset req. HTTP. cookie;} # accept-encoding is the encoding type supported by the browser. # modify the accept-encoding header of the client. # prevent individual browsers from sending messages similar to deflate, gzip if (req. HTTP. accept-encoding) {If (req. URL ~ "^ (. *) \. (JPG | PNG | GIF | JPEG | FLV | BMP | GZ | tgz | bz2 | TBZ) ($ | \?) ") {Remove Req. http. Accept-encoding;} else if (req. http. Accept-encoding ~ "Gzip") {set Req. http. Accept-encoding = "gzip";} else if (req. http. Accept-encoding ~ "Deflate") {set Req. http. Accept-encoding = "deflate";} else if (req. http. Accept-encoding ~ "SDCh") {# new compression set req in chrome. HTTP. accept-encoding = "SDCh";} else {remove req. HTTP. accept-encoding; }}# Add the X-forwarded-for header for the first access, so that the backend program can obtain the Client IP if (req. restarts = 0) {If (req. HTTP. x-forwarded-For) {set req. HTTP. x-forwarded-for = req. HTTP. x-forwarded-for + "," + client. IP;} else {set req. HTTP. x-forwarded-for = client. IP ;}# if (req. request! = "Get" & # Req. Request! = "Head" & # Req. Request! = "Put" & # Req. Request! = "Post" & # Req. Request! = "Trace" & # Req. Request! = "Options" & # Req. Request! = "Delete") {#/* Non-RFC2616 or connect Which is weird. */# Return (PIPE) ;#} if (req. Request! = "Get" & Req. Request! = "Head") {/* We only deal with get and head by default */Return (PASS);} If (req. HTTP. authorization) {/* Not cacheable by default */Return (PASS);} # the CSS file contains cookies and cannot be retrieved from the backend server every time # If (req. HTTP. cookie) {#/* Not cacheable by default */# Return (PASS) ;#}# if the requested dynamic page is forwarded directly to the backend server if (req. URL ~ "^ (. *)\. (Aspx | asmx | ashx) ($ |. *) ") {return (PASS);} return (lookup);} sub vcl_pipe {# note that only the first request to the backend will have # X-forwarded-for set. if you use X-forwarded-for and want to # have it set for all requests, make sure to have: # Set bereq. HTTP. connection = "close"; # here. it is not set by default as it might break some broken web # applications, like IIS with NTLM Authentication. return (PIPE) ;}# let it go, let it directly go to the background server to request data sub vcl_pass {return (PASS);} sub vcl_hash {hash_data (req. URL); If (req. HTTP. host) {hash_data (req. HTTP. host);} else {hash_data (server. IP address) ;}# added support for compression to prevent the content sent to browsers that do not support compression if (req. HTTP. accept-encoding) {hash_data (req. HTTP. accept-encoding) ;}return (hash) ;}# Cache Server Lookup hit: Hit sub vcl_hit {# cache refresh request operation, set TTL to 0, response Code # If (req. request = "P Urge ") {# Set obj. TTL = 0 s; # error 200 "purged. ";#}// # Return (deliver) after the Cache Server hits (found);} # The Cache Server Lookup finds the Miss sub vcl_miss {# request operation for refreshing the cache, # If (req. request = "purge") {# Error 404 "not in cache. ";#}// # The cache server does not hit (retrieve from the backend server) Return (FETCH);} # After retrieving data from the backend server, cache sub vcl_fetch {# If a dynamic page is requested for direct forwarding # If a dynamic request is returned, it must be placed before processing if (req. URL ~ "^ (. *)\. (Aspx | asmx | ashx) ($ |. *) ") {set beresp. HTTP. cache-control = "no-cache, no-store"; unset beresp. HTTP. expires; Return (deliver) ;}# set beresp only when the request can be cached. grace. If the request cannot be cached, no beresp is set. grace if (beresp. TTL> 0 s) {set beresp. grace = 1 m;} If (beresp. TTL <= 0 S | beresp. HTTP. set-Cookie | beresp. HTTP. vary = "*") {/** mark as "Hit-for-Pass" for the next 2 minutes */set beresp. TTL = 120 s; # No next request Perform lookup and directly pass return (hit_for_pass) ;}# set the cache TTL if (req. url ~ "^ (. *) \. (PDF | XLS | PPT | Doc | docx | XLSX | pptx | CHM | RAR | zip) ($ | \?) ") {# Remove the cookie unset beresp sent by the server. HTTP. set-cookie; # Add the cache time set beresp. TTL = 30d; Return (deliver);} else if (req. URL ~ "^ (. *)\. (BMP | JPEG | JPG | PNG | GIF | SVG | PNG | ICO | TXT | CSS | JS | HTML | htm) ($ | \?) ") {# Remove the cookie unset beresp sent by the server. HTTP. set-cookie; # Add the cache time set beresp. TTL = 15d; Return (deliver);} else if (req. URL ~ "^ (. *)\. (MP3 | WMA | MP4 | rmvb | Ogg | mov | Avi | WMV | MPEG | MPG | dat | 3pg | SWF | FLV | ASF) ($ | \?) ") {# Remove the cookie unset beresp sent by the server. HTTP. set-cookie; # Add the cache time set beresp. TTL = 30d; Return (deliver) ;}# the response information returned from the backend server is not cached. If (beresp. HTTP. pragma ~ "No-Cache" | beresp. http. cache-control ~ "No-Cache" | beresp. http. cache-control ~ "Private") {return (deliver);} # Call sub vcl_deliver before the cache server sends data to the client {# Add a header ID, to determine whether the cache is hit. If (obj. hits> 0) {set resp. HTTP. x-Cache = "hit from TG.varnish-cache.jjcj.com"; # Set resp. HTTP. x-varnish = "hit from TG.varnish-cache.jjcj.com";} else {set resp. HTTP. x-Cache = "Miss from TG.varnish-cache.jjcj.com"; # Set resp. HTTP. x-varnish = "Miss from TG.varnish-cache.jjcj.com";} # Remove the unset resp header that is not required. HTTP. vary; unset resp. HTTP. x-powered-by; unset resp. HTTP. x-ASPnet-version; Return (Deli Ver);} sub vcl_error {set obj. HTTP. content-Type = "text/html; charsets = UTF-8"; Set obj. HTTP. retry-after = "5"; synthetic {"<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <HTML> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.