近期尝试使用Nginx做负载均衡,所使用的服务器一个是我自己的腾讯云服务器,另一个是吉林大学的服务器,采用默认的轮询算法实现负载均衡。
以下是关于Nginx做负载均衡的介绍:
配置文件设置如下:
#用户组 用户
#user nobody;
#工作进程,根据硬件调整
worker_processes 1;
#错误日志
#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类型
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指令指定nginx是否调用sendfile函数来输出文件,
#对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,
#以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#设置缓存路径和名称为webserver,内存缓存空间大小为200MB,30天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
#proxy_cache_path /usr/local/nginx/webserver levels=1:2 keys_zone=webserver:200m inactive=3d max_size=30g;
upstream web{
server 202.198.16.80 ;
server 123.206.89.25:8080;
#server 127.0.0.1:8080 weight=1;
}
server {
#指定虚拟主机的服务端口
listen 1025;
#制定虚拟主机的IP地址
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#自定义响应首部
#添加一个X-Via显示服务器地址
#add_header X-Via $server_addr;
#添加一个X-Cache显示缓存是否命中
#add_header X-Cache $upstream_cache_status;
location / {
#root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径
root html;
#index用于设定访问的默认首页地址
index index.html index.htm;
#设置转发的目的路径即被代理服务器的地址
#proxy_pass http://www.jlu.edu.cn;
proxy_pass http://web;
proxy_set_header X-Real-IP $remote_addr;
#设置缓冲区的名字
#proxy_cache webserver;
#为不同应答设置缓存时间
#proxy_cache_valid 200 10m;
}
#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;
#proxy_pass http://www.jlu.edu.cn;
#proxy_cache webserver;
#proxy_cache_valid 200 10m;
}
# 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;
# }
#}
}