加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux Centos7 实现nginx的七层负载均衡和动静分离

发布时间:2020-12-14 00:40:40 所属栏目:Linux 来源:网络整理
导读:一:环境 准备一个nginx代理服务器 三台http服务器两台处理静态和一台处理动态。(nginx/1.17.3) 二、在nginx主配置文件配置nginx反向代理upstream(地址池)指向真实服务器 vim /etc/nginx/nginx.conf 在http标签中加 upstream static { server 10.30.161.
一:环境
准备一个nginx代理服务器 三台http服务器两台处理静态和一台处理动态。(nginx/1.17.3)

二、在nginx主配置文件配置nginx反向代理upstream(地址池)指向真实服务器

vim /etc/nginx/nginx.conf

在http标签中加

upstream static {
    server 10.30.161.214:80 weight=2 max_fails=2 fail_timeout=2s;
    server 10.30.161.242:80 weight=2 max_fails=2 fail_timeout=2s;
    }   
    upstream php {
    server 10.30.161.241:80 weight=2 max_fails=2 fail_timeout=2s;   
    }

三、在子配置文件中

vim /etc/nginx/conf.d/proxy.conf

1、动态资源加载
在server标签中添加

location ~ .(php|jsp)$ {
    proxy_pass http://phpserver;
    #指向动态服务器地址池
    proxy_set_header Host $host:$server_port;
    #重新定义或者添加发往后端服务器的请求头$host真实服务器主机名$server_port端口
    proxy_set_header X-Real-IP $remote_addr;
    #启用客户端真实地址(否则日志中显示的是代理在访问网站)
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #显示http请求端的真实IP
    }

2、静态资源加载

location ~ .*.(html|gif|jpg|png|bmp|swf|css|js)$ {
        proxy_pass http://static;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
        }

至此代理服务器配置完成

四、两台静态服务器的单独配置

server {
    listen 80;
    server_name     localhost;

    location ~ .(html|jpg|png|js|css|gif|bmp|jpeg) {
    root   /web1;
    index  index.html;
}

}
配置静态项目
静态服务器1

mkdir /web1
        echo "this is jingtai22222" > /web1/index.html

静态服务器2

mkdir /web1
        echo "this id jingtai22222" > /web1/index.html

五、一台动态服务器的单独配置

yum 安装php7.1
[[email?protected] ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[[email?protected] ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[[email?protected] ~]#yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[[email?protected] ~]#yum install -y php71w-fpm
[[email?protected] ~]#systemctl start php-fpm
[[email?protected] ~]#systemctl enable php-fpm

编辑nginx的配置文件:

server {
        listen      80;
        server_name     localhost;
        location ~ .php$ {
            root           /web1;  #指定网站目录
            fastcgi_pass   127.0.0.1:9000;    #指定访问地址
            fastcgi_index  index.php;       #指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
                }
        }

配置动态web1上项目

mkdir /web1
 cd /web1
 vim index.php 
 <?php
phpinfo();
?>

六、测试
1,静态访问测试
用浏览器访问代理服务器IP 10.30.161.51/index.html
即可访问到静态服务器/web1上的项目,并且两台服务器来回切换,这就实现了nginx的负载均衡

2,动态访问测试用浏览器访问代理服务器IP 10.30.161.51/index.php即可访问到动态服务器/web1上的项目,从而实现了nginx的动静分离

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读