1.反向代理
安装:
环境准备,执行yum update升级Linux的软件内核,再执行下面的命令添加依赖:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
下载nginx的tar包
http://nginx.org/en/download.html
将包传到linux下的usr下的src中,可以使用SSH工具连接Linux系统进行传输
打开Linux的终端控制台,输入tar -xvf nginx的包名解压
进入解压后的目录,和解压包的名字一样,但是是文件夹,进入后执行./configure
执行make && make install
执行下面的命令
[root@localhost nginx-1.14.2]# cd /usr
[root@localhost usr]# cd local
[root@localhost local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@localhost local]# cd nginx/
[root@localhost nginx]# ls
conf html logs sbin
[root@localhost nginx]#
[root@localhost nginx]# cd sbin
[root@localhost sbin]# ./nginx
[root@localhost sbin]#
此时nginx启动起来了,执行下面命令查看nginx的运行情况
[root@localhost sbin]# ps -ef | grep nginx
Linux查看已开放的端口号:firewall-cmd --list-all
对外开放访问端口:firewall-cmd --add-port=8080/tcp --permanent
重启防火墙:firewall-cmd -reload
实例1:
需要实现的效果:
访问Linux的域名和端口(如192.168.17.129:80),由Nginx代理访问到部署到Linux上的项目(127.0.0.1:8080)
配置文件在Linux的local/nginx/conf文件夹中下的nginx.conf,可以看我上面执行安装命令时的目录结构的标红部分
配置Nginx配置文件找到配置文件中如图所示位置:
按照我的Linux主机的IP地址此时应该访问192.168.17.129:80端口就可以访问相对应的127.0.0.1:8080端口的应用,如Tomcat服务器,如果访问你的Linux上的项目,请自己按需配置自己的Linux的IP地址和端口,请开放相应的端口,或者关闭防火墙.
实例2:
需要实现的效果:
访问http://192.168.17.128:9001/edu/ 直接跳转到127.0.0.1:8080
访问http://192.168.17.128:9001/vod/ 直接跳转到127.0.0.1:8081
此时可以访问通过一个端口访问到两个项目。
步骤:1、服务器中部署两个项目,项目的端口号为8080和8081
配置配置文件
重启Nginx
记得开放对外访问的端口9001
2.负载均衡
实现效果:
浏览器地址输入http://192.168.17.128/edu/hello.html时实现负载均衡,将访问自动分配到不同的端口中。
负载均衡有4种分配策略:
轮询的配置方式:
weight
hash:
fair: