从零开始的Nginx [ 4 ] --- Nginx动静分离


Nginx动静分离

  • 静态资源

  • 图片 css js index.html mp3

  • 动态资源

  • php java python

  - index.php 
     <div> {
   
   { user }}</div>
     <img srv="/img/a.jpg">

在这里插入图片描述

1 准备环境

  • 一个 nginx代理 [也可以说是负载均衡器]

  • 两个http 分别处理动态和静态

  • [都需要nginx支持]

2 代理机

配置nginx反向代理upstream

[root@localhost ~]# vim /etc/nginx/nginx.conf
user  nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
pid       /var/run/nginx.pid;

events {
    
    
    worker_connections 1024;
}
http {
    
    
    upstream static {
    
    
        server 静态资源主机IP;
    }
    upstream phpserver {
    
    
        server 动态资源主机IP;
        }
    server {
    
    
          listen    80;
          server_name localhost;
          #动态资源加载
          location ~ \.(php|jsp)$ {
    
    
            proxy_pass http://phpserver;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 }
          #静态资源加载
          location ~ \.(html|jpg|png|css|js)$ {
    
    
            proxy_pass http://static;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }
         }
}

3 静态资源配置

vim /etc/nginx/conf.d/default.conf

server {
    
    
        listen 80;
        server_name     localhost;

        location ~ \.(html|jpg|png|js|css) {
    
    
        root /home/www/nginx;
        index  index.html;
        }

}

写入静态页面

[root@localhost ~]# echo "这是一个静态网页" /var/www/nginx/index.html

测试

[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl 127.0.0.1/index.html
这是一个静态网页

4 动态资源配置

a 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

b 编辑nginx的配置文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    
    
        listen      80;
        server_name     localhost;
        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;    #站点根目录,取决于root配置项
            include        fastcgi_params;    #包含nginx常量定义
        }
}

c 写入动态网页

<!DOCTYPE html>
<html>
<head>
        <title>PHP 动态网站</title>
</head>
<body>
  <?php
        $user_name = 'bilibi 干杯!!!';
        echo "<h1>".$user_name."</h1>";
  ?>
  <img src="/test.jpg" >
</body>
</html>

测试

[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://127.0.0.1/index.php

<!DOCTYPE html>
<html>
<head>
        <title>PHP 动态网站</title>
</head>
<body>
  <h1>bilibi 干杯!!!</h1>  <img src="/test.jpg" >
</body>
</html>

动态网页中的 test.jpg 文件续需放入静态机目录下 与 静态网页index.html同目录 给权限

然后去代理机测试
curl http://代理机IP/index.php

<!DOCTYPE html>
<html>
<head>
        <title>PHP 动态网站</title>
</head>
<body>
  <h1>this is my 壁纸</h1>  <img src="/test.jpg" >
</body>
</html>

最后去windows桌面浏览器 进行访问
http://代理机IP/index.php

完成!在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Houaki/article/details/111342421