nginx做负载均衡和tomcat简单集群

简介: Nginx做负载均衡和TOMCAT简单集群                1.下载安装nginx及其依赖包                                             ...

                                                    Nginx做负载均衡和TOMCAT简单集群
                1.下载安装nginx及其依赖包
                
                
                
                安装nginx准备工作必须先安装依赖包:wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
                
                解压:tar zxvf pcre-8.35.tar.gz
                
                进入目录:cd pcre-8.35
                
                配置: ./configure
                
                编译安装:    make && make install
                
                下载:wget http://nginx.org/download/nginx-1.6.2.tar.gz
                解压:tar zxvf nginx-1.6.2.tar.gz
                
                
                进入目录:cd nginx-1.6.2
                
                配置:/configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35
                
                编译:make
                
                安装: make install
                
                查看版本:/usr/local/nginx/sbin/nginx -v
                
                创建ngingx运行时使用的用户:  /usr/sbin/groupadd nginx
                                             /usr/sbin/useradd -g nginx nginx
                                            
                配置nginx.conf:
                
                user www www;
                worker_processes 2; #设置值和CPU核心数一致
                error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
                pid /usr/local/webserver/nginx/nginx.pid;
                #Specifies the value for maximum file descriptors that can be opened by this process.
                worker_rlimit_nofile 65535;
                events
                {
                use epoll;
                worker_connections 65535;
                }
                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';

                #charset gb2312;
                
                server_names_hash_bucket_size 128;
                client_header_buffer_size 32k;
                large_client_header_buffers 4 32k;
                client_max_body_size 8m;
                
                sendfile on;
                tcp_nopush on;
                keepalive_timeout 60;
                tcp_nodelay on;
                fastcgi_connect_timeout 300;
                fastcgi_send_timeout 300;
                fastcgi_read_timeout 300;
                fastcgi_buffer_size 64k;
                fastcgi_buffers 4 64k;
                fastcgi_busy_buffers_size 128k;
                fastcgi_temp_file_write_size 128k;
                gzip on;
                gzip_min_length 1k;
                gzip_buffers 4 16k;
                gzip_http_version 1.0;
                gzip_comp_level 2;
                gzip_types text/plain application/x-javascript text/css application/xml;
                gzip_vary on;

                #limit_zone crawler $binary_remote_addr 10m;
                #下面是server虚拟主机的配置
                server
                {
                listen 80;#监听端口
                server_name localhost;#域名
                index index.html index.htm index.php;
                root /usr/local/webserver/nginx/html;#站点目录
                  location ~ .*\.(php|php5)?$
                {
                  #fastcgi_pass unix:/tmp/php-cgi.sock;
                  fastcgi_pass 127.0.0.1:9000;
                  fastcgi_index index.php;
                  include fastcgi.conf;
                }
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
                {
                  expires 30d;
                # access_log off;
                }
                location ~ .*\.(js|css)?$
                {
                  expires 15d;
                # access_log off;
                }
                access_log off;
                }

                }
                
                
                检查nginx配置文件nginx.conf正确命令:
                /usr/local/webserver/nginx/sbin/nginx -t
                
                启动nginx:
                /usr/local/webserver/nginx/sbin/nginx
                
                打开浏览器输入ip地址进入到nginx欢迎页说明配置成功,
                启动过程中会报错,解决办法进入/etc/sysconf/iptable配置其他端口或通过lsof -i:80端口看那个在占80端口,然后将其杀死即可解决。
                改配置文件记得重启。
                
                /usr/local/webserver/nginx/sbin/nginx -s reload            # 重新载入配置文件
                /usr/local/webserver/nginx/sbin/nginx -s reopen            # 重启 Nginx
                /usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx
                
                2.下载安装tomcat
                
                此命令一步安装:wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz
                (记得配置先要下载好jdk才行)
                //补充说明,为了辨别是否是不同tomcat可以进入tomcat目录下webapps中的ROOT修改index.jsp即可达到目的
                3.修改nginx配置文件
                
                #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 {  
                    use epoll;  
                    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;  
                      
                    #说明:端口必须保持一致
                    upstream servers{  
                    server 120.25.56.93:8080;  
                    server 120.25.58.50:8080;    
                    }  
                    #gzip  on;  
                  
                    server {  
                        listen       8084;  
                        server_name  120.76.112.207;  
                  
                        #charset koi8-r;  
                  
                        #access_log  logs/host.access.log  main;  
                  
                        location / {  
                           proxy_pass  http://servers;  
                       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 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;  
                    #    }  
                    #}  
                  
                }

                    4.重新启动nginx
                    

                    [root@iZ94phz01rnZ sbin]# ./nginx -s reload  
                    [root@iZ94phz01rnZ sbin]# ./nginx -s reopen
                           

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
负载均衡 网络协议 算法
一文读懂什么是Nginx?它能否实现IM的负载均衡?
Nginx(及其衍生产品)是目前被大量使用的服务端反向代理和负载均衡方案,从某种意义上来讲,Nginx几乎是低成本、高负载Web服务端代名词。 如此深入人心的Nginx,很多人也想当然的认为,在IM或消息推送等场景下是否也能使用Nginx来解决负载均衡问题? 另外,即时通讯网的论坛和QQ群里也经常有人问起,Nginx是否能支持TCP、UDP、WebSocket的负载
466 4
|
存储 安全 应用服务中间件
将下载的Nginx证书转换为Tomcat证书格式
好,可以看到,将Nginx证书转换为Tomcat证书的过程就像在烘焙一块蛋糕。你需要准备材料(证书),配备工具(OpenSSL, keytool),按照一定的步骤慎重制作,最后你就拥有了一块可以在浏览器中呈现出漂亮的安全状态的HTTPS蛋糕。就这么简单,明了,没有任何复杂的理论知识,就像在家庭厨房里烘焙的快乐一样。
555 16
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
456 21
|
负载均衡 Java 应用服务中间件
Tomcat与Nginx的负载均衡与动静分离技巧
总的来说,Tomcat和Nginx各有各的优点,在负载均衡和动静分离这两方面它们都有很好的应用。灵活使用这两个工具能够让Web应用具有更好的扩展性和用户体验。
440 14
|
负载均衡 前端开发 JavaScript
LVS-DR模式、keepalived、Nginx与Tomcat合作,打造动静分离,高效负载均衡与高可用性
为了采用这样的架构,你需要对LVS-DR、Keepalived、Nginx与Tomcat有一定的理解和掌握,同时也需要投入一些时间去研究和配置,但是一旦你把它运行起来,你将会发现,这一切都是值得的。
552 11
|
负载均衡 算法 关系型数据库
大数据新视界--大数据大厂之MySQL数据库课程设计:MySQL集群架构负载均衡故障排除与解决方案
本文深入探讨 MySQL 集群架构负载均衡的常见故障及排除方法。涵盖请求分配不均、节点无法响应、负载均衡器故障等现象,介绍多种负载均衡算法及故障排除步骤,包括检查负载均衡器状态、调整算法、诊断修复节点故障等。还阐述了预防措施与确保系统稳定性的方法,如定期监控维护、备份恢复策略、团队协作与知识管理等。为确保 MySQL 数据库系统高可用性提供全面指导。
|
负载均衡 算法 应用服务中间件
Nginx长连接负载均衡详细说明以及案例
本文详细介绍了Nginx长连接负载均衡的配置与原理。长连接(Keepalive)允许客户端和服务器保持连接,减少建立和关闭连接的开销。Nginx支持多种负载均衡算法,如轮询、IP哈希等。通过在Nginx配置文件中使用`upstream`模块和`keepalive`指令,可以实现长连接负载均衡,从而提高系统的性能和响应速度。示例配置展示了如何设置后端服务器组、长连接数及HTTP/1.1协议,确保连接复用,降低延迟。
877 5
|
负载均衡 应用服务中间件 nginx
如何使用nginx实现负载均衡?
如何使用nginx实现负载均衡?
|
缓存 负载均衡 NoSQL
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
298 1