nginx 动静分离详解

准备一个nginx代理 两个http 分别处理动态和静态。
1.配置nginx反向代理upstream;
vim /etc/nginx/ngnx.conf

upstream static {
server 10.0.105.196:80 weight=1 ;
}
upstream php {
server 10.0.105.200:80 weight=1 ;
}
server {
listen 80;
server_name localhost;
#动态资源加载
location ~ .(php|jsp)$ {
proxy_pass http://php; #要和上面创建的 php 一致
}
#静态资源加载
location ~ .*.(html|gif|jpg|png|bmp|swf|css|js)$ {
proxy_pass http://static; #要和上面创建的 static 一致
}
}

静态资源配置
vim /etc/nginx/ngnx.conf
server {
listen 80;
server_name localhost;
location ~ .(html|jpg|png|js|css|gif|bmp|jpeg) {
root /home/www/nginx;
}
}

动态资源配置:
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@nginx-server ~]#systemctl start php-fpm
[root@nginx-server ~]#systemctl enable php-fpm

编辑nginx的配置文件:
vim /etc/nginx/ngnx.conf
server {
listen 80;
server_name localhost;
location ~ .php$ {
root /home/nginx/html; #指定网站目录
fastcgi_pass 127.0.0.1:9000; #指定访问地址
fastcgi_index index.php; #指定默认文件
fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name; #站点根目录,取决于root配置项
include fastcgi_params; #包含nginx常量定义
}
}
**#要在网站发布目录里创建一个“index.php"的文件
#vim /home/nginx/html/index.php 把下面内容添加进去
<?php
phpinfo();
?>

        php index.php  执行一下

访问动态网页是时ip加上动态页面文件
如 10.11.56.67:/index.php

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值