FastDFS蛋疼的集群和负载均衡(五)之tracker配置反向代理

diary_report.jpg

Interesting things

接着上一篇。

What did you do today

  • 我们需要在tracker1和tracker2配置反向代理服务,那么你肯定会问了什么是反向代理服务?

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

  • 在tracker1和tracker2上解压ngx_cache_purge-2.3.tar.gz文件到/usr/local/fast/
    命令:tar zxvf ngx_cache_purge-2.3.tar.gz -C /usr/local/fast/


    image.png
  • 我们发现/usr/local/fast/目录下多了ngx_cache_purge-2.3文件夹。


    image.png
  • 下载依赖库 yum install pcre、yum install pcre-devel、yum install zlib、yum install zlib-devel

  • 解压nginx-1.6.2.tar.gz到/usr/local/目录下,命令: tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/


    image.png
  • 进入/usr/local/nginx-1.6.2/目录下,
    cd /usr/local/nginx-1.6.2/


    image.png
  • 添加ngx_cache_purge-2.3模块并且检查。命令:./configure --add-module=/usr/local/fast/ngx_cache_purge-2.3/


    image.png
  • 老操作,make && make install编译安装nginx。

  • 进入/usr/local/nginx/conf/目录下,找到nginx.conf,配置反向代理。

#user  nobody;
worker_processes  1;

error_log  /usr/local/nginx/logs/error.log;
error_log  /usr/local/nginx/logs/error.log  notice;
error_log  /usr/local/nginx/logs/error.log  info;

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


events {
         worker_connections  1024;
        use epoll;
}


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" "$http_x_forwarded_for"';

    access_log /usr/local/nginx/logs/access.log  main;

    sendfile        on;
    tcp_nopush      on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
        server_names_hash_bucket_size   128;
        client_header_buffer_size       32k;
        large_client_header_buffers     4       32k;
        client_max_body_size    300m;

        proxy_redirect  off;
        proxy_set_header        Host    $http_host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout   90;
        proxy_send_timeout      90;
        proxy_read_timeout      90;
        proxy_buffer_size       16k;
        proxy_buffers   4       64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size      128k;

        proxy_cache_path        /fastdfs/cache/nginx/proxy_cache levels=1:2
        keys_zone=http-cache:200m       max_size=1g     inactive=30d;
        proxy_temp_path /fastdfs/cache/nginx/proxy_cache/tmp;

        upstream fdfs_group1 {
                server 192.168.12.33:8888 weight=1  max_fails=2 fail_timeout=30s;
                server 192.168.12.44:8888 weight=1  max_fails=2 fail_timeout=30s;

        }

        upstream fdfs_group2 {
                server 192.168.12.55:8888 weight=1 max_fails=2 fail_timeout=30s;
                server 192.168.12.66:8888 weight=1 max_fails=2 fail_timeout=30s;

        }
    server {

        listen      8000;
        server_name  localhost;

        #charset koi8-r;

       access_log  /usr/local/nginx/logs/host.access.log  main;

       location / {
           root   html;
           index  index.html index.htm;
       }

        location /group1/M00 {
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_cache http-cache;
                proxy_cache_valid 200 304 12h;
                proxy_cache_key $uri$is_args$args;
                proxy_pass http://fdfs_group1;
                expires 30d;

        }

        location /group2/M00 {
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_cache http-cache;
                proxy_cache_valid 200 304 12h;
                proxy_cache_key $uri$is_args$args;
                proxy_pass http://fdfs_group2;
                expires 30d;
        }

        location ~/purge(/.*) {
                allow 127.0.0.1;
                allow 192.168.12.0/24;
                deny all;
                proxy_cache_purge http-cache $1$is_args$args;

        }

       # location ~/group([0-9])/M00 {  
            # ngx_fastdfs_module;   
        # }

        #error_page  404              /404.html;

        #redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
  • 创建/fastdfs/cache/nginx/proxy_cache
    和/fastdfs/cache/nginx/proxy_cache/tmp,因为proxy_cache_path和proxy_temp_path设置了路径,所以我们要创建。


    image.png
image.png
  • 由于tracker1和tracker2的端口是8000,所以需要在防火墙配置8000端口。
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8000 -j ACCEPT,然后重启防火墙,让策略生效。


    image.png
  • 启动tracker1和tracker2的nginx。命令:/usr/local/nginx/sbin/nginx


    image.png
  • 我们在tracker1上传2张图片,发现一张存储在group1,一张存储在group2.


    image.png
  • 我们可以通过tracker1(192.168.12.11)和tracker2(192.168.12.22)的8000端口去访问这2张图片。

  • 访问http://192.168.12.11:8000/group1/M00/00/00/wKgMIVpEgoSAcs8VAADRd6mMX3g514.jpg

    image.png

image.png

Summary

Nginx对外提供服务有可能碰到服务挂掉的时候,我们需要搭建一个nginx和keepalived集合实现的nginx集群高可用环境,下一篇讲。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容