Ubuntu下Nginx做负载实现高性能WEB服务器2

Ubuntu下Nginx做负载实现高性能WEB服务器
Ubuntu下Nginx做负载实现高性能WEB服务器2—日志优化
Ubuntu下Nginx做负载实现高性能WEB服务器3—PHP网站的部署和时间同步
Ubuntu下Nginx做负载实现高性能WEB服务器4---RSYNC实现网站同步
Ubuntu下Nginx做负载实现高性能WEB服务器5---MySQL主主同步

1.环境

前端nginx:外网 192.168.80.8 内网 192.168.1.8 ubuntu10.10 X86 +nginx 1.0.14

后端web1 : 内网 192.168.1.9  ubuntu10.10 X86+apache 2.2.16+mysql 5.1.61+PHP 5.3.3

后端web2:内网 192.168.1.10 ubuntu10.10 X86+apache 2.2.16+mysql 5.1.61+PHP 5.3.3

Notice: 需要在web1和web2上做同样的设置,或者做好同步工作。

2.首先看前端nginx设置 /etc/nginx/conf.d/default.conf

upstream loadbalance {

server 192.168.1.9 weight=10 max_fails=3 fail_timeout=30s; # Reverse proxy to Web server 1

server 192.168.1.10 weight=10 max_fails=3 fail_timeout=30s; # Reverse proxy to Web server 2

server 192.168.1.11 backup;

server 192.168.1.12 down;

#ip_hash;

}

server

{

listen 192.168.80.8:80; # Listen on the external interface

server_name 192.168.80.8; # The server name , also can

location / {

proxy_pass ; # Load balance the URL location "/" to the upstream loadbalance

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Get the real client ip address

}

log_format 192.168.80.8 '$remote_addr - $remote_user [$time_local] $request '

'"$status" $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log 192.168.80.8; # access log format

location /nginx_status { # nginx status monitor

stub_status on;

access_log off;

allow 10.10.10.3;

deny all;} # only allow 10.10.10.3 access

}

其中有下面几句代码,其中"proxy_set_header"指令便是向用来向后端apache2发送真实IP的。

proxy_set_header Host $host;  # 向后端服务器发起请求时添加指定的header头信息

proxy_set_header X-Real-IP $remote_addr; # 向后端服务器发送真实 IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 让后端能直接通过变量获取真实IP

如果没有这几句,查看apache访问日志会看到全是127.0.0.1的访问记录

root@ubuntu2:~# less /var/log/apache2/access.log

Nginx配置,Nginx负载均衡

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3a0e4fe4a9c7bf481786ffdcdf835188.html