`
shmayl
  • 浏览: 36036 次
  • 性别: 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)

 

分享到:
评论

相关推荐

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

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

    linux下apache+tomcat实现集群

    linux下apache+tomcat实现集群,压缩包里包括两篇文档(操作步骤和详解),3个压缩包(安装文件)

    nginx tomcat集群 session复制

    jdk-1_5_0_22-linux-i586.bin #mkdir –p /usr/local/soft #将以上软件上传到soft目录下 1.2 server环境 redhat5.4 nginx server:192.168.10.2:80 tomcat1:192.168.10.2:8080 tomcat2:192.168.10.2:8180 2. 安装...

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

    nginx zlib perl pcre openssl redis jdk 8 tomcat 可安装于centos6.0以上版本

    apache-tomcat-8.5.51.tar.gz

    刚下咋tomcat.tar.gz源码安装包适用于centos Redhat系列的安装包需要别的可以留言建议搭建nginx+mysql+tomcat nginx用于转发技术不懂可以留言

    Linux安装手册

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

    Nginx Web Server 1.1.4 RPM for CentOS 5.5

    整合了nginx的nginx的1.1.41.1.4干线与nginx的AJP模块。 与Tomcat的整合可带来更好的效能。

    LNMT-master.zip

    LNMT一键安装包 – CentOS/RedHat下自动编译安装Nginx,JRE,Tomcat,MySQL

    微网快站自助建站系统

    您还可以通过与Nginx、Apache等Web服务器集成后实现服务器集群与负载均衡,以支持大规模的并发访问。同时,本系统的软件架构设计考虑到了功能的可扩展性,并不局限于一个相对简单的新闻类型的信息发布网站,您还可以...

    微网快站自助建站系统1.0

    微网快站自助建站系统旨在帮助各类单位、企业、...下载的安装包中已经集成了Tomcat 8.0.46,您只需要自行安装并配置好jdk和MySQL,运行数据库脚本创建数据库,然后修改安装包中相应的MySQL的连接账号信息即可运行起来。

    javapms门户网站源码

    操作系统:Microsoft Windows 2000/XP/2003/Vista、Redhat Linux、CentOs、Suse Linux、AIX、Solaris、HP-Unix等 数据库:Oracle、DB2、SQL Server、Mysql等 WEB 服务器系统:Tomcat-7.0.23、Nginx、apache、weblo等...

    oneinstack:OneinStack-一个PHPJAVA部署工具

    此脚本是使用Shell编写的,以便快速部署适用于CentOS 6的LEMP / LAMP / LNMP / LNMPA / LTMP (Linux,Nginx / Tengine / OpenResty,生产环境中MySQL / MariaDB / Percona,PHP,JAVA) 〜8(包括redhat),Debian ...

Global site tag (gtag.js) - Google Analytics