【知识积累】大数据旅程-Nginx 反向代理和负载均衡

一、数据采集器

  • log_format:日志格式定义
  • main:日志格式名称
  • access_log:日志文件路径

二、基本配置

1、配置server

1 2#user nobody; 3worker_processes 1; 4 5#error_log logs/error.log; 6#error_log logs/error.log notice; 7#error_log logs/error.log info; 8 9#pid logs/nginx.pid; 10 11 12events { 13 worker_connections 1024; 14} 15 16# load modules compiled as Dynamic Shared Object (DSO) 17# 18#dso { 19# load ngx_http_fastcgi_module.so; 20# load ngx_http_rewrite_module.so; 21#} 22 23http { 24 include mime.types; 25 default_type application/octet-stream; 26 27 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 28 # '$status $body_bytes_sent "$http_referer" ' 29 # '"$http_user_agent" "$http_x_forwarded_for"'; 30 31 #access_log logs/access.log main; 32 33 sendfile on; #0拷贝,也就是不会进行用户态和内核态的切换,直接从磁盘读取到内核的存储空间,然后直接返回。 34 #tcp_nopush on; #快速推送,禁用buffer机制。 35 36 #keepalive_timeout 0; #保持的超时时间,减少资源消耗。 37 keepalive_timeout 65; 38 39 #gzip on; #压缩,时间换带宽。 40 41 upstream userServices { #配置负载均衡的服务器列表 42 server 192.168.217.12; 43 server 192.168.217.13; 44 } 45 46 upstream orderServices { #配置负载均衡的服务器列表 47 server 192.168.217.12:8080; 48 server 192.168.217.13:8080; 49 } 50 51 server { 52 listen 80; #监听的端口 53 server_name www.darren.com; #服务名 54 55 location / { #资源定位 56 root /mnt; 57 autoindex on; #自动索引(也就是主页) 58 } 59 60 location /go { 61 proxy_pass http://192.168.217.12/; #实际请求地址:http://192.168.217.12/ 62 } 63 64 location /baidu { 65 proxy_pass https://www.baidu.com/; #需要将http修改为https,否则会跳转到www.baidu.com 66 } 67 68 location /user { 69 proxy_pass http://userServices/; #负载均衡到userServices配置的服务器 70 } 71 72 location /order { 73 proxy_pass http://orderServices/; #负载均衡到orderServices配置的服务器 74 } 75 76 } 77 78 server { 79 listen 80; 80 81 server_name node01; #修改为node01 82 83 #charset koi8-r; 84 85 #access_log logs/host.access.log main; 86 87 location / { 88 root html; 89 index index.html index.htm; 90 } 91 92 #error_page 404 /404.html; 93 94 # redirect server error pages to the static page /50x.html 95 # 96 error_page 500 502 503 504 /50x.html; 97 location = /50x.html { 98 root html; 99 } 100 101 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 102 # 103 #location ~ \.php$ { 104 # proxy_pass http://127.0.0.1; 105 #} 106 107 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 108 # 109 #location ~ \.php$ { 110 # root html; 111 # fastcgi_pass 127.0.0.1:9000; 112 # fastcgi_index index.php; 113 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 114 # include fastcgi_params; 115 #} 116 117 # deny access to .htaccess files, if Apache's document root 118 # concurs with nginx's one 119 # 120 #location ~ /\.ht { 121 # deny all; 122 #} 123 } 124 125 126 # another virtual host using mix of IP-, name-, and port-based configuration 127 # 128 #server { 129 # listen 8000; 130 # listen somename:8080; 131 # server_name somename alias another.alias; 132 133 # location / { 134 # root html; 135 # index index.html index.htm; 136 # } 137 #} 138 139 140 # HTTPS server 141 # 142 #server { 143 # listen 443 ssl; 144 # server_name localhost; 145 146 # ssl_certificate cert.pem; 147 # ssl_certificate_key cert.key; 148 149 # ssl_session_cache shared:SSL:1m; 150 # ssl_session_timeout 5m; 151 152 # ssl_ciphers HIGH:!aNULL:!MD5; 153 # ssl_prefer_server_ciphers on; 154 155 # location / { 156 # root html; 157 # index index.html index.htm; 158 # } 159 #} 160 161} 162 163

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]

1location /go { 2 proxy_pass http://192.168.217.12/; #实际请求地址:http://192.168.217.12/ 3} 4 5

如果被代理的地址后面接了uri,这去掉最大前缀,例如:"**http://192.168.217.12/**"带了" /",则不会拼接" /go"。

2、测试

六、负载均衡

1、配置

1 upstream userServices { #配置负载均衡的服务器列表 2 server 192.168.217.12; 3 server 192.168.217.13; 4 } 5server { 6 listen 80; #监听的端口 7 server_name www.darren.com; #服务名 8 9 location /user { 10 proxy_pass http://userServices/; #负载均衡到userServices配置的服务器 11 } 12 } 13 14

2、测试

代码交流 2021