安装:
# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
3、下载Nginx
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.13.0.tar.gz
4、 编译安装 Nginx
# cd /usr/local/src/
# tar -zxvf nginx-1.13.0.tar.gz
# cd nginx-1.13.0
# ./configure --prefix=/usr/local/nginx
# make && make install
测试 Nginx 是否安装成功
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动 Nginx
# /usr/local/nginx/sbin/nginx
停止 Nginx
# /usr/local/nginx/sbin/nginx -s stop
重启 Nginx
# /usr/local/nginx/sbin/nginx -s reload
设置 Nginx 开机启动
# vi /etc/rc.local
加入:
/usr/local/nginx/sbin/nginx
负载均衡策略:
进入到conf目录之后,修改nginx.conf
详细说明:
user nginx nginx; # 运行用户 worker_processes 8; #启动进程,通常设置成和cpu的数量相等 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
error_log /opt/log/nginx/error_pipe; #全局错误日志及PID文件 pid /opt/nginx-1.10.1/nginx.pid;
events { worker_connections 65535; #单个后台worker process进程的最大并发链接数 use epoll; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { fastcgi_intercept_errors on; server_tokens off; server_name_in_redirect off; include mime.types; default_type text/plain; log_format main '$remote_addr $upstream_addr $request_time $http_x_readtime [$time_local] "$request_method $host$request_uri" $status $request_length $body_bytes_sent "$http_referer" "$http_user_agent"'; #设置日志格式 access_log /opt/log/nginx/access_pipe main; #设置全局访问日志 log_not_found off; sendfile on; #指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件 tcp_nopush on; keepalive_timeout 60; #连接超时时间 client_body_timeout 15s; client_max_body_size 16M; lingering_close off; charset utf-8; charset_types text/plain; server_names_hash_bucket_size 128;
gzip on; #开启 gzip 压缩 gzip_http_version 1.0; gzip_comp_level 9; gzip_min_length 1024; gzip_proxied any; gzip_vary on; gzip_disable msie6; gzip_buffers 96 8k; gzip_types text/xml text/plain text/css application/javascript application/x-javascript application/rss+xml;
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Web-Server-Type nginx; proxy_set_header WL-Proxy-Client-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_intercept_errors on; proxy_redirect off; proxy_buffers 128 8k; proxy_ignore_client_abort on; proxy_connect_timeout 300; proxy_read_timeout 300; proxy_send_timeout 300;
# ai-dc的负载均衡配置 upstream ai-dc.oneapm { server 10.128.6.90:8080; server 10.128.6.91:8080; }
# ai-dv 的负载均衡配置 upstream ai-dv.oneapm { server 10.128.6.92:8090; server 10.128.6.93:8090; ip_hash; #设置session共享 }
# usercenter 的负载均衡配置 upstream usercenter { server 10.128.6.92:8088; }
# jetty 的负载均衡配置 upstream jetty{ server 10.128.6.92:8000; }
server{ listen 81; #侦听81 端口 server_name 10.128.6.240; #设定server的ip地址 error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
location = /nginx_status { allow 127.0.0.1; deny all; stub_status on; access_log off; }
location / { root html; proxy_pass http://usercenter; #设置跳转到用户中心的负载均衡配置 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log /applog/nginx/usercenter_pipe main; error_log /applog/nginx/usercenter_error_pipe; } }
server{
listen 80; #侦听80 端口 server_name 10.128.6.240; #设定server的ip地址 error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
location = /nginx_status { allow 127.0.0.1; deny all; stub_status on; access_log off; }
location / { root html; proxy_pass http://ai-dv.oneapm; #设置跳转到ai-dv的负载均衡配置 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log /applog/nginx/ai-dv_pipe main; error_log /applog/nginx/ai-dv_error_pipe; }
location ~* .+/agent\.do { proxy_pass http://ai-dc.oneapm; #设置跳转到ai-dc的负载均衡配置 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 4M;
access_log /applog/nginx/ai-dc_pipe main; error_log /applog/nginx/ai-dc_error_pipe; } location /alert/v2 { proxy_pass http://jetty; #设置跳转到jetty的负载均衡配置 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log /applog/nginx/jetty_pipe main; error_log /applog/nginx/jetty_error_pipe;
}
}
} |
nginx反向代理:
在nginx的nginx.conf文件里面:
upstream tomcatserver1 {
server 192.168.236.66:8080;
}
upstream tomcatserver2 {
server 192.168.236.66:8081;
}
server {
listen 80;
server_name 8080.isc.com;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
}
}
server {
listen 80;
server_name 8081.isc.com;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
注意:upstream和proxy_pass必须要一致。
nginx负载均衡:
1、轮询
这种是默认的策略,把每个请求按顺序逐一分配到不同的server,如果server挂掉,能自动剔除。
upstream fengzp.com {
server 192.168.99.100:42000;
server 192.168.99.100:42001;
}
2、最少连接
把请求分配到连接数最少的server
upstream fengzp.com {
least_conn;
server 192.168.99.100:42000;
server 192.168.99.100:42001;
}
3、权重
使用weight来指定server访问比率,weight默认是1。以下配置会是server2访问的比例是server1的两倍。
upstream fengzp.com {
server 192.168.99.100:42000 weight=1;
server 192.168.99.100:42001 weight=2;
}
4、ip_hash
每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,可以解决session的问题。如果server挂掉,能自动剔除。
upstream fengzp.com {
ip_hash;
server 192.168.99.100:42000;
server 192.168.99.100:42001;
}
ip_hash可以和weight结合使用。