三台ubuntu计算机,ip分别为
192.168.0.5 负载均衡服务器
192.168.0.7 web服务器
192.168.0.8 web服务器
三台设备都安装了nginx
在web服务器安装目录/usr/local/nginx/html下创建文件夹proxy_loc,proxy_loc文件下创建index.html文件
weijl@weijl-ubuntu:/usr/local/nginx/html$sudo mkdir proxy_loc
weijl@weijl-ubuntu:/usr/local/nginx/html/proxy_loc$sudo touch index.html
weijl@weijl-ubuntu:/usr/local/nginx/html$sudo chmod -R 777 proxy_loc
192.168.0.7 服务器index.html文件内容如下:
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to 192.168.0.7</h1></center>
</body>
</html>
192.168.0.8 服务器index.html文件内容如下:
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to 192.168.0.8</h1></center>
</body>
</html>
配置负载均衡服务器nginx.conf
#worker工作进程的用户及用户组
user weijl;
#Nginx worker进程个数
worker_processes 1;
#error日志的设置,默认logs/error.log error
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid文件的路径
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
#嵌入配置文件mime.types
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream test.proxy.com {
#ip_hash;
server 192.168.0.7;
server 192.168.0.8;
}
server {
listen 127.0.0.1:80;
server_name test.proxy.com;
#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;
}
#静态图片资源
location /image/ {
root /home/weijl/workspace/;
autoindex on;
}
#反向代理
location /proxy_loc/ {
root html;
proxy_set_header Host $host;
proxy_pass http://test.proxy.com;
#禁用缓存
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
#实现自己的HTTP模块
location /test {
mytest;
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;
# }
#}
}
测试验证结果如图:
刷新后