Nginx动静分离

动静分离简介

分为两种情况
第一种情况:将动态页面和静态页面物理分离。将静态文件、动态文件分别放在单独的服务器上,使用独立的域名,是目前主流方案。

第二种情况:将动态文件跟静态文件混合在一起发布,通过nginx来区分。

作用:提高访问效率。

动静分离案例

要求:
两台服务器,一台Nginx代理服务器10.154.0.111,一台tomcat服务器10.154.0.112
Nginx服务器上部署静态资源,tomcat服务器上部署动态资源

nginx配置文件如下

$ cat nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        default_type text/html;

	location / {
	    root webapps;
            proxy_pass http://10.154.0.112:8080;
	}
    #动态资源访问tomcat服务器
	location ~*\.(php|jsp|cgi|shtml)?$ {
	    root webapps;
            proxy_pass http://10.154.0.112:8080;
	}
	#静态资源访问nginx服务器根目录
	location ~*\.(html|png|jpg|txt|js|css)$ {
	    root html;
	    #缓存30天
	    expires 30d;
	}
	
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

}


Nginx服务器配置如下

#上传静态资源tp.png
$ cd /usr/local/nginx/html
$ rz -be
$ nginx -s reload

tomcat服务器配置如下

$ cd /usr/local/tomcat9/webapps/ROOT
$ cat index.jsp
<html>
    <head>
           <title>Test JSP!</title>
    </head>
    <body>
           <%
                  out.println("This is test JSP!");
           %>

    </body>
</html>

$ cd /usr/local/tomcat9/bin
$ ./shutdown.sh
$ ./startup.sh

使用浏览器访问


后记
Nginx详解高级课程中的案例已完成,视频中案例里html是静态文件,当作动态文件实现。我觉的不太严谨,但曝光Linux企业运维实战中的案例缺少了部分配置。只有先这样吧,时间不早了,以后再研究一下。

2021年1月14日,动静分离可参考这篇博文


学习来自:B站大学-Nginx详解高级课程P12P13,《曝光Linux企业运维实战》

posted @ 2021-01-14 01:05  努力吧阿团  阅读(103)  评论(0)    收藏  举报