Nginx使用upstream实现动静分离

一、为什么要进行动静分离

分离资源,减少不必要到的请求消耗,减少请求延时。

注:我这里,是nginx处理静态资源,apache处理动态资源。

场景分析:

1、未分离之前的场景步骤

(1)客户端请求url到中间件(比如nginx,apache)

(2)中间件根据url请求相应目录,程序框架

(3)程序框架运行程序逻辑

(4)程序逻辑请求相应数据资源

(5)将数据资源返回给客户端

注:其实,静态资源是不需要经过动态请求,直接中间件返回给客户端就可以了。也就是说只要第1步和第5步就可以了

配置文件展示:

upstream php_api{
    #代理请求到本地apache服务器,实现动静分离(这里我将apache默认端口更改为81)
    server 127.0.0.1:81;
}
server {
    listen       80;
    server_name  www.xiaobudiu.top;

    access_log  /etc/nginx/logs/access/www.xiabudiu.top.access.log  main;
    root /data/www;

    location ~ \.php$ {
        #如果网站访问的url后缀是.php,则代理使用apache进行解析
        proxy_pass http://php_api;
        index  index.html index.htm;
    }

    #如果请求的是静态资源,则默认使用nginx进行处理
    location ~ \.(jpg|png|gif)$ {
        expires 1h;
        gzip on;
    }

    location /{
        index  index.html index.htm;
    }


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504 404 403  /404.html;
    location = /404.html {
        root   /data/errorPage;
    }


    location ~ /\.ht {
        deny  all;
    }
}

 
  或者是这样:

upstream image {
    server  192.168.0.3:80;
    server  192.168.0.4:80;
}

upstream php {
    server  192.168.0.5:80;
    server  192.168.0.6:80;
}

server {
    listen       80;
    server_name  www.xiaobudiu.top;

    access_log  /etc/nginx/logs/access/www.xiabudiu.top.access.log  main;

    location  /{
        #如果uri后缀不是.php或是图片后缀,就走本地服务器进行处理
        root data/www;
        index  index.php index.html;
    }

    location ~* \.php$ {
        #如果是.php结尾,反向代理到upstream php组里进行轮询
        proxy_pass  http://php;
    }

    location ~* "\.(.jpg|png|jpeg|gif)" {
        #如果是.jpg,.png,.jpeg,.gif结尾,反向代理到upstream image组里进行轮询
        proxy_pass http://image;
    }

    # redirect server error pages to the static page /404.html
    error_page   500 502 503 504 404 403  /404.html;
    location = /404.html {
        root   /data/errorPage;
    }

    location ~ /\.ht {
        deny  all;
    }

}

 
注:这是在子配置文件中进行的定义,比如,上面编辑的就是/etc/nginx/conf.d/www.xiaobudiu.top.conf 文件

当然,由于nginx对代理有一定要求,所以,在nginx.conf中也要进行一定的定义,比如这样:
  
nginx.conf

user  nginx;
worker_processes  1;
worker_rlimit_nofile 65536;

error_log  /etc/nginx/logs/error/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    multi_accept on;
    use epoll;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /etc/nginx/logs/access/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    client_max_body_size 20m;

    gzip  on;
    gzip_proxied any;
    gzip_comp_level 3;
    gzip_min_length 1k;
    gzip_buffers 16 32k;
    gzip_http_version 1.0;
    gzip_types text/plain text/css application/json application/xml+rss text/javascript image/jpeg image/gif image/png;

    fastcgi_buffers 256 16k;
    fastcgi_buffer_size 128k;
    fastcgi_connect_timeout 3s;
    fastcgi_send_timeout 120s;
    fastcgi_read_timeout 120s;
    reset_timedout_connection on;
    server_names_hash_bucket_size 100;

    include /etc/nginx/conf.d/*.conf;

}

 
最后,需要说明的是,上述配置文件只是为了说明反向代理和负载均衡是如何实现的,并没有结合实际项目。

 
注:nginx 官方proxy模块文档 http://nginx.org/en/docs/http/ngx_http_proxy_module.html

注:负载均衡中多态服务器间的数据同步这里采用rsync,当然,还有其他方式。可参考:
https://www.cnblogs.com/miclesvic/p/6189540.html