`
shmayl
  • 浏览: 36995 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

RedHat Linux AS 5下Nginx的安装及Nginx+Tomcat负载均衡配置

阅读更多

一、软件准备

zlib-1.2.5.tar.gz[支持gzip],下载地址:http://www.zlib.net/

pcre-8.10.tar.gz[支持rewrite module],下载地址:http://sourceforge.net/projects/pcre/files/

openssl-1.0.0a.tar.tar[支持ssl],下载地址:http://www.openssl.org/source/

下载LATEST版本

nginx-0.8.53.tar.gz,下载地址:http://nginx.org/en/download.html

二、安装过程

由于ssl暂时没有用到,所以没有安装

1、解压缩相关软件

[root@localhost tools]# tar zxvf zlib-1.2.5.tar.gz

[root@localhost tools]# tar zxvf pcre-8.10.tar.gz

[root@localhost tools]# tar zxvf nginx-0.8.53.tar.gz

2、具体安装

[root@localhost tools]# cd nginx-0.8.53

[root@localhost nginx-0.8.53]# ./configure --prefix=/opt/nginx

--with-http_realip_module --with-http_sub_module --with-http_flv_module

--with-http_dav_module –with-http_gzip_static_module --with-http_stub_status_module

--with-http_addition_module --with-pcre=/opt/tools/pcre-8.10 --with-zlib=/opt/tools/zlib-1.2.5

[root@localhost nginx-0.8.53]#make

[root@localhost nginx-0.8.53]#make install

三、Nginx+Tomcat负载均衡配置

  1、架构描述:

     前端一台Nginx服务器做负载均衡,后端放两台[当然可以多台]tomcat服务器,通过

Nginx转发到后面tomcat服务器,并且做动静分离

Nginx服务器IP:192.168.11.197

Tomcat01服务器IP:192.168.11.191

Tomcat02服务器IP:192.168.11.192

 

2、修改Nginx配置文件

[root@localhost conf]# vi nginx.conf

#运行nginx所在的用户名和用户组

user  nobody nobody; 

#运行CPU个数,可以按照实际服务器来计算

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;

    include /opt/nginx/conf/proxy.conf;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

   #设定请求缓冲

   client_header_buffer_size 1k;

   large_client_header_buffers 4 4k;

   #设定可以使用gzip相关参数

   gzip on;

gzip_comp_level 7;

需要压缩的最小长度

   gzip_min_length 1100;

   gzip_buffers 4 8k;

   #指定需要压缩的文件类型

   gzip_types  text/plain application/javascript text/css text/xml

   gzip_types text/plain;

   output_buffers 1 32k;

   postpone_output 1460;

   #设定访问日志

   access_log  logs/access.log  main;

   client_header_timeout 3m;

   client_body_timeout 3m;

   send_timeout 3m;

   sendfile on;

   tcp_nopush on;

   tcp_nodelay on;

 

 

   # sendfile        on;

   #tcp_nopush     on;

 

   #keepalive_timeout  0;

   #keepalive_timeout  65;

 

   #gzip  on;

   upstream tomcat_server{

     #设定转向Serverweight代表优先级,优先级高的先访问

     server  192.168.11.191:8080 weight=1;

     server  192.168.11.192:8080 weight=2;

    }

    server {

        listen       80;

        server_name  localhost;

        charset gbk;

        access_log  logs/host.access.log  main;

        location / {

            root /opt/www/root

            index  index.html index.htm index.jsp;

            proxy_pass http://tomcat_server;

        }

#设定查看Nginx状态的地址(非默认安装模块,需要在编译时加上

--with-http_stub_status_module)  

location /status {  

    stub_status            on;  

    access_log            on;  

    auth_basic            "status";  

    auth_basic_user_file     conf/passwd;  

}

#访问http://192.168.11.197/status会提示输入账号

#htpasswd用法

#首先在conf/目录下建立passwd文件,具体命令:touch passwd

#htpasswd –cb passwd user password

 

        #css|js|ico|gif|jpg|jpeg|png|txt|html|htm|xml|swf|wav这些都是静态文件,

#但应分辨,jscss可能经常会变,过期时间应小一些,图片、

html基本不变,过期时间可以设长一些  

location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm)$ {  

    root         /opt/www/root;  

    access_log   off;  

    expires      30d;  

}  

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {  

    root         /opt/www/root;   

    access_log   off;  

    expires      24h;  

}  

#注:location不包括?后面带的参数,所以以上正则可以

#匹配http://192.168.11.197/image/sxxx.jpg?a=xxx

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

    #    }

    #}

 

}

conf目录下创建proxy.conf文件,文件内容为

proxy_redirect off;

proxy_set_header Host $host;

#获取真实IP

proxy_set_header X-Real-IP $remote_addr;

#获取代理者的真实IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;

client_body_buffer_size 128k;

proxy_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

 

3、测试Nginx

测试nginx.conf文件是否正确

[root@localhost sbin]# ./nginx -t -c conf/nginx.conf

the configuration file /opt/nginx/conf/nginx.conf syntax is ok

configuration file /opt/nginx/conf/nginx.conf test is successful

Nginx启动

[root@localhost sbin]# ./nginx -c /opt/nginx/conf/nginx.conf

 

查看是否启动成功

[root@localhost sbin]# ps aux|grep nginx |grep -v grep

root      4612  0.0  0.0   3792   472 ? Ss   Nov01   0:00 nginx: master process ./nginx

nobody    4613  0.0  0.1   4144  1284 ?    S    Nov01   0:05 nginx: worker process

 

出现上述两行代表启动成功

输入http://192.168.11.197/测试下效果吧

 

 

 

4Nginx启动、关闭脚本

#!/bin/sh  

#  

# description: Starts, stops nginx  

#  

#chkconfig: 2345 20 80 

#dscription: Startup script for nginx webserver on CentOS. Place in /etc/init.d   

#  

# Author: Touya  

set -e  

 

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/nginx  

DESC="nginx daemon" 

NAME=nginx  

DAEMON=/opt/nginx/sbin/$NAME  

CONFIGFILE=/opt/nginx/conf/nginx.conf  

PIDFILE=/opt/nginx/log/$NAME.pid  

SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.  
test -x $DAEMON || exit 0 
 
d_start() {  
echo "Starting $DESC: $NAME" 
$DAEMON -c $CONFIGFILE || echo "already running" 
}  
 
d_stop() {  
echo "Stopping $DESC: $NAME" 
test -f $PIDFILE && kill -QUIT `cat $PIDFILE`  
}  
 
d_reload() {  
echo "Reloading $DESC configuration…" 
kill -HUP `cat $PIDFILE` || echo "can’t reload" 
}  
case "$1" in  
'start')  
    d_start  
    echo "started." 
;;  
'stop')  
    d_stop  
    echo "stoped." 
;;  
'reload')  
    d_reload  
    echo "reloaded." 
;;  
'restart')  
    echo "Restarting $DESC: $NAME ..." 
    d_stop  
    # One second might not be time enough for a daemon to stop,  
    # if this happens, d_start will fail (and dpkg will break if  
    # the package is being upgraded). Change the timeout if needed  
    # be, or change d_stop to have start-stop-daemon use --retry.  
    # Notice that using --retry slows down the shutdown process somewhat.  
    sleep 3 
    d_start  
    echo "done." 
;;  
'list')  
    ps auxf | egrep '(PID|nginx)' | grep -v grep  
;;  
'test')  
    $DAEMON -t -c $CONFIGFILE  
;;  
*)  
echo "Usage: $SCRIPTNAME {reload|list|test|start|stop|restart}" >&2 
exit 3 
;;  
esac  
exit 0 
保存文件nginx ,拷贝到/etc/init.d下,并chmod  +x  /etc/init.d/nginx
[root@localhost init.d]# chkconfig --list nginx
service nginx supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add nginx')
增加后台服务
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]#
接下可以用service nginx start|restart|stop来操作你的nginx服务器(restart时重新读入config)

 

分享到:
评论

相关推荐

    linux_apache+tomcat负载均衡安装和调试.pdf

    《Linux Apache + Tomcat 负载均衡安装与调试指南》 在当今互联网环境中,为了保证服务的高可用性和性能,负载均衡成为了必备的技术。本文将详细介绍如何在Linux(以RedHat Linux 5.0为例)系统上安装Apache HTTP...

    Nginx实现tomcat与weblogic集群的负载均衡及故障处理

    本文将详细讲解如何使用Nginx作为负载均衡器,实现对Tomcat和WebLogic集群的负载均衡以及故障处理。我们将遵循由浅入深的原则,适合初学者和进阶者学习。 首先,我们来看看基础环境。系统为Redhat7.5,JDK版本为1.8...

    nginx1.9.3+tomcat7+redis3.2.6集群session共享

    亲测在redhat6.5上部署成功--包含详细步骤,这里linux安装、网络配置、jdk安装、环境变量配置和防火墙配置都略过去了,

    linux下apache+tomcat实现集群

    在Linux环境下,构建Apache与Tomcat的集群是提高Web服务可扩展性和高可用性的重要手段。这个集群配置通常涉及到负载均衡、故障转移以及资源优化。本文将深入探讨如何在Red Hat 5操作系统上实现这样的集群配置。 ...

    nginx-1.24.0 binary runnable package for redhat 7.9 (linux)

    Nginx 可作为反向代理服务器,将来自客户端的请求转发到后端的应用服务器,如 Apache、Tomcat 等,实现负载均衡和提高响应速度。 **八、静态文件处理** Nginx 对于静态文件(如 HTML、CSS、JavaScript、图片等)的...

    Linux下配置Tomcat日志分析

    以下是如何在Linux系统,特别是Redhat发行版上配置Tomcat日志以使用awstats进行分析的详细步骤。 首先,我们需要确保系统已经安装了Apache Tomcat服务器。如果尚未安装,可以使用如下命令进行安装: ```bash sudo ...

    linux与阿里云服务器+nginx.docx

    【描述】:本课程专注于Linux操作系统在阿里云服务器上的应用,以及Nginx的配置和管理,旨在帮助开发者和运维人员快速掌握在Linux环境下部署项目和利用Nginx进行高效服务管理。 【标签】:“Linux”,“阿里云”,...

    Nginx编译安装整套工具,亲测可用于centos Redhat

    本文将详细介绍如何在CentOS或Redhat系统上,编译安装Nginx以及其依赖的库,如zlib、perl、pcre和openssl,同时也会提及与之相关的Redis、Java(JDK 8)和Tomcat的安装。 首先,让我们从Nginx开始。Nginx以其事件...

    nginx.1.23.el6

    2. **反向代理**:作为反向代理服务器,Nginx 可以接收来自客户端的请求,并转发到后端的应用服务器(如 Apache、Tomcat),提高了系统负载均衡和容错能力。 3. **负载均衡**:Nginx 可以根据预设策略(如轮询、最少...

    nginx服务部署1

    5. **异步网络I/O**:Nginx采用epoll(Linux 2.6+)或kqueue(FreeBSD)模型,提供高效的网络I/O操作,相比之下,Apache的select模型效率较低。 Nginx相对于Apache的优势在于其异步非阻塞的网络I/O模型,这就像宿...

    apache-tomcat-8.5.51.tar.gz

    关于与Nginx的集成,Nginx作为一个高性能的反向代理和负载均衡服务器,常被用来分发请求到多个后端服务器,如Tomcat实例。通过Nginx的反向代理功能,可以隐藏Tomcat的实际IP和端口,提高安全性,同时利用Nginx的缓存...

    深入剖析Tomcat.pdf

    - Apache、Nginx是另外两款广泛使用的Web服务器,与Tomcat搭配使用可以实现更复杂的部署和负载均衡策略。 - Python、Java、C语言是编写Web应用和服务端逻辑的常用编程语言。 - Hadoop、OpenStack、集群等技术提供...

    大学生实习报告范文Linux运维实习.docx

    3. **应用服务部署**:在Linux环境下,熟悉并能部署常见的Web服务器如Apache、Tomcat和Nginx,这些是支撑互联网服务的基础。特别地,将应用程序成功部署在Tomcat上,确保其正常运行。 4. **系统优化与管理**:学习...

    2022 年linux计算机专业大学生实习报告.docx

    3. 在 linux 下熟识应用项目的部署,包括 apache、tomcat、nginx 等服务器的部署,并且把应用项目胜利部署在 tomcat 服务器。 4. 熟识应用项目的负载均衡配置、日志切割、数据备份等一些服务器优化软件和工具,为...

    企业级Tomcat部署实践及安全调优1

    - **负载均衡**:通过Nginx或Apache HTTPD分发请求到多个Tomcat实例,提升整体处理能力。 总结,企业级Tomcat部署不仅涉及基础的安装和配置,还需要关注安全性和性能优化。通过对JDK的合理使用和对Tomcat的深入理解...

    linux计算机专业实习报告范文.doc

    3. **Linux应用与系统运维**:掌握Redhat Linux服务器的安装与配置,能够独立完成批量服务器系统的安装。同时,学习RAID和远程管理卡,以及磁盘阵列的部署,提升系统运维能力。 4. **服务器软件部署**:熟练部署...

    关于计算机2020实习报告热门篇.doc

    - 应用部署:掌握在Linux下部署Apache、Tomcat、Nginx等服务器软件的方法。 - 服务器优化:进行应用项目的负载均衡配置,日志切割,以及数据备份等优化操作。 - 数据库管理:学习Oracle数据库,了解其在项目中的...

    Linux安装手册

    实时发布最新Linux资讯,包括Linux、Ubuntu、Fedora、 RedHat、红旗Linux、Linux教程、Linux认证、SUSE Linux、Android、Oracle、Hadoop、CentOS、 MySQL、Apache、Nginx、Tomcat、Python、Java、C语言、OpenStack、...

Global site tag (gtag.js) - Google Analytics