首先,打开Nginx配置文件
Vi /etc/nginx/nginx.cnf
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
keepalive_timeout 65;
limit_zone myzone $binary_remote_addr 10m;
upstream www.server110.com {
server 222.186.41.118;
server 222.186.41.174;
}
Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
server {
limit_conn myzone 10;
listen 80;
server_name www.server110.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://www.server110.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
location /status {
stub_status on;
access_log off;
error_log off;
}
选定的位置是我做的修改。这样就可以实现Nginx负载均衡了,但前提是后端源服务器要保证数据的一致性。
最后添加的status模块,是查看Nginx的运行状态。
http://www.domain.com/status
Active connections: 1
server accepts handled requests
66 66 98
Reading: 0 Writing: 1 Waiting: 0
备注:
active connections– 对后端发起的活动连接数
server accepts handled requests– nginx 总共处理了 30871298 个连接, 成功创建 30871298 次握手,总共处理了 105864919个请求
reading– nginx 读取到客户端的Header信息数
writing– nginx 返回给客户端的Header信息数
waiting– 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接
如果reading或writing的值很高,说明正在处理的数据量很大,可能是因为后端的php程序处理慢,拖了后腿,而一般来说,PHP之后以慢,是因为MYSQL,另一个原因很可能就是IO慢,或者客户端的网络慢(这种情况在国内常见些).