nginx-动静分离

动静分离

Nginx 动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用 Nginx处理静态页面,Tomcat 处理动态页面。动静分离从目前实现角度来讲大致分为两种:
(1)纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;
(2)动态跟静态文件混合在一起发布,通过 nginx 来分开。

动静分离原理图:

                         

在配置文件中:
1、通过 location 指定不同的后缀名实现不同的请求转发。
 
2、可通过 expires 参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量(非必要)。
 
////////具体 Expires 定义:是给一个资源 设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用 Expires 来缓存),我这里设置 3d,表示在这 3 天之内访问这个 URL,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码 304,如果有修改,则直接从服务器重新下载,返回状态码 200。

准备工作

准备静态资源:创建nginx_test_data文件夹,其下两个文件夹,分别放一个页面和图片

准备动态资源:在tomcat的webapps目录下随便建一个目录(如nginx),其下随便放一个jsp文件(如index.jsp)

 

静态配置

在nginx.conf的server块配置: 

       location /www/{
            root  I:/nginx-1.17.10/nginx_test_data;
        }

       location /image/{
            root  I:/nginx-1.17.10/nginx_test_data;
            autoindex  on;
        }

 

动态配置

配置一个代理:

 #配置代理
     upstream mynginx{
    server 127.0.0.1:8080;
     }

在server下配置:

# 所有动态请求都转发给tomcat处理 
    location ~ \.(jsp|do)$ { 
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_pass http://mynginx;
    } 

测试

浏览器输入http://127.0.0.1/image/chutian.jpg就能访问到图片资源

浏览器输入http://127.0.0.1/www/test.html就能访问到静态页面

浏览器输入http://127.0.0.1/nginx/index01.jsp,访问结果如下:

 

完整配置文件


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #配置服务器列表
    upstream myserver{
        server 127.0.0.1:8080 weight=1;
        server 127.0.0.1:8081 weight=1;
}

     #配置代理
     upstream mynginx{
    server 127.0.0.1:8080;
     }

    server {
        listen       80;
        server_name  www.xb.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass  http://myserver;
            index  index.html index.htm;
        }
       # 所有静态请求都转发给nginx处理 
         location /www/{
            root  I:/nginx-1.17.10/nginx_test_data;
        }

         location /image/{
            root  I:/nginx-1.17.10/nginx_test_data;
            autoindex  on;
        }
    
    # 所有动态请求都转发给tomcat处理 
       location ~ \.(jsp|do)$ { 
          proxy_next_upstream http_502 http_504 error timeout invalid_header;
          proxy_pass http://mynginx;
       } 

        #error_page  404              /404.html;

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
          listen       9001;
         server_name  localhost;

          location  ~ /edu {
         	 proxy_pass http://127.0.0.1:8080;
          }
         location  ~ /vod {
         	 proxy_pass http://127.0.0.1:8081;
          }
    }


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章