反向代理与负载均衡
环境说明
主机名 | IP | 服务 |
---|---|---|
agent | 192.168.47.115 | nginx |
nginx | 192.168.47.120 | nginx |
httpd | 192.168.129.121 | httpd |
注:nginx服务都是源码安装 、httpd为yum安装
nginx源码安装请参考:nginx
准备工作
每台主机开启服务,并关闭防火墙与selinux
systemctl distable --now firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
setenforce 0
agent端配置
root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
33 #gzip on;
34
35 upstream webservers { #配置负载均衡
36 server 192.168.47.120;
37 server 192.168.47.121;
38 }
39
40 server {
41 listen 80;
42 server_name localhost;
43
44 #charset koi8-r;
45
46 #access_log logs/host.access.log main;
47
48 location / { #配置反向代理
49 proxy_pass http://webservers;
50 }
[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主机IP地址访问,并刷新测试
动静分离
环境说明
主机名 | IP | 服务 |
---|---|---|
agent | 192.168.47.115 | nginx |
nginx | 192.168.47.120 | lnmp |
httpd | 192.168.129.121 | httpd |
lnmp架构部署详细步骤请参考:lnmp
修改agent主机配置文件
#gzip on;
upstream dynamic {
server 192.168.47.120; #lnmp主机的ip
}
upstream static {
server 192.168.47.121; #httpd主机的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
使用agent主机IP地址访问测试