少年游

欲买桂花同载酒,终不似,少年游。

0%

NGINX + TOMCAT配置负载均衡

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

CENTOS准备工作

1
2
3
4
5
6
7
8
9
10
yum -y install gcc automake autoconf libtool make gcc-c++

# install pcre
[root@localhost ]# cd /usr/local/
[root@localhost ]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
[root@localhost ]# tar -zxvf pcre-8.21.tar.gz
[root@localhost ]# cd pcre-8.21
[root@localhost ]# ./configure
[root@localhost ]# make
[root@localhost ]# make install

NGINX安装和配置

  • 安装nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # install nginx 
    [root@localhost ]# cd /
    [root@localhost ]# mkdir soft
    [root@localhost ]# cd soft
    [root@localhost ]# wget http://nginx.org/download/nginx-1.10.1.tar.gz
    [root@localhost ]# tar -xzvf nginx-1.10.1.tar.gz
    [root@localhost ]# mv nginx-1.10.1 /usr/local
    [root@localhost ]# cd /usr/local/nginx-1.10.1
    [root@localhost ]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
    [root@localhost ]# make && make install

    —prefix=/usr/local/nginx 代表mginx安装目录

  • 开放端口
    由于CENTOS默认开启防火墙,我们需要手动开放端口。

    1
    [root@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  • 配置文件

    1
    2
    [root@localhost ]# cd nginx/
    [root@localhost ]# vim conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
user  root paul;                                                                                        
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;

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

gzip on;
# upstream tomcatCluster{
# server 127.0.0.1:8080 weight=1;
#}

upstream tomcatCluster { # (1).随便命名
# 可设置多个tomcat服务器 转发权重
server 127.0.0.1:8080 weight=1;
# server 127.0.0.1:8081 weight=2;
}

server {
listen 80;
# (3)localhost可以不改
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

#location / {
# root html;
# index index.html index.htm;
#}
location / {
#(2)对于上面的(1)处的名字
proxy_pass http://tomcatCluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect default ;
proxy_redirect default;
}
#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;
}

location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcatCluster;
}

location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma
{ expires 15d; }

location ~ .*.(js|css)?$
{ expires 1h; }

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

TOMCAT

  • 安装tomcat

    1
    2
    3
    4
    5
    6
    7
    # install tomcat
    [root@localhost ]# cd /soft
    [root@localhost ]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.70/bin/apache-[root@localhost ]# tomcat-7.0.70.tar.gz
    [root@localhost ]# tar xzvf apache-tomcat-6.0.41.tar.gz
    [root@localhost ]# mv apache-tomcat-6.0.41/ /usr/local
    [root@localhost ]# cd /usr/local/apache-tomcat-6.0.41
    [root@localhost ]# sh bin/startup.sh
  • 开放端口
    如果修改了conf/server.xml文件启动端口,则相应的nginx.conf和下面的8080都要改变

    1
    [root@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
  • 如果出现下面的信息代表启动成功
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Using CATALINA_BASE:   /usr/local/apache-tomcat-7.0.70
    Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.70
    Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.70/temp
    Using JRE_HOME: /
    Using CLASSPATH: /usr/local/apache-tomcat-7.0.70/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.70/bin/tomcat-juli.jar
    Using CATALINA_PID: /usr/local/apache-tomcat-7.0.70/logs/tomcat.pid
    Existing PID file found during start.
    Removing/clearing stale PID file.
    Tomcat started.

基本命令

1
2
3
4
5
6
[root@localhost ]# ./sbin/nginx -s reload  # 重启nginx
[root@localhost ]# vi [filename] # 新建文件
[root@localhost ]# > [filename] # 清空文件内容
[root@localhost ]# ps -ef|grep tomcat #查询进程
[root@localhost ]# kill -9 [pid] #杀死进程
[root@localhost ]# chmod +xw [filename] # 新增执行和写入权限