一、准备内容
在本实例中,需要准备四台电脑,并且为每一台电脑分配相应的IP地址,由于IP地址在实验环境中存在差异,所以在以下的操作中,使用机器名称来确定操作的机器。
lb01: Nginx 主负载均衡器
web01: 动态服务器
web03: 文件上传服务器
1)安装必要的工具集
# yum install wget vim gcc –y
2)安装依赖包集合
# yum install openssl openssl-devel pcre pcre-devel -y
3)下载nginx
# mkdir /home/tools
# cd /home/tools
# wget http://nginx.org/download/nginx-1.6.3.tar.gz
4)创建管理用户
# useradd nginx -s /sbin/nologin –M
5)解压并安装
# tar -zxvf nginx-1.6.3.tar.gz
# cd nginx-1.6.3
# ./configure –user=nginx –group=nginx –prefix=/data/nginx-1.6.3 –with-http_stub_status_module –with-http_ssl_module
# make && make install
# ln -s /data/nginx-1.6.3/ /data/nginx
# /data/nginx/sbin/nginx –t
# /data/nginx/sbin/nginx
1)将web01、web02及web02服务器的Nginx的配置文件进行如下修改。
# vi /data/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”‘; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.ls.com; # 此处为自定义的域名 location / { root html/www; index index.html index.htm; } access_log logs/access_www.log main; } } |
# echo “web01 dynamic ” >> /data/nginx/html/www/index.html
# mkdir /data/nginx/html/www
# echo “web02 static” >> /data/nginx/html/www/index.html
# curl 127.0.0.1
4)在web03服务器上建立测试目录及文件
# mkdir /data/nginx/html/www
# echo “web03 upload” >> /data/nginx/html/www/index.html
# curl 127.0.0.1
# vi /data/nginx/conf/nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream dynamic_pools { server 10.70.21.87:80 weight=1; } upstream static_pools { server 10.70.21.60:80 weight=1; } upstream upload_pools { server 10.70.21.104:80 weight=1; } server { listen 80; server_name www.ls.com; location / { proxy_pass http://dynamic_pools; include proxy.conf; } location /static/ { proxy_pass http://static_pools; include proxy.conf; } location /upload/ { proxy_pass http://upload_pools; include proxy.conf; } } } |
创建proxy配置信息,解决重复包含的问题。
# vi /data/nginx/conf/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; |
# /data/nginx/sbin/nginx -s reload
# mkdir /data/nginx/html/www/dynamic
# echo “web01 dynamic pools” >> /data/nginx/html/www/dynamic/ index.html
web02服务器
# mkdir /data/nginx/html/www/static
# echo “web02 static pools” >> /data/nginx/html/www/static/index.html
web03服务器
# mkdir /data/nginx/html/www/upload
# echo “web03 upload pools” >> /data/nginx/html/www/upload/index.html
访问”www.ls.com/static/”的结果:
访问”www.ls.com/upload/”的结果: