FastDFS蛋疼的集羣和負載均衡(五)之tracker配置反向代理

diary_report.jpg
###Interesting things

接着上一篇。php

###What did you do todayhtml

  • 咱們須要在tracker1和tracker2配置反向代理服務,那麼你確定會問了什麼是反向代理服務?

反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。nginx

  • 在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/ bash

    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/ session

    image.png

  • 進入/usr/local/nginx-1.6.2/目錄下, cd /usr/local/nginx-1.6.2/ app

    image.png

  • 添加ngx_cache_purge-2.3模塊而且檢查。命令:./configure --add-module=/usr/local/fast/ngx_cache_purge-2.3/ tcp

    image.png

  • 老操做,make && make install編譯安裝nginx。ui

  • 進入/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

  • 訪問http://192.168.12.22:8000/group2/M00/00/00/wKgMQlpEgoaABUrWAADRd6mMX3g168.jpg

image.png

###Summary

Nginx對外提供服務有可能碰到服務掛掉的時候,咱們須要搭建一個nginx和keepalived集合實現的nginx集羣高可用環境,下一篇講。

相關文章
相關標籤/搜索