单服务器配置nginx做webserver 负载均衡器和缓存服务器(测试玩)

1、Nginx 负载均衡与缓存服务器在 Linux 下的编译安装:
//这些软件下载地址,有可能有新版本,所以老版本就不存在了,如下载不了,可以到这个网站去看!!
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.01.tar.gz
tar zxvf pcre-8.00.tar.gz
cd pcre-8.00/
./configure
make && make install
cd ../

wget http://labs.frickle.com/files/ngx_cache_purge-1.1.tar.gz
tar zxvf ngx_cache_purge-1.1.tar.gz

wget http://nginx.org/download/nginx-0.8.36.tar.gz
tar zxvf nginx-0.8.36.tar.gz
cd nginx-0.8.36/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.1 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
2.实现Nginx,单服务器做负载均衡器和缓存服务器(比较好玩)
/usr/local/nginx/conf/nginx.conf配置如下

user  www www;

worker_processes 8;

error_log  /var/log/nginx/nginx_error.log  crit;

pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
use epoll;
worker_connections 65535;
}

http
{
include       mime.types;
default_type  application/octet-stream;

#charset  gb2312;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

client_body_buffer_size  512k;
proxy_connect_timeout    5;
proxy_read_timeout       60;
proxy_send_timeout       5;
proxy_buffer_size        16k;
proxy_buffers            4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path   /tmp/proxy_temp_dir;  #这个两个目录要创建
proxy_cache_path  /tmp/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;


gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#cache
upstream backend_server {
server  127.0.0.1:80 weight=1 max_fails=2 fail_timeout=30s;
}

#负载均衡
upstream  www.zeno.com.cn  {
server   127.0.0.1:81;
server   127.0.0.1:82;
}
#负载均衡服务器
server{


listen  127.0.0.1:80;
server_name  www.zeno.com.cn;


location / {
proxy_pass        http://www.zeno.com.cn;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}


}
#cache 缓存服务器
server
{
listen       80;
server_name  www.zeno.com 192.168.110.249;
index index.html index.htm;
root  /usr/local/www/zeno;


location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid  200 304 12h;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $remote_addr;
proxy_pass http://backend_server;
expires      1d;
}


location ~ /purge(/.*)
{
allow            127.0.0.1;
allow            192.168.110.249;
deny            all;
proxy_cache_purge    cache_one   $host$1$is_args$args;
}


location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $remote_addr;
proxy_pass http://backend_server;
}


access_log  off;
}


#虚机1
server
{
listen       127.0.0.1:81;
server_name  zeno.com;
index index.html index.htm index.php;
root  /usr/local/www/zeno.com;
include common_www;

}

#虚机2
server
{
listen       127.0.0.1:82;
server_name  zeno.cn;
index index.html index.htm index.php;
root  /usr/local/www/zeno;
include common_www;
}

}

你可能感兴趣的:(linux,Nginx)