目录结构
nginx动静分离,负载均衡简单使用
动静分离,负载均衡
1.官网: http://nginx.org
2.解压: tar -zxvf
nginx-1.13.9.tar.gz -C /usr/local/src
3.编译:cd /usr/local/src/nginx-1.13.9
4.检查安装环境,并指定将要安装的位置 ./configure --prefix=/usr/local/nginx
#缺包报错 ./configure:error:C compiler cc is not found
使用yum安装缺少的包 yum -y install gcc pcre-devel openssl openssl-devel
5.编译安装 make && make install
6.测试是否安装成功
启动:/usr/local/nginx/bin/nginx
查看端口是否有nginx进程监听: netstat -ntlp | grep 80
7.修改nginx配置文件
server{
listen 80;
server_name nginx-01.itcast.cn; #nginx所在服务器主机名
#反向代理的配置
location/{
root html;
proxy_pass http://192.168.1.100; #z这里是代理走向的服务器:tomcat
}
}
8.动静分离
修改配置文件,配置locaation
#动态资源
location ~.*\.(jsp|do|action)$ {
proxy_pass http://172.168.14.1:8080;
}
#静态态资源
location ~.*\.(html|js|css|gif|jpg|png)$ {
expires 3d;
}
9.负载均衡
修改配置文件,在http节点下配置upstream,名称要和location下proxy_pass http://后的保持一致
http{
upstream tomcat{
server 172.168.1.12:8080 weight = 1;
server 172.168.1.11:8080 weight = 1;
}
location ~.*\.(jsp|do|action)$ {
proxy_pass http://tomcat;
}
}