nginx 实现动静分离
在生产环境中,nginx的图片相关的静态信息是存放在专用的存储服务器上,然后通过挂载让nginx来读取文件。
server | IP |
---|---|
nginx | 172.20.27.10 |
http1 | 172.20.27.20 |
http2 | 172.20.27.21 |
nfs | 172.20.27.22 |
nginx 服务器做为反向代理来实现动静分离
nginx服务器配置
1.配置反向代理
[[email protected] ~]# mkdir /apps/nginx/conf/servers
[[email protected] ~]# vim /apps/nginx/conf/servers/http_proxy.conf
upstream app {
server 172.20.27.20:80 weight=1 max_fails=3 fail_timeout=5s;
server 172.20.27.21:80 weight=1 max_fails=3 fail_timeout=5s;
}
server {
server_name www.mylinuxops.com;
listen 80;
location / {
root /data/www;
index index.html;
}
location /app {
proxy_pass http://app;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/static;
index index.html;
}
}
~
2.将反向代理的配置导入主配置文件
http {
......
include /apps/nginx/conf/servers/*.conf;
}
3.启动服务
[[email protected] ~]# nginx
apache服务器配置
1.分别在两台服务器上安装httpd并创建站点目录
yum install httpd -y
mkdir /var/www/html/app
2.分别创建测试文件
[[email protected] ~]# echo test1 > /var/www/html/app/index.html
[[email protected] ~]# echo test2 > /var/www/html/app/index.html
3.启动服务
systemctl start httpd
4.在客户端测试反向代理能否使用
[[email protected] ~]# curl www.mylinuxops.com/app/
test1
[[email protected] ~]# curl www.mylinuxops.com/app/
test2
nfs配置
1.创建出需要共享出去的目录
[[email protected] ~]# mkdir /data/static
2.配置nfs文件
[[email protected] ~]# vim /etc/exports
/data/static *(rw,no_root_squash)
3.启动nfs服务
[[email protected] ~]# systemctl start nfs
4.在nginx上挂在nfs文件系统
[[email protected] ~]# vim /etc/fstab
172.20.27.22:/data/static /data/static nfs defaults,_netdev 0 0
#使用nfs挂载时需要加上参数_netdev,否则服务器重启时可能会卡住无法启动
[[email protected] ~]# mount -a
测试
原文地址:https://blog.51cto.com/11886307/2403951
时间: 2024-10-29 12:06:03