nginx实现反向代理负载均衡+tomcat集群

以下均在linux下进行:

第一步:需要安装gcc的环境。

yum install gcc-c++

第二步:nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

 

第三步:nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

 

yum install -y zlib zlib-devel

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

yum install -y openssl openssl-devel

第五步: 上传nginx.tar.gz包解压(我用的是xshell和xftp)

[root@izu5nd0knyspy8z nginx]# tar zxf nginx-1.12.0.tar.gz

 

第六步: 使用configure命令创建一makeFile文件

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

# -- prefix 是安装目录

第七步: 编译 --> 安装

[root@izu5nd0knyspy8z nginx-1.12.2]# make
# 编译完会出现Makefile 文件夹 继续执行下面命令
[root@izu5nd0knyspy8z nginx-1.12.2]# make install
# 安装完毕之后 
[root@izu5nd0knyspy8z nginx-1.12.2]# cd /usr/local/nginx
[root@izu5nd0knyspy8z nginx]# ll
drwxr-xr-x 2 root root 4096 Dec 29 14:24 conf
drwxr-xr-x 2 root root 4096 Dec 29 14:15 html
drwxr-xr-x 2 root root 4096 Dec 29 14:15 sbin 这里就是启动nginx的命令

[root@izu5nd0knyspy8z sbin]# ./nginx  启动
[root@izu5nd0knyspy8z sbin]# ./nginx -s stop 停止
[root@izu5nd0knyspy8z sbin]# ./nginx -s reload 从起

注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

[root@izu5nd0knyspy8z sbin]# mkdir /var/temp/nginx/client -p

至此nginx安装完毕可以启动了 可以根据IP访问 或者 域名访问

进入conf目录 
编辑     vi nginx.conf
# 具体详细配置,大家自己查一下 
# 第一步
   upstream 上面就是我(对应下面){
      ip_hash; # 如果不用这个需要配置session共享(绑定ip) 具体自己查
      server 127.0.0.1:8001; # tomcat的ip:端口 怎么改自己查 很简单
      server 127.0.0.1:8002;
      server 127.0.0.1:8003 weight = 2; # weight 代表权重 越大接受的请求越多, 默认轮训分配
   }
# 第二部(修改就行)
 server {
        listen       80;  # 监听的端口 一般就是服务器的80端口
        server_name  填写域名(多种方式);

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           # 配置反向代理 
	    proxy_pass http://这里和上面对应;
            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;
        }

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

 

 

 

 

转载于:https://www.cnblogs.com/412013cl/p/8145115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值