配置Nginx负载均衡器,配置nginx负载均衡


需要一台负载均衡服务器Nginx,两台Tomcat真实服务器

Nginx服务器IP 192.168.45.120

Tomcat服务器IP:192.168.45.121

Tomat服务器IP:192.168.45.122

查看nginx的使用命令:/usr/local/nginx/sbin/nginx -h

截图:


修改配置文件配置nginx服务器

配置文件目录:/usr/local/nginx/conf/nginx.conf

配置文件:

#user  nobody;
worker_processes  1;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#error_log  "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";


#pid        logs/nginx.pid;




events {
    worker_connections  1024;
}


# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}


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;
    #access_log  "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G"  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #添加tomcat真实服务器
    upstream local_tomcat  {    
        server 127.0.0.1:8080 weight=1;  
        server 127.0.0.1:8088 weight=1;  
  
  
        #健康检查  
        check interval=3000 rise=2 fall=5 timeout=1000 type=http;  
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";  
        check_http_expect_alive http_2xx http_3xx;  
    } 
	
	
    #gzip  on;


    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;
        #access_log  "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G"  main;


	#修改内容
        location / {
            proxy_pass http://local_tomcat; 
        }


        #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 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;
    #    }
    #}


}

修改完配置文件后使用命令 /usr/local/nginx/sbin/nginx -t 测试文件是否正确。如果正确使用命令 /usr/local/nginx/sbin/nginx -s reload 来重新加载配置文件。


    更多配置参考淘宝的tengine中文翻译的nginx配置信息

    地址:http://tengine.taobao.org/nginx_docs/cn/docs/        点击打开链接

参考文献:https://blog.csdn.net/nimasike/article/details/51909835        点击打开链接

                 http://seanlook.com/2015/05/17/nginx-install-and-config/   配置    

                 http://seanlook.com/2015/05/17/nginx-location-rewrite/    URL规则

                 http://www.nginx.cn/115.html  URL规则


                    


相关内容

    暂无相关文章