注册 X
提交 注:点击提交后系统会发送邮件到邮箱验证!(仅支持中国大陆邮箱)
我已阅读并同意 服务条款
首页 > IT技术笔记 > 查看笔记

Linux配置Nginx+Tomcat负载均衡

cd /usr/local/tomcat1/webapps/ROOT/

tar -zxvf nginx-1.14.2.tar.gz -C /usr/local

一、Linux配置Nginx

一、下载Nginx

  方式1:从http://nginx.org/en/download.html上下载稳定版,解压安装

1527293-20181210193825241-631799412.png

方式2:直接在Linux上用命令下载: wget http://nginx.org/download/nginx-1.10.2.tar.gz
  -bash: wget: command not found

  安装wget:
  yum -y install wget

  再执行下载nginx

二、解压安装包   

  tar -zxvf nginx-1.14.2.tar.gz -C /usr/local

      cd /usr/local/nginx-1.10.2

生成Makefile文件

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

编译源码
make

安装
make install

   2、安装相关组件

  • yum install -y pcre pcre-devel

  • yum install -y zlib zlib-devel

  • yum install -y openssl openssl-devel

  • 1527293-20181210202315710-164305067.png

  • 这是提示缺少c++环境 ,用 yum install gcc-c++ 安装一下,再执行 ./configure,然后又报错了:

1527293-20181210203251955-2019281836.png

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.

没装伪静态模块需要pcre库
解决方法:
yum install -y pcre pcre-devel

还有可能出现:
错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.

解决办法:
yum -y install openssl openssl-devel

最后 ./configure

1527293-20181210204039871-813309100.png

执行make 编译:

1527293-20181210204143065-1899061903.png

生成Makefile文件

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

编译源码
make

安装
make install

这里不必要太纠结,只要 /usr/local/ 下出现了 /nagix文件就ok,进入 cd /usr/local/nginx/sbin 下,启动 ./nginx

问题1:出现端口占用,nginx一般是80端口,要么把其他的kill掉,要么更改nginx的端口

1、kill掉其他的之前,要知道哪个占用了:用 netstat -anp | grep 80可以查看,这里是之前的lamp占用了


2、我们可以修改nginx自身的监听端口,vi /usr/local/nginx/conf/nginx.conf  ,将listen 80,改为自己要的就行


实现端口转发

当我们在服务器上搭建一个图书以及一个电影的应用,其中图书应用启动了 8001 端口,电影应用启动了 8002 端口。此时如果我们可以通过

localhost:8001    //图书localhost:8002    //电影

 

但我们一般访问应用的时候都是希望不加端口就访问域名,也即两个应用都通过 80 端口访问。但我们知道服务器上的一个端口只能被一个程序使用,这时候如何该怎么办呢?一个常用的方法是用 Nginx 进行端口转发。Nginx 的实现原理是:用 Nginx 监听 80 端口,当有 HTTP 请求到来时,将 HTTP 请求的 HOST 等信息与其配置文件进行匹配并转发给对应的应用。例如当用户访问 book.douban.com 时,Nginx 从配置文件中知道这个是图书应用的 HTTP 请求,于是将此请求转发给 8001 端口的应用处理。当用户访问 movie.douban.com 时,Nginx 从配置文件中知道这个是电影应用的 HTTP 请求,于是将此请求转发给 8002 端口的应用处理。一个简单的 Nginx 配置文件(部分)如下面所示:

 

#配置负载均衡池
#Demo1负载均衡池
upstream book_pool{
    server 127.0.0.1:8001;
}
#Demo2负载均衡池
upstream movie_pool{
    server 127.0.0.1:8002;
}

#Demo1端口转发
server {
    listen       80;
    server_name  book.chanshuyi.com;
    access_log logs/book.log;
    error_log logs/book.error;
    
    #将所有请求转发给demo_pool池的应用处理
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://book_pool;    }
}
#Demo2端口转发
server {
    listen       80;
    server_name  movie.chanshuyi.com;
    access_log logs/movie.log;
    error_log logs/movie.error;
    
    #将所有请求转发给demo_pool池的应用处理
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://movie_pool;    }
}


上面这段配置实现了:

1、当用户访问的域名是:http://book.chanshuyi.com 时,我们自动将其请求转发给端口号为 8001 的 Tomcat 应用处理。

2、当用户访问的域名是:http://movie.chanshuyi.com 时,我们自动将其请求转发给端口号为 8002 的 Tomcat 应用处理。

上面的这种技术实现就是端口转发。端口转发指的是由软件统一监听某个域名上的某个端口(一般是80端口),当访问服务器的域名和端口符合要求时,就按照配置转发给指定的 Tomcat 服务器处理。我们常用的 Nginx 也有端口转发功能。


	        
			
	      

 打赏        分享



评论

邮箱: 昵称:
\n