nginx动静分离[centos7、centos8]

动静分离:
服务器配置:
动态资源 吃cpu和内存,动态资源放在cpu多、内存大的服务器上
静态资源 吃硬盘和带宽,静态资源放在硬盘大、带宽高的服务器上

【centos8系统】
#准备三台服务器 105:反向代理服务器  106:静态服务器  107:动态服务器
#准备三台nginx[nginx/1.20.2]  在www.nginx.org->download->Linux packages for stable and mainline versions.-> RHEL/CentOS->
#[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
dnf install -y nginx #centos8版本
[root@localhost ~]# echo "hello world 192.168.1.105" >  /usr/share/nginx/html/index.html  #105
[root@localhost ~]# nginx  #192.168.1.105
[root@localhost ~]# dnf install php* -y  #107
[root@localhost ~]# echo "hello world 192.168.1.106" >  /usr/share/nginx/html/index.html  #106
[root@localhost ~]# nginx  #192.168.1.106
[root@localhost ~]# vim /usr/share/nginx/html/index.php     #准备nginx的php配置文件
<?php
echo "hello world";
phpinfo();
?>
[root@localhost ~]# php /usr/share/nginx/html/index.php #107
[root@localhost ~]# php --version  #107  【是php7的版本】
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies
[root@localhost ~]# cd /etc/nginx/    #107
[root@localhost nginx]# ls   #107
conf.d  default.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@localhost nginx]# ls conf.d/   #107
default.conf  php-fpm.conf  
[root@localhost nginx]# rm -rf conf.d/php-fpm.conf   #107
[root@localhost nginx]# rm -rf default.d/php.conf   #107
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf  #107
#改三处:
1、location /里的index index.html index.htm;改成index index.php;
2、location ~ \.php$里的root  html改成root   /usr/share/nginx/html;
3、把php注释打开, 把/scripts改成$document_root$
location / {
        root   /usr/share/nginx/html;
        index  index.php;
    }
    
location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
[root@localhost ~]# vim /etc/php-fpm.d/www.conf  #107
;listen = /run/php-fpm/www.sock
listen = 9000
[root@localhost ~]# systemctl enable php-fpm
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# nginx  #192.168.1.107
[root@localhost ~]# ss -lnpt   #"php-fpm"9000端口已起来
#浏览器访问http://192.168.1.107/  ->php页面出现
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf   #105[配置负载均衡器],实现动静分离
upstream static {
        server 192.168.1.106 max_fails=2 fail_timeout=2;
}
upstream php {
        server 192.168.1.107 max_fails=2 fail_timeout=2;

}

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
        proxy_pass http://static;
    }

    location ~ \.php$ {
        proxy_pass http://php;
}
[root@localhost ~]# nginx -s reload  #105
#访问http://192.168.1.105/index.html -> 页面显示hello world 192.168.1.106
#访问http://192.168.1.105/index.php  ->php页面

【centos7系统】
#准备三台服务器 110:反向代理服务器  111:静态服务器  112:动态服务器
#准备三台nginx[nginx/1.20.2]  在www.nginx.org->download->Linux packages for stable and mainline versions.-> RHEL/CentOS->
#[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
dnf install -y nginx #centos8版本
[root@localhost ~]# echo "hello world 192.168.1.110" >  /usr/share/nginx/html/index.html  #110
[root@localhost ~]# nginx  #192.168.1.110
[root@localhost ~]# vim /usr/share/nginx/html/index.php  #112
<?php
echo "hello world";
phpinfo();
?>
动态资源配置:  #centos8默认的php是7版本,centos7默认的php是5版本,所以用centos7系统要装下面的源
yum 安装php7.1
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx-server ~]#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
[root@nginx-server ~]#yum install -y php71w-fpm
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf  #107
#改三处:
1、location /里的index index.html index.htm;改成index index.php;
2、location ~ \.php$里的root  html改成root   /usr/share/nginx/html;
3、把php注释打开, 把/scripts改成$document_root$
location / {
        root   /usr/share/nginx/html;
        index  index.php;
    }
    
location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
[root@nginx-server ~]#systemctl enable php-fpm
[root@localhost ~]# vim /etc/php-fpm.d/www.conf  #112
listen = 9000
[root@nginx-server ~]#systemctl start php-fpm
[root@localhost ~]# nginx  #192.168.1.112
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf   #110[配置负载均衡器],实现动静分离
upstream static {
        server 192.168.1.111 max_fails=2 fail_timeout=2;
}
upstream php {
        server 192.168.1.112 max_fails=2 fail_timeout=2;

}

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
        proxy_pass http://static;
    }

    location ~ \.php$ {
        proxy_pass http://php;
}
[root@localhost ~]# nginx -s reload  #110
#访问http://192.168.1.110/index.html -> 页面显示hello world 192.168.1.110
#访问http://192.168.1.112/index.php  ->php页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值