博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ubuntu服务器 安装nginx 为tomcat实现负载均衡
阅读量:3645 次
发布时间:2019-05-21

本文共 3534 字,大约阅读时间需要 11 分钟。

一: 安装Nginx依赖库

#安装gcc g++的依赖库apt-get install build-essentialapt-get install libtool#安装 pcre依赖库sudo apt-get updatesudo 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 startor$ sudo /etc/init.d/apache2 start二、 Restart Apache 2 Server /重启apache服务# /etc/init.d/apache2 restartor$ sudo /etc/init.d/apache2 restart三、Stop Apache 2 Server /停止apache服务# /etc/init.d/apache2 stopor$ sudo /etc/init.d/apache2 stop

参考:

apache 常用命令: 

四: 配置负载均衡:

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 就能访问    nginx欢迎页; 

   负载均衡得到的tomcat欢迎页

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

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

 

 

转载地址:http://mneyn.baihongyu.com/

你可能感兴趣的文章
物联网之智能灯-Django(一)
查看>>
使用计算机视觉技术进行工业品质检测
查看>>
Java重要知识点——方法的定义
查看>>
LinkedHashSet的使用
查看>>
JS 整数与罗马数字相互转换(1~3999)
查看>>
JUC - 阻塞队列:
查看>>
JUC - 线程池:
查看>>
JUC - Java8流式编程
查看>>
MySQL - 高级部分:
查看>>
JavaWeb框架 - Spring注解部分:
查看>>
SpringBoot使用外部的Tomcat: bean with name 'defaultValidator' defined in class path resource
查看>>
SpringBoot上传文件413问题:
查看>>
Java多线程 - AQS简单实现:
查看>>
建造者模式:
查看>>
适配器模式:
查看>>
LinkedList源码分析
查看>>
美团Java一面面经
查看>>
疏漏总结(九)——http与https
查看>>
疏漏总结(十)
查看>>
线程池
查看>>