海运的博客

Nginx反向代理做负载均衡及缓存服务器

发布时间:May 2, 2012 // 分类:高可用 // No Comments

安装编译环境及相关组件:

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 \
libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl \
curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap \
openldap-devel nss_ldap openldap-clients openldap-servers unzip

编译安装PCRE、Nginx及缓存清除模块ngx_cache_purge:

/usr/local/src/
wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.zip
unzip pcre-8.30.zip 
cd pcre-8.30
./configure 
make && make install  
cd ..
wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
tar zxvf ngx_cache_purge-1.2.tar.gz 
wget http://nginx.org/download/nginx-1.0.15.tar.gz
tar zxvf nginx-1.0.15.tar.gz 
cd nginx-1.0.15
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module  \
--with-http_gzip_static_module --add-module=../ngx_cache_purge-1.2
make && make install  

Nginx配置文件:

user  www www;

worker_processes 4;

error_log  /usr/local/nginx/logs/nginx_error.log  crit;

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

worker_rlimit_nofile 65535;

events
    {
        use epoll;
        worker_connections 65535;
    }

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

                charset utf-8;   

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

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

                proxy_temp_path /cache_tmp;
                proxy_cache_path /cache levels=1:2 keys_zone=proxy_cache:256m inactive=10d max_size=10g;

                client_body_buffer_size  512k;
                proxy_connect_timeout 5;   
                proxy_read_timeout 60;   
                proxy_send_timeout 15; 
                proxy_buffer_size        16k;
                proxy_buffers            4 64k;
                proxy_busy_buffers_size 128k;
                proxy_temp_file_write_size 128k;

        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;

                upstream proxy_server_pool 
                {
                server node1.www.haiyun.me;
                server node2.www.haiyun.me;
                }
                 

                 log_format   proxy
                 $remote_addr-$remote_user-$time_local-$request-$body_bytes_sent-$http_referer-
                 $http_user_agent-$upstream_addr-$upstream_cache_status-$upstream_status ;
server
        {
                 listen       80;
                 server_name cdn.www.haiyun.me;
                 location /
                 {
                 proxy_next_upstream http_502 http_504 error timeout invalid_header;   
                 proxy_cache proxy_cache;
                 proxy_cache_valid  200 304 1d;
                 proxy_cache_key $host$uri$is_args$args; #$http_cookie
                 add_header Nginx-Cache "$upstream_cache_status from $upstream_addr";
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_pass http://proxy_server_pool;
                 }

                 location ~ .*\.(php|jsp|cgi)?$
                 {
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_pass http://proxy_server_pool;
                 }
                 location ~ /purge(/.*)   
                 {    
                 allow 127.0.0.1;   
                 deny all;   
                 proxy_cache_purge proxy_cache $host$uri$is_args$args;   
                 }   
                 access_log  /usr/local/nginx/logs/cdn.www.haiyun.me.log proxy;
         }
}

标签:nginx, linux, 反向代理, 负载均衡

评论已关闭

分类
最新文章
最近回复
  • opnfense: 谢谢博主!!!解决问题了!!!我之前一直以为内置的odhcp6就是唯一管理ipv6的方式
  • liyk: 这个方法获取的IPv6大概20分钟之后就会失效,默认路由先消失,然后Global IPV6再消失
  • 海运: 不好意思,没有。
  • zongboa: 您好,請問一下有immortalwrt設定guest Wi-Fi的GUI教學嗎?感謝您。
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...