Nginx实现动静分离

本文介绍了在CentOS环境下,如何使用Nginx实现动静分离以及配置负载均衡。首先,详细讲述了在DR服务器上安装Nginx、Dynamic服务器上部署LNMP以及Static服务器上安装httpd的过程,并确保各环节访问正常。接着,重点讨论了Nginx的动静分离配置,通过设置使静态资源由httpd服务器处理,动态内容由PHP支持的服务器处理。最后,通过配置负载均衡,确保请求可以在多个服务器之间均匀分布,增强了系统的可用性和稳定性。
摘要由CSDN通过智能技术生成

Nginx实现动静分离

简介:
环境说明:
系统 IP 服务
CentOS8 调度器 DR 192.168.220.9 Nginx nginx-1.20.1.tar.gz
CentOS8 动态页面处理 Dynamic(动态) 192.168.220.10 LNMP nginx-1.20.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz php-8.0.10.tar.gz
CentOS8 静态页面处理 Static(静态) 192.168.220.17 httpd apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz

关闭三台防火墙和selinux,改名

# DR
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# hostnamectl set-hostname DR
[root@localhost ~]# bash
[root@DR ~]#

# Dynamic
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# hostnamectl set-hostname Dynamic
[root@localhost ~]# bash
[root@Dynamic ~]#

# Static
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# hostnamectl set-hostname Static
[root@localhost ~]# bash
[root@Static ~]#
DR上安装Nginx
# 安装***Nginx***    
# 安装依赖包和工具包
[root@DR ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@DR ~]# yum -y group mark install "Development Tools"

# 创建用户
[root@DR ~]# useradd -r -M -s /sbin/nologin nginx

# 创建日志存放目录
[root@DR ~]# mkdir /var/log/nginx -p

# 下载nginx,解压
root@DR ~]# mkdir /usr/src/soft
[root@DR ~]# cd /usr/src/soft/
[root@DR soft]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@DR soft]# ls
nginx-1.20.1.tar.gz
[root@DR soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local/

# 编译安装Nginx
[root@DR soft]# cd /usr/local/nginx-1.20.1/
[root@DR nginx-1.20.1]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
......

[root@DR nginx-1.20.1]# make && make install
......

# 配置环境变量
[root@DR nginx-1.20.1]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@DR nginx-1.20.1]# which nginx
/usr/local/nginx/sbin/nginx

# 尝试启动Nginx
[root@DR nginx-1.20.1]# /usr/local/nginx/sbin/nginx 
[root@DR nginx-1.20.1]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         128                0.0.0.0:80(Nginx默认端口)                0.0.0.0:*                  
LISTEN    0         128                   [::]:22                   [::]:*

# 设置开机自启
[root@DR nginx-1.20.1]# cd /usr/lib/systemd/system/
[root@DR system]# cp sshd.service nginxd.service
[root@DR system]# vi nginxd.service
[Unit]
Description=Nginx server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillMode=process

[Install]
WantedBy=multi-user.target

[root@DR system]# /usr/local/nginx/sbin/nginx -s stop
[root@DR system]# systemctl daemon-reload
[root@DR system]# systemctl start nginxd.service
[root@DR system]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         128                0.0.0.0:80                0.0.0.0:*                  
LISTEN    0         128                   [::]:22                   [::]:*
访问页面是否正常

在这里插入图片描述

Dynamic安装LNMP
# 安装***Nginx***
# 安装依赖包和需要的工具包
[root@Dynamic ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@Dynamic ~]# yum -y group mark install "Development Tools"

# 创建用户nginx
[root@Dynamic ~]# useradd -r -M -s /sbin/nologin nginx

# 创建nginx的日志存放目录
[root@Dynamic ~]# mkdir -p /var/log/nginx

# 下载nginx并解压
[root@Dynamic ~]# mkdir /usr/src/soft
[root@Dynamic ~]# cd /usr/src/soft/
[root@Dynamic ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@Dynamic soft]# ls
nginx-1.20.1.tar.gz
[root@Dynamic soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local/

# 开始编译安装nginx
[root@Dynamic soft]# cd /usr/local/nginx-1.20.1/
[root@Dynamic nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值