nginx是一款由俄罗斯人开发的web服务器/反向代理服务器/email服务器/负载均衡器软件,并在一个BSD-like 协议下发行,目前比较流行,最初近适用于Linux平台,目前已有Windows平台版本,优点是资源消耗少、并发处理能力强。
具体的使用的场景参考:Nginx官方参考文档。
具体平台版本见:http://nginx.org/packages/
实验环境:
1,Win7(192.168.84.1)加虚拟机CentOS7(192.168.84.128)
2,Win7下tomcat:apache-tomcat-7.0.68,CentOS7下tomcat:apache-tomcat-7.0.70
3,CentOS7中安装Nginx版本:nginx-1.10.1
CentOS7中安装nginx-1.10.1:
我下载的是源码编译版:nginx-1.10.1.tar.gz,编译安装稳定版需要安装好gcc、gcc-c++、openssl-devel、pcre-devel、zlib-devel,并下载pcre-devel、zlib-devel的源码。
1,解压nginx源码后的目录下,执行:
./configure \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-zlib=/home/merrick/zlib-1.2.8 \
--with-pcre=/home/merrick/pcre-8.38 \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
</pre><p></p><pre>
2,编译,安装:
[root@localhost nginx-1.10.1]# make & make install
3,启动,停止:
启动:/usr/local/nginx/sbin/nginx
停止:查到nginx主进程号后(master进程),执行kill -QUIT 主进程号
配置负载均衡:
1,配置/etc/nginx/nginx.conf(参考https://www.nginx.com/resources/admin-guide/load-balancer/):
worker_processes 1;
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 backend{
server 192.168.84.128:8080 weight=5 max_fails=2 fail_timeout=600s;
server 192.168.84.1:8080 weight=4 max_fails=2 fail_timeout=600s;
}
server {
listen 192.168.84.128:80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
location / {
proxy_pass http://backend;
# root html;
# index index.html index.htm;
}
location /NginxStatus {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
2,启动win7下和centos7下的tomcat
3,在win7下访问centos7的80主页测试几次:
可以看到访问nginx这个代理服务器时会随机转到两个web主页: