3台VM虚拟机,1台做负载均衡,2台做RS。
系统:CentOS 6.9 x86_64
软件:nginx-1.15.2.tar.gz
3台服务器均安装Nginx。
yum -y install openssl openssl-devel pcre pcre-devel
useradd nginx -s /sbin/nologin -M tar zxvf nginx-1.15.2.tar.gz ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make make install
在两台web服务器上操作,配置如下:
[root@web01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } 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; keepalive_timeout 65; server { listen 80; server_name www.test.com; location / { root html/test; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
mkdir /usr/local/nginx/html/test echo "www.test.com 212" > /usr/local/nginx/html/test/index.html
在windows客户端测试,需先开通服务器防火墙的80端口,或关闭防火墙。
在lb01服务器上操作,配置如下:
[root@lb01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } 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; keepalive_timeout 65; **upstream test_server_pools { server** **192.168.5.212:80 weight=1; server 192.168.5.213:80 weight=1** **; }** server { listen 80; server_name www.test.com; location / { **proxy_pass http:** **//** **test_server_pools;** } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
官方示例:
upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; server backup1.example.com backup; }
server的参数说明:
可把参数写成一个文件,使用 include 包含,看起来更规范。
location / { proxy_pass http://test_server_pools; include proxy.conf; } [root@lb01 conf]# cat proxy.conf proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
1、修改hosts地址进行测试,Windows系统hosts文件路径:C:\Windows\System32\drivers\etc
192.168.5.210 www.test.com
2、浏览器端采用无缓存刷新页面,请求配均匀的分配到后端服务器。
原文链接:https://www.cnblogs.com/qiaokeshushu/p/9385763.html