單服務器配置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;
<!--more-->
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;
<span style="color: #ff0000;">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;</span>

<span style="color: #ff0000;"> </span> 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;
<span style="color: #333399;">#cache
upstream backend_server {
server  127.0.0.1:80 weight=1 max_fails=2 fail_timeout=30s;
}</span>
<span style="color: #ff0000;">#負載均衡
upstream  www.zeno.com.cn  {
server   127.0.0.1:81;
server   127.0.0.1:82;
}
#負載均衡服務器
server{</span>

<span style="color: #ff0000;"> listen  127.0.0.1:80;
server_name  www.zeno.com.cn;</span>

<span style="color: #ff0000;"> 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;
}</span>

<span style="color: #ff0000;"> }</span>
<span style="color: #333399;">#cache 緩存服務器
server
{
listen       80;
server_name  www.zeno.com 192.168.110.249;
index index.html index.htm;
root  /usr/local/www/zeno;</span>

<span style="color: #333399;"> 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;
}</span>

<span style="color: #333399;"> location ~ /purge(/.*)
{
allow            127.0.0.1;
allow            192.168.110.249;
deny            all;
proxy_cache_purge    cache_one   $host$1$is_args$args;
}</span>

<span style="color: #333399;"> location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $remote_addr;
proxy_pass http://backend_server;
}</span>

<span style="color: #333399;"> access_log  off;
}</span>

#虛機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;
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章