nginx反向代理_负载均衡
注意ip地址为:
虚拟机ip设置
BOOTPROTO="static"
NAME="enp0s3"
DEVICE="enp0s3"
ONBOOT="yes"
IPADDR="172.18.25.175"
NETMASK="255.255.255.0"
GATEWAY="172.18.25.1"
DNS1=8.8.4.4
DNS2=8.8.8.8
IPV6INIT=no
Nginx简介
Nginx (engine x) 是一个高性能的 HTTP 和反向代理服务,也是一个 IMAP/POP3/SMTP 服务。 其特点是占有内存少,并发能力强,事实上nginx 的并发能力确实在同类型的网页服务器中 表现较好,中国大陆使用 nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。在连接高并发的情况下,Nginx 是 Apache 服务不错的替代品。
Nginx 的主要功能
Nginx安装
安装Nginx环境依赖
- 编译环境
yum install gcc gcc-c++
- Pcre库
yum -y install pcre-devel
- Zlib压缩工具
yum install zlib-devel
本地主机上传nginx安装包
- 服务器新建文件夹
mkdir -p /soldier/app
服务器直接下载
cd /soldier/app
yum install wget
wget http://nginx.org/download/nginx-1.8.1.tar.gz- 或者本地上传
scp nginx-1.8.0.tar.gz 172.18.25.172:/soldier/app/
- 创建安装目录
mkdir /usr/local/nginx
解压安装包
tar zxvf nginx-1.8.1.tar.gz
Nginx参数配置并安装
- 进入解压好的Nginx目录
cd /soldier/app/nginx-1.8.1
- 配置参数
#新建nginx安装目录
mkdir /usr/local/nginx直接复制粘贴执行
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi直接复制粘贴执行
- 执行make编译
make
- 执行make install安装
make install
- 查看安装效果
ll /usr/local/nginx/
配置防火墙的80端口
- 修改
vi /etc/sysconfig/iptables
- 添加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
- 重启
service iptables restart
- 或者直接关闭防火墙
systemctl stop firewalld
启动Ngxin
- 进入Nginx安装目录的sbin目录
cd /usr/local/nginx/sbin/
- 启动
./nginx
- 重启命令
./nginx -s reload
一、Nginx 实现虚拟主机
应用场景:
当多个系统需要部署的时候,有系统访问很小,为了节省成本,就需要将多个系统部署到同一台服务器上,怎么在同一台服务器上,完成不同系统的部署和访问,就需要使用虚拟主机实现。
1、使用端口号实现虚拟主机
第一步:配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
把server下没用的注解删除
将server复制一份,端口为90,并修改80访问index1,90访问index2
server {
listen ;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
root html;
index index1.html index.htm;
}
} server {
listen ;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
root html;
index index2.html index.htm;
}
}
第二步:将html目录下的index.html复制两份,并分别修改他们的背景颜色为red,green
cd /usr/local/nginx/html/
cp -rf index.html index1.html
cp -rf index.html index2.html
bgcolor='red' bgcolor='green'
第三步:重启nginx并访问
/usr/local/nginx/sbin/nginx -s reload
2、使用IP地址实现虚拟主机
第一步:关闭当前虚拟机,打开virtualbox,为虚拟机为主机添加第二网卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-enp0s3 ifcfg-enp0s8
vi ifcfg-enp0s8
将里面的IPADDR="172.18.25.175"改为IPADDR="192.168.1.175"
TYPE="Ethernet"
BOOTPROTO="static"
NAME="enp0s8"
DEVICE="enp0s8"
ONBOOT="yes"
IPADDR="192.168.1.175"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
DNS1=4.4.4.4
DNS2=8.8.8.8
IPV6INIT=NO
service network restart
第二步:配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
将两个server修改为,均访问80端口
server {
listen ;
server_name 172.18.25.175;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
root html;
index index1.html index.htm;
}
} server {
listen ;
server_name 192.168.1.175;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
root html;
index index2.html index.htm;
}
}
第三步:重启nginx并访问
/usr/local/nginx/sbin/nginx -s reload
3、使用域名实现虚拟主机
第一步:删除网卡2,并删除ifcfg-enp0s8文件,配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
server {
listen ;
server_name www.nwes.com;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
root html;
index index1.html index.htm;
}
} server {
listen ;
server_name www.item.com;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
root html;
index index2.html index.htm;
}
}
第二步:修改本地主机的hosts文件,为ip172.18.25.175添加两个域名
su
vim /etc/hosts
加入
172.18.25.175 www.news.com www.item.com
重启本地主机网络模块
sudo /etc/init.d/networking restart
第三步:重启nginx并访问
/usr/local/nginx/sbin/nginx -s reload
二、Nginx实现反向代理_负载均衡
新建一个虚拟机(172.18.25.176),模拟负载均衡,上传或者下载jdk和tomcat
vi /etc/profile
末尾加入
# 2019.11. ::
JAVA_HOME=/usr/local/java/jdk1..0_221
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
生效
source /etc/profile
解压tomcat并复制3份,实现集群
cp -rf apache-tomcat-9.0./ apache-tomcat-9.0.-
cp -rf apache-tomcat-9.0./ apache-tomcat-9.0.-
cp -rf apache-tomcat-9.0./ apache-tomcat-9.0.-
分别修改他们的端口号
vi apache-tomcat-9.0.-/conf/server.xml
将里面的8005 8080 8009改为:
apache-tomcat-9.0.20-占用端口:6005 6060 6009
apache-tomcat-9.0.20-占用端口:7005 7070 7009
apache-tomcat-9.0.20-3占用端口:8005 8080 8009
启动3个tomcat,关闭防火墙
./apache-tomcat-9.0.-/bin/startup.sh
./apache-tomcat-9.0.-/bin/startup.sh
./apache-tomcat-9.0.-/bin/startup.sh
systemctl stop firewalld
修改nginx虚拟机里的nginx.conf
ssh 172.18.25.175
vi /usr/local/nginx/conf/nginx.conf
把两个server修改为:
# nginx需要代理的三台tomcat主机,然后由nginx实现负载均衡
upstream www.manager.com{
#ip_hash; 可以根据hash选择主机
# weight 是权重,权重越大被选中概率越大
server 172.18.25.176: weight=;
server 172.18.25.176:;
server 172.18.25.176:;
} server {
listen ;
server_name www.ego.com;
#charset koi8-r;
#access_log logs/host.access.log main; location / {
#root html;
#index index1.html index.htm;
proxy_pass http://www.manager.com;
}
# 如果项目页面加载静态资源时,动静分离了,这里就不用配了,如:www.xxx.com/js/jquery.js
location ~ .*\.(ico|png|jpg|js|css)$ {
# 指定静态资源的位置
proxy_pass http://www.manager.com;
}
}
修改本地hosts文件
su
vim /etc/hosts
加入
172.18.25.175 www.manager.com
重启本地主机网络模块
sudo /etc/init.d/networking restart
最新文章
- 从零开始使用Jenkins来构建Docker容器(Ubuntu 14.04)
- SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码
- 【hibernate】<;第一节>;hibernate简单入门
- 微软的决心,开发者的信心!惊喜的 Connect(); // 2016
- 安装Netsharp演示插件
- php获取网站根目录
- 2015第10周日CSS—3
- 探讨css中repaint和reflow
- Java中的嵌套类和内部类
- ";ORA-01460: 转换请求无法实现或不合理";及C#操作Blob总结
- mongodb取出最大值与最小值
- 一步步学习操作系统(2)——在STM32上实现一个可动态加载kernel的";my-boot";
- 201521123082 《Java程序设计》第9周学习总结
- Redis的使用初探
- YARN的基础配置
- 生成器(generator,yield),next,send
- Django 1.11.7学习,配置MySQL数据库(python3.5)
- WebApp开发技术搭配
- js实现打印
- SpringBoot的幕后推手...
热门文章
- Windows上redis下载与安装
- JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能
- R-seq()
- Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [/Cppcc] due to a StackOverflowError. Possible root causes include a too low setting for -Xs
- 一、Git的一些命令操作----创建版本库、增加文件到Git库、时光机穿梭、远程仓库
- Java程序如何限速(控制下载和上传速度)
- [论文理解] FoveaBox: Beyond Anchor-based Object Detector
- Python做域用户验证登录
- Thread 源码阅读
- NAT地址转换常用命令详解