栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Nginx实现动静分离

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx实现动静分离

目录
    • 环境:
    • DR上安装Nginx
    • Dynamic安装LNMP
    • Static安装httpd
    • 配置动静分离
    • 访问测试:

环境:
系统IP服务
CentOS8 动态192.168.235.160LNMP
CentOS8 静态192.168.235.155Nginx
Redhat8 调度器 DR192.168.235.135Httpd

关闭三台防火墙和selinux、修改主机名

DR上安装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

[root@dr ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
--2021-10-30 20:08:25--  http://nginx.org/download/nginx-1.20.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1061461 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.20.1.tar.gz”

100%[======================================>] 1,061,461    629KB/s 用时 1.6s   

2021-10-30 20:08:27 (629 KB/s) - 已保存 “nginx-1.20.1.tar.gz” [1061461/1061461])

[root@dr ~]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
[root@dr ~]# cd /usr/local/nginx-1.20.1/

//编译安装Nginx
[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]# /usr/local/nginx/sbin/nginx 
[root@dr nginx-1.20.1]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128          *:22                       *:*                                
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*  

//设置nginx开机自启
[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
[root@dr system]# cat 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              
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128          *:22                       *:*                        
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                  
[root@dr system]# 

访问测试:

Dynamic安装LNMP

lnmp部署

访问页面

Static安装httpd
//下载工具包
[root@Static ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make wget vim
[root@Static ~]# yum -y  groups mark install 'Development Tools'

[root@Static ~]# useradd -r -M -s /sbin/nologin apache

//下载包
[root@Static ~]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@Static ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@Static ~]# wget https://downloads.apache.org/httpd/httpd-2.4.48.tar.gz
[root@Static ~]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz   

//解压并编译安装apr
[root@Static ~]# tar xf apr-1.7.0.tar.gz -C /usr/local/
[root@Static ~]# tar xf apr-util-1.6.1.tar.gz  -C /usr/local/
[root@Static ~]# tar xf httpd-2.4.48.tar.gz  -C /usr/local/
[root@Static ~]# cd /usr/local/apr-1.7.0/
[root@Static apr-1.7.0]# vim configure

31879     trap "$RM "$cfgfile"; exit 1" 1 2 15
31880 #    $RM "$cfgfile"    //删除或注释掉此行

[root@Static apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@Static apr-1.7.0]# make && make install

//编译安装apr-util
[root@Static local]# cd apr-util-1.6.1/
[root@Static apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@Static apr-util-1.6.1]# make && make install

//编译安装httpd
[root@Static apr-util-1.6.1]# cd ../httpd-2.4.48/
[root@Static httpd-2.4.48]# ./configure --prefix=/usr/local/apache 
--enable-so 
--enable-ssl 
--enable-cgi 
--enable-rewrite 
--with-zlib 
--with-pcre 
--with-apr=/usr/local/apr 
--with-apr-util=/usr/local/apr-util/ 
--enable-modules=most 
--enable-mpms-shared=all 
--with-mpm=prefork
[root@Static httpd-2.4.48]# make && make install

//配置环境变量
[root@Static httpd-2.4.48]# echo "export PATH=/usr/local/apache/bin:$PATH" > /etc/profile.d/httpd.sh
[root@Static httpd-2.4.48]# bash
[root@Static httpd-2.4.48]# which httpd
/usr/local/apache/bin/httpd

[root@Static httpd-2.4.48]# cd /usr/lib/systemd/system/
[root@Static system]# cp sshd.service httpd.service
[root@Static system]# vi httpd.service
[root@Static system]# cat httpd.service 
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

[root@Static system]#  systemctl daemon-reload
[root@Static system]# systemctl start httpd
[root@Static system]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port        
LISTEN    0         128                      *:80                     *:*       

访问测试:

配置动静分离

修改nginx配置文件

33     #gzip  on;
34         upstream Dynamic {  	//添加
35            server 192.168.235.160;	//添加
36         }		//添加
37     
38         upstream Static {		//添加
39             server 192.168.235.135;   	//添加
40         } 		//添加

42     server {
43         listen       80;
44         server_name  localhost;
45         
46         #charset koi8-r;
47 
48         #access_log  logs/host.access.log  main;
49 
50         #location / {
51          #   root   html;
52           #  index  index.html index.htm;
53         #}
54         location ~ .php$ {		//添加
55              proxy_pass   http://Dynamic;		//添加
56         }
57         
58         #error_page  404              /404.html;
59 
60         # redirect server error pages to the static page /50x.html
61         #
62         error_page   500 502 503 504  /50x.html;
63         location = /50x.html {
64             root   html;
65         }
66 
67         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
68         #
69         #location ~ .php$ {
70         #    proxy_pass   http://127.0.0.1;
71         #}
72          location / {		//添加
73             proxy_pass http://Static;		//添加
74         }		//添加
访问测试:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/360768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号