nginx [engine x]是一个HTTP和反向代理服务器,一个邮件代理服务器和一个通用的TCP/UDP代理服务器,最初由Igor Sysoev编写。

环境:

Ubuntu16.04

安装nginx需要的库:pcre,zlib,openssl

 sudo apt-get install libpcre3 libpcre3-dev  #perl 5的库,支持正则表达式

 sudo apt-get install zlib1g-dev #使支持对http包进行gzip压缩

 sudo apt-get install openssl libssl-dev #使支持https

接着,解压缩nginx源码包,进行三部曲:./configure ----》make ----》sudo make install

启动nginx:cd /usr/local/nginx/sbin ----》sudo ./nginx (默认配置文件:/usr/local/nignx/conf,可以指定配置文件启动:-c filelocation)

查看nginx:ps aux | grep nginx

停止nginx:sudo ./nginx -s quit

修改配置文件后重新加载:sudo ./nginx -s reload

浏览器访问:ip:80

nginx ----> nginx配置/反向代理/负载均衡-LMLPHP

应用:

 1     server {
 2         listen       80;
 3         server_name  localhost;
 4
 5         location / {
 6             #将请求与我们定义的服务器进行映射
 7             proxy_pass http://localhost:8080/loginForm;  //分号不能少
 8             #root   html;
 9             #index  index.html index.htm;
10         }
11
12         error_page   500 502 503 504  /50x.html;
13         location = /50x.html {
14             root   html;
15         }
16
17
18     }

浏览器输入:http://localhost/ ----> http://localhost:8080/loginForm

这样就实现了反向代理

备注:测试项目使用spring boot+mybatis

1   # server外部使用关键字upstream 定义服务器集群,服务器集群名字取为test
 2     upstream test{
 3         server localhost:8080;
 4         server localhost:8081;
 5     }
 6
 7     server {
 8         listen       80;
 9         server_name  localhost;
10
11         location / {
12
13             #将定向的路径映射到服务器集群上
14             proxy_pass http://test/loginForm;
15
16             #root   html;
17             #index  index.html index.htm;
18         }
19
20         error_page   500 502 503 504  /50x.html;
21         location = /50x.html {
22             root   html;
23         }
24
25     }

浏览器输入(交替输入):http://localhost/ ----> http://localhost:8080/loginForm 或者 http://localhost:8081/loginForm

这样就实现了负载均衡

备注:测试项目使用spring boot+mybatis(项目是一样的,改动appliaction.properties,将端口号改为server.port= 8081 )

nginx与tomcat集群实现负载均衡,参考博客https://www.cnblogs.com/gcjava/p/6601293.html

主要配置点:

1、修改tomcat 的三处端口

2、nginx的配置

3、复制两份tomcat

note:

v1.1

增加了 nginx与tomcat集群实现负载均衡

04-19 17:41
查看更多