【知识积累】大数据旅程-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:port[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、测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值