- 论坛徽章:
- 0
|
nginx和varnish的详细配置:
nginx和varnish的详细配置:
nginx的配置文件如下:
user www www;
worker_processes 2;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/var/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
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" ';
log_format timing '$remote_addr $remote_user - [$time_local] $request '
'upstream_response_time $upstream_response_time '
'msec $msec request_time $request_time';
log_format up_head '$remote_addr $remote_user - [$time_local] $request '
'upstream_http_content_type $upstream_http_content_type';
access_log /usr/local/nginx/logs/access.log main;
keepalive_timeout 30;
server_tokens off;
sendfile on;
#tcp_nopush on;
tcp_nodelay on;
upstream bk1 {
server 192.168.111.10:8080;
server 192.168.111.20:8080;
}
server
{
listen 80;
client_max_body_size 50M;
server_name .mysite.cn .mysite.com
index index.htm index.html index.asp index.aspx;
root /www/default;
#error_page 403 500 502 503 504 /404.html;
#limit_conn httplimit 10;
location /status {
stub_status on;
access_log off;
#auth_basic "NginxStatus";
#auth_basic_user_file conf/htpasswd;
}
location /varnish {
alias /www/default/varnish.txt;
default_type text/plain;
}
location /crm {
alias /www/default/crm.txt;
default_type text/plain;
}
location / {
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;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
if (!-f $request_filename) {
proxy_pass http://bk1;
}
}
}
}
Varnish的配置文件如下:
acl purge {
"localhost";
}
backend bk1 {
set backend.host = "192.168.111.110";
set backend.port = "80";
}
backend bk2 {
set backend.host = "192.168.111.111";
set backend.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "mysite.cn") {
if (req.restarts == 0) {
set req.backend=bk1;
}
else {
set req.backend=bk2;
}
}
else {
if (req.http.host ~ "mysite.com"") {
if (req.restarts == 0) {
set req.backend=bk2;
}
else {
set req.backend=bk1;
}
}
else {
error 200 "No cahce for this domain";
}
}
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
else {
lookup;
}
}
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
if (req.http.Expect) {
pipe;
}
if (req.http.Authenticate ){
pass;
}
if (req.http.Cache-Control ~ "no-cache") {
pass;
}
if(req.url ~ "purge.php"){
pass;
}
if (req.url ~ "\.(htm|html|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|css|js|vsd|doc|ppt|pps|xls|pdf|mp3|mp4|m4a|ogg|mov|avi|wma|flv|wmv|sxw|zip|gz|bz2|tgz|tar|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") {
lookup;
}
lookup;
}
sub vcl_pipe {
if (bereq.http.x-forwarded-for) {
set bereq.http.X-forwarded-for =
bereq.http.X-forwarded-for ", "
regsub(client.ip, ":.*", "");
} else {
set bereq.http.X-forwarded-for =
regsub(client.ip, ":.*", "") ;
}
pipe;
}
sub vcl_pass {
if (bereq.http.x-forwarded-for) {
set bereq.http.X-forwarded-for =
bereq.http.X-forwarded-for ", "
regsub(client.ip, ":.*", "");
} else {
set bereq.http.X-forwarded-for =
regsub(client.ip, ":.*", "");
}
pass;
}
sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
hash;
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
if (!obj.cacheable) {
pass;
}
#set obj.http.X-TTL = obj.ttl;
deliver;
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
fetch;
}
sub vcl_fetch {
if (obj.status >400 && req.restarts == 0 )
{
restart;
}
if (!obj.valid) {
error;
}
if (!obj.cacheable) {
pass;
}
if (obj.status == 404) {
pass;
}
if (obj.ttl < 120000s) {
set obj.ttl = 120000s;
}
if (req.url ~ "\.(html|htm|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|css|js|vsd|doc|ppt|pps|xls|pdf|mp3|mp4|m4a|ogg|mov|avi|wma|flv|wmv|sxw|zip|gz|bz2|tgz|tar|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") {
insert;
}
if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") {
set obj.http.MYCACHE ="force-cache";
insert;
#pass;
}
insert;
}
sub vcl_deliver {
deliver;
}
sub vcl_timeout {
discard;
}
sub vcl_discard {
discard;
}
[ 本帖最后由 minuteman 于 2008-4-15 11:33 编辑 ] |
|