ubuntu服务器 安装nginx 为tomcat实现负载均衡

一: 安装Nginx依赖库

#安装gcc g++的依赖库
apt-get install build-essential
apt-get install libtool

#安装 pcre依赖库
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

#安装 zlib依赖库
apt-get install zlib1g-dev

#安装 ssl依赖库
apt-get install openssl

二: 安装nginx 

#下载最新版本:
wget http://nginx.org/download/nginx-1.11.3.tar.gz
#解压:
tar -zxvf nginx-1.11.3.tar.gz
#进入解压目录:
cd nginx-1.11.3
#配置:
./configure --prefix=/opt/nginx   #nginx 安装在/opt/nginx下
#编辑nginx:
make
注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
#安装nginx:
sudo make install
#启动nginx:
sudo /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx

# nginx 常用命令:
启动nginx:  ./sbin/nginx 
关闭nginx:
./sbin/nginx -s stop
./sbin/nginx -s quit
重新加载配置:
./sbin/nginx -s reload
指定配置文件启动:
./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
查看nginx 版本: ./sbin/nginx -v

三: 启动nginx时 会报80 端口已经被apache 占用,需要解决apache 和nginx端口80冲突的问题: 

方案: 修改apache 的监听端口为8080, nginx还是监听80端口

vim etc/apacpe2/ports.conf    将Listen 80--->改为: Listen 8080

同时在nginx中配置,将http://www.aliserver.com/apache 的请求转发到 http://127.0.0.1:8080 上, 访问apache服务器

apache服务 常用命令
一、Start Apache 2 Server /启动apache服务
# /etc/init.d/apache2 start
or
$ sudo /etc/init.d/apache2 start
二、 Restart Apache 2 Server /重启apache服务
# /etc/init.d/apache2 restart
or
$ sudo /etc/init.d/apache2 restart
三、Stop Apache 2 Server /停止apache服务
# /etc/init.d/apache2 stop
or
$ sudo /etc/init.d/apache2 stop

参考:

https://blog.csdn.net/qq_38483191/article/details/80198913

https://www.jb51.net/article/78921.htm

apache 常用命令: https://www.cnblogs.com/supe/p/8010612.html

四: 配置负载均衡:

nginx.conf 配置:


#user  nobody;
worker_processes  1;

#全局错误日志及PID文件
#error_log  logs/error.log;
error_log  logs/error.log  warn;   # warn 日志级别
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024; #单个后台worker process进程的最大并发链接数
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
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;
    include /opt/nginx/conf.d/*.conf;
}
# http{}中原有的server 注释掉了,,将所有的配置都放在/opt/nginx/conf.d/*.conf

配置 conf.d/tools.conf

upstream tomcat {
      #ip_hash;
     server 127.0.0.1:8081 weight=5;#负载均衡 
     server 127.0.0.1:8082 weight=6;
     }


server {
        listen 80;
        server_name www.aliserver.com;vim #这个域名要在 客户端机器上配置hosts

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ^~ /apache/ {
                proxy_pass http://127.0.0.1:8080/;
         }

        location ^~ /tomcat/ {
                proxy_pass http://tomcat/;
         }

}

 nginx配置完后,此时在阿里服务器 curl http://www.aliserver.com/  是访问不了的,,因为找不到相应的域名,需要在 vim /etc/hosts添加如下:

127.0.0.1       www.aliserver.com

这样在阿里服务器就能curl http://www.aliserver.com/  成功了,

此外还需要在本地mac 上配置hosts  也是修改 vim /etc/hosts 添加:

服务器的外网ip www.aliserver.com

这样 本地mac 就能访问 http://www.aliserver.com/   nginx欢迎页; 

 http://www.aliserver.com/tomcat/  负载均衡得到的tomcat欢迎页

过了一天发现域名不能访问了,, 原因: 域名和公网VIP是分开购买的,阿里云会给你一个公网IP。如果只是开发访问的话,IP+端口就可以访问; 如果自己私自设一个域名, 通过域名访问,可以不加端口号就能访问80/443端口,但是一段时候后会被扫描到,就会被禁止访问,要求去备案,为了防止这种情况发生,需要去申请域名和备案. 所以为了让客户访问方便(不必记ip)只通过域名访问,就需要进行域名的申请和备案.

哎?,只能通过ip 访问nginx 然后转发,再访问各个服务了..

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值