Nginx负载均衡实现动态分离
环境
主机名 | IP | 服务 |
---|---|---|
agent | 192.168.164.128 | Nginx |
Nginx | 192.168.164.133 | Nginx |
httpd | 192.168.164.137 | httpd |
反向代理与负载均衡
每台主机都关闭防火墙和selinux
[root@localhost]# systemctl stop firewalld.service //临时关闭防火墙,重启后依旧启动
[root@localhost]# setenforce 0
[root@localhost]# vim /etc/selinux/config
...
SELINUX=disabled
...
agent配置
[root@localhost]# vim /usr/local/nginx/conf/nginx.conf
......
upstream webservers { //配置负载均衡
server 192.168.164.133;
server 192.168.164.137;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { // 配置反向代理
proxy_pass http://webservers;
}
......
[root@agent ~]# nginx -t //检查语法是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@agent ~]# nginx -s reload //重新加载配置文件
使用agent的站点
配置动静分离
修改agent主机的Nginx配置文件
[root@localhost]# vim /usr/local/nginx/conf/nginx.conf
......
#gzip on;
upstream dynamic {
server 192.168.47.120; //动态主机的ip
}
upstream static {
server 192.168.47.121; //静态主机的ip
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://static; //访问静态资源会自动跳转至静态主机
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
proxy_pass http://dynamic; //访问动态资源会自动跳转到动态主机
}
......
[root@agent ~]# nginx -t //检查配置文件语法是否有问题
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@agent ~]# nginx -s reload //重新加载配置文件