Nginx的安装配置和tomcat负载均衡

Nginx简介

  • 什么是nginx?

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

  • nginx的应用场景

1、http服务器。nginx是一个http服务可以独立提供http服务。可以作网页静态服务器。

2、虚拟主机。可以实现在一台服务器上虚拟出多个网站。

3、反向代理,负载均衡。当网站的访问量达到一定的程度后,单台服务器已经无法满足高并发请求时,需要多台服务器集群使用NGINX做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载过高宕机而使服务器无法使用。

  • nginx安装

官网下载:http://nginx.org/

由于nginx是使用C语言编写,官方只提供了源码。所以需要提前配置好安装环境。

1、安装PCRE

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install pcre pcre-devel -y

2、安装zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install zlib zlib-devel -y

3、安装openssl

OpenSSL 是一个强大的安全(不知道为什么这里被敏感了)套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install openssl openssl-devel -y
  • 安装步骤

1、解压nginx源码包

tar xzvf nginx-1.8..tar.gz

2、使用解压出来的configure可执行文件创建MakeFile文件

./configure --prefix=/usr/local/nginx

3、make && make install

make && make install

如果在安装过程中没有报错,那么恭喜你,nginx已经安装完成。

4、启动nginx

cd /usr/local/nginx/sbin
./nginx
或者
/usr/local/nginx/sbin/nginx
或者指定配置文件启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

5、关闭nginx

./nginx -s stop
但是推荐使用
./nginx -s quit

6、重启nginx

./nginx -s reload

7、访问nginx

在浏览器地址栏输入:你的ip地址就可以看到nginx了

  • 配置虚拟主机

虚拟主机配置有两种方式,一种是域名不同,一种是端口不同

1、端口不同

#如果conf下面没有nginx.conf文件
#将nginx.conf.default复制一份为nginx.conf即可
vim /usr/local/nginx/conf/nginx.conf #user nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ; #gzip on;
#一个server就是一个虚拟主机
server {
########这里的端口是80
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
}
}
server {
########这里的端口是81
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html-;
index index.html index.htm;
}
}
}
这样,同一个地址,不同的端口可以访问不同的网站。 、域名不同 #user nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ; #gzip on; server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
}
}
##################################
####这里的域名为www.yalogs.com######
##################################
server {
listen ;
server_name www.yalogs.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
}
}
########################################
####这里的域名为www.image.yalogs.com######
####这个域名实际上是yalogs的图片服务器的域名##
####但是确是两个不同的网站##################
########################################
server {
listen ;
server_name www.image.yalogs.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
}
}
}

这样不同的域名就可以访问同一个服务器中不同的网站。

  • 反向代理,负载均衡

1、安装两个tomcat,分别运行在8080和8180端口上。

2、启动两个tomcat。

3、反向代理和负载均衡的配置

upstream tomcat1 {
server 192.168.0.10:8080;
server 192.168.0.11:8180 weight=2;
}
server {
listen 80;
server_name www.sina.com.cn; #charset koi8-r; #access_log logs/host.access.log main; location / {
proxy_pass http://tomcat1;
index index.html index.
}
}

上面的wight=2是调整服务器的权重,权重越大,分配到的请求越多。

  • NGINX高可用
    • ​​​​​​​通过使用keepalived+nginx实现(知道有这个东西就OK了)
  • 结束语
    • ​​​​​​​到这里NGINX的安装配置,负载均衡就已经实现了,以后忘了就来看看就OK了,嘿嘿,有没有很机智。。。。。
上一篇:BZOJ 4804: 欧拉心算


下一篇:nginx初级安装配置