1、本文件末尾给出的配置是一台运行正常的Nginx服务器
2、如果需要使用同一台Nginx服务器对应多个不同的网站,可以新增多个server节点,指向不同的集群
3、如需下载NGINX一键安装程序包,可以访问https://download.csdn.net/download/wxl847466025/10634363,按照说明操作即可
其他备注:
upstream lb {#加权轮询,按服务器的性能给予权重,本例是1:2分配
server 192.168.196.130 weight=1 fail_timeout=20s;
server 192.168.196.132 weight=2 fail_timeout=20s;
}
upstream lb {#ip_hash轮询方法,不可给服务器加权重
server 192.168.196.130 fail_timeout=20s;
server 192.168.196.132 fail_timeout=20s;
ip_hash;
}
upstream backserver {#每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
server 192.168.0.14;
server 192.168.0.15;
}
upstream backserver {#指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
upstream backserver {#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
upstream backserver {#按后端服务器的响应时间来分配请求,响应时间短的优先分配。
server server1;
server server2;
fair;
}
upstream backserver {#按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
在需要使用负载均衡的server中增加
proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载)
server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器)
}
#user nobody;
worker_processes 1;#运行进程数,如有多个核心且未部署其他网站,可以设为与核心数相同或稍多(不超过一倍)
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';#日志类型
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat_server{
server 127.0.0.1:3001 weight=1;#权重
server 127.0.0.1:3002 weight=2;
server 127.0.0.1:3003 weight=3;
ip_hash;#来自于一个IP地址的访问分配至指定的业务服务器,如此项不设置,则Session、Cookie可能失效
}
server {
listen 3000;#监听端口
server_name tomcat_server;#对应权限设置
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.aspx index.html index.htm;
proxy_pass http://tomcat_server;#代理地址
proxy_set_header Host $host;#默认即可
proxy_set_header X-Real-IP $remote_addr; #默认即可
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#默认即可
}
#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 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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}