Nginx系列-9.配置NFS实现Nginx实现动静分离
目录 - Nginx系列
Nginx系列-1.Linux下安装Nginx
Nginx系列-2.配置LNMP(Linux、Nginx、MySQL、PHP)架构
Nginx系列-3.配置Nginx虚拟主机
Nginx系列-4.Nginx日志配置及日志切割
Nginx系列-5.配置Nginx的防盗链
Nginx系列-6.配置Nginx的HTTPS
Nginx系列-7.配置Nginx使用uwsgi支持web.py框架
Nginx系列-8.配置Nginx+Apache实现动静分离
Nginx系列-9.配置NFS实现Nginx实现动静分离
Nginx系列-10.采用Nginx搭建正向代理服务
Nginx系列-11.配置Nginx反向代理和负载均衡
实验环境
三台最小化安装的 CentOS 7.3
server1-Nginx-ip: 192.168.204.133
server2-Apache-ip: 192.168.204.134
server3-NFS-ip: 192.168.204.135
实验拓扑
一、在server3上安装配置NFS和nginx
- 在nfs上安装
nfs-utils
和nginx
yum install -y epel-* yum install -y nfs-utils nginx
- 配置
NFS
共享目录vim /etc/sysconfig/nfs
查找
MOUNTD_PORT=892
,并解除注释 - 启动
rpcbind
服务,启动nfs
服务,查看监听端口systemctl start rpcbind nfs netstat -anpt | grep rpcbind
- 指定
/var/www/share
目录为共享目录,设置权限777
mkdir /var/wwwroot mkdir /var/wwwroot/share chmod 777 /var/wwwroot/share
- 编辑
/etc/exports
文件,并添加如下内容/var/wwwroot/share 192.168.204.*(rw,sync)
- 重启
nfs
服务systemctl restart nfs
- 修改
nginx
配置文件/etc/nginx/nginx.conf
server { listen 80; server_name file.test.com; location / { root /var/wwwroot; index index.html index.htm; } }
启动nginx
服务systemctl start nginx
- 排除偶然因素,关闭防火墙
setenforce 0 systemctl stop firewalld
二、在server2上配置NFS,安装Apache和PHP
- 安装
nfs-utils
yum install -y nfs-utils
- 查看
NFS
上的共享目录showmount -e 192.168.204.135
- 把共享目录挂载到本地
mkdir /var/wwwroot mkdir /var/wwwroot/share mount 192.168.204.135:/var/wwwroot/share /var/wwwroot/share
- 测试
NFS
在server2
上读写文件,在server3
上查看 - 安装
Apache
和PHP
yum install -y php httpd
- 修改
Apache
的配置文件
将网站根目录修改为/var/wwwroot
vim /etc/httpd/conf/httpd.conf
在配置文件中添加节点,允许访问/var/wwwroot
目录<Directory "/var/wwwroot"> Require all granted </Directory>
- 启动
Apache
systemctl start httpd
- 排除偶然因素,关闭防火墙
setenforce 0 systemctl stop firewalld
- 建立测试文件
cd /var/wwwroot echo -e "<?php phpinfo(); ?>" >> info.php
- 在宿主机访问该测试文件
三、在server1(nginx)上配置NFS和Apache动静分离
- 安装
nginx
yum install -y epel-* yum install -y nginx vim
- 修改
nginx
配置文件/etc/nginx/nginx.conf
server { listen 80; server_name proxy-server.test.com; root /var/wwwroot; location / { proxy_pass http://192.168.204.134; #Apache Web Server } location /share { proxy_pass http://192.168.204.135/share; #NFS } }
- 启动
nginx
服务systemctl start nginx
- 排除偶然因素,关闭防火墙
setenforce 0 systemctl stop firewalld
四、测试
- 在
server2
编辑一个PHP文件vim /var/wwwroot/index.php
添加如下内容
<!DOCTYPE html> <html> <head> <title>Test</title> <link rel=‘stylesheet‘ href=‘./share/style.css‘> </head> <body> <?php $str = date(‘Y-m-d h:i:sa‘, time()); ?> <p class=‘time‘><?php echo $str; ?></p> </body> </html>
- 在
server3
编辑一个css文件vim /var/wwwroot/share/style.css
添加如下内容
.time { color: red; font-size: 20px; text-align: center; }
- 在宿主机访问
server1
的IP地址
其中访问的index.php
文件来自server2
,style.css
文件来自server3
原文地址:http://blog.51cto.com/tong707/2126866
时间: 2024-12-19 21:31:10