【知識積累】大數據旅程-Nginx 反向代理和負載均衡

一、數據採集器

  • log_format:日誌格式定義
  • main:日誌格式名稱
  • access_log:日誌文件路徑

二、基本配置

1、配置server


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}

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  logs/access.log  main;

    sendfile        on;			#0拷貝,也就是不會進行用戶態和內核態的切換,直接從磁盤讀取到內核的存儲空間,然後直接返回。
    #tcp_nopush     on;			#快速推送,禁用buffer機制。

    #keepalive_timeout  0;		#保持的超時時間,減少資源消耗。
    keepalive_timeout  65;

    #gzip  on;				#壓縮,時間換帶寬。

    upstream userServices {		#配置負載均衡的服務器列表
        server 192.168.217.12;
        server 192.168.217.13;
   }

    upstream orderServices {		#配置負載均衡的服務器列表
        server 192.168.217.12:8080;
        server 192.168.217.13:8080;
   }

    server {
	listen 80;			#監聽的端口
	server_name www.darren.com;	#服務名
	
	location / { 			#資源定位
	   root /mnt;
	   autoindex on;		#自動索引(也就是主頁)
	}
	
	location /go {
	  proxy_pass http://192.168.217.12/;  #實際請求地址:http://192.168.217.12/ 			
	}

	location /baidu {
	  proxy_pass https://www.baidu.com/;  #需要將http修改爲https,否則會跳轉到www.baidu.com
	}

	location /user {
	  proxy_pass http://userServices/;    #負載均衡到userServices配置的服務器
	}
	
	location /order {
	  proxy_pass http://orderServices/;    #負載均衡到orderServices配置的服務器
	}
	
    }

    server {
        listen       80;

        server_name  node01;               #修改爲node01

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

}

2、windows中的hosts文件配置

3、service nginx reload:重新加載配置

4、!service:history命令從後往前找到匹配命令,然後執行

5、測試

 

三、yum倉庫

1、掛載cdrom目錄到mnt

mount /dev/cdrom /mnt

2、配置location

3、測試

四、匹配規則

nginx 收到請求頭:判定ip、port、hosts決定server

nginx location匹配:用客戶端的uri匹配location的uri

1、先普通

  • 順序無關
  • 最大前綴
  • 匹配規則簡單

打斷(不再正則匹配):

  • ^~
  • 完全匹配

2、再正則

  • 不完全匹配
  • 正則特殊性:一條URI可以和多條location匹配上
  • 有順序的
  • 先匹配,先應用,即時退出匹配。

五、反向代理

1、格式:proxy_pass ip:prot[uri]

location /go {
   proxy_pass http://192.168.217.12/;  #實際請求地址:http://192.168.217.12/ 			
}

如果被代理的地址後面接了uri,這去掉最大前綴,例如:"http://192.168.217.12/"帶了"/",則不會拼接"/go"。

2、測試

六、負載均衡

1、配置

 upstream userServices {		#配置負載均衡的服務器列表
        server 192.168.217.12;
        server 192.168.217.13;
  }
server {
	listen 80;			#監聽的端口
	server_name www.darren.com;	#服務名

	location /user {
	  proxy_pass http://userServices/;    #負載均衡到userServices配置的服務器
	}
  }

2、測試

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