网站建设中的主要功能,备案域名购买地址,网站设计问题,湖北建设厅官方网站Nginx 的动静分离 我们通过中间件将动态请求和静态请求进行分离#xff0c;减少了不必要的请求消耗和延时。 动静分离后#xff0c;即使动态服务不可用#xff0c;但静态资源不会受到影响。 应用实例 1、准备环境 系统角色主机名IP服务CentOS 7.2反向代理Nginx_Proxy192.168… Nginx 的动静分离 我们通过中间件将动态请求和静态请求进行分离减少了不必要的请求消耗和延时。 动静分离后即使动态服务不可用但静态资源不会受到影响。 应用实例 1、准备环境 系统角色主机名IP服务CentOS 7.2反向代理Nginx_Proxy192.168.1.10Nginxv1.16.1CentOS 7.2静态资源Nginx_Static192.168.1.11Nginxv1.16.1CentOS 7.2动态资源Tomcat_Server192.168.1.12Tomcat/7.0.762、配置服务器 2.1、在Nginx Static主机中配置静态资源以图片为例 [rootNginx_Static]# vim /etc/nginx/conf.d/static.conf
server {listen 80;server_name static.com;root /data/www/static;index index.html;location ~* .*\.(png|jpg|gif)$ {root /data/www/static/images;}
}
#准备存放图片的目录及图片
[rootNginx_Static]# mkdir -pv /data/www/static/images
[rootNginx_Static]# wget -O /data/www/static/images/nginx.png http://nginx.org/nginx.png
[rootNginx_Static]# systemctl restart nginx 2.2、在 Tomcat_Server主机中配置动态资源以随机数为例 #安装Tomcat并编辑随机数的jsp文件
[rootTomcat_Server]# yum -y install tomcat
[rootTomcat_Server]# mkdir /usr/share/tomcat/webapps/ROOT
[rootTomcat_Server]# vim /usr/share/tomcat/webapps/ROOT/java.jsp
% page languagejava importjava.util.* pageEncodingutf-8%
HTMLHEADTITLEJSP Test Page/TITLE/HEADBODY%Random rand new Random();out.println(h1Random number:/h1);out.println(rand.nextInt(99)100);%/BODY
/HTML#重启tomcat服务
[rootTomcat_Server]# 2.3、在Nginx_Proxy主机上配置代理实现访问jsp和png [rootNginx_Proxy]# vim /etc/nginx/conf.d/proxy.conf
upstream static {server 192.168.1.11:80;
}
upstream java {server 192.168.1.12:8080;
}server {listen 80;server_name proxy.com;location / {root /data/www/whole;index index.html;}location ~ .*\.(png|jpg|gif)$ {proxy_pass http://static;}location ~ .*\.jsp$ {proxy_pass http://java;}
}
#重启nginx
[rootNginx_Proxy]# systemctl restart nginx 2.4、通过代理服务器测试访问动态及静态资源 2.5、在Nginx_Proxy主机上将动态和静态资源合并为html文件 [rootNginx_Proxy]# mkdir /data/www/whole
[rootNginx_Proxy]# vim /data/www/whole/index.html
html langen
headmeta charsetUTF-8 /title测试ajax和跨域访问/titlescript srchttp://libs.baidu.com/jquery/2.1.4/jquery.min.js/script
/head
script typetext/javascript
$(document).ready(function(){$.ajax({type: GET,url: http://proxy.com/java.jsp,success: function(data) {$(#get_data).html(data)},error: function() {alert(fail!!,请刷新再试!);}});
});
/scriptbodyh1测试动静分离/h1img srchttp://proxy.com/nginx.pngdiv idget_data/div/body
/html 2.6、使用浏览器测试访问动态及静态资源能否加载在一个html文件中 2.7、关闭静态资源的nginx服务后发现静态内容无法访问但动态内容可以正常浏览 2.8、关闭动态资源的tomcat服务后发现动态内容无法访问但静态内容可以正常浏览 转载于:https://www.cnblogs.com/Smbands/p/11415727.html