初识nginx负载均衡

来源:互联网 发布:js正则获取标签属性值 编辑:程序博客网 时间:2024/06/06 09:26

环境:

主机三台,系统centos,一个为nginx服务器(1)180.150.184.156,另外两台发布tomcat服务(2)180.150.184.202(3)180.150.184.203。

(1)

nginx安装:

yum -y install nginx


修改nginx配置文件nginx.conf

vim /etc/nginx/nginx.conf


user              nginx;
worker_processes  4;


error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;


pid        /var/run/nginx.pid;




events {
    use epoll;
    worker_connections  1024;
}




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;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;


    upstream 180.150.184.156 {
     server 180.150.184.202:8080 weight=1;
     server 180.150.184.203:8080 weight=6;
#     server 180.150.184.204:8080 weight=3;
    }


server {
listen      80;
server_name  180.150.184.156;
location / {
proxy_pass http://180.150.184.156;

}

}

}

(2)(3)分别安装jdk  tomcat。分别发布简单的rest接口作为测试使用,接口地址:

http://180.150.184.202:8080/historylife/rest/index;

http://180.150.184.203:8080/historylife/rest/index

启动tomcat。

启动nginx:  service nginx start.

访问:http://180.150.184.156:80/historylife/rest/index 按照轮询算法,将按照1:6的比例分别转发请求到(2)(3)服务器。






0 0
原创粉丝点击