Nginx动静分离-tomcat
生活随笔
收集整理的這篇文章主要介紹了
Nginx动静分离-tomcat
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、動(dòng)靜分離
1、通過(guò)中間件將動(dòng)態(tài)請(qǐng)求和靜態(tài)請(qǐng)求分離。
2、為什么?
分離資源,減少不必要的請(qǐng)求消耗,減少請(qǐng)求延時(shí)。
3、場(chǎng)景
還可以利用php,fastcgi,python 等方式 處理動(dòng)態(tài)請(qǐng)求
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000location ~ \.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; } 處理php動(dòng)態(tài)請(qǐng)求 [root@web-01 ~]# cat ngixn.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events {worker_connections 1024; } http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"'; ?include /etc/nginx/mime.types;default_type application/octet-stream;?include /etc/nginx/conf.d/cp4/*.conf; } #主配置文件server conf 的配置
[root@web-01 ~]# cat test_mysite.conf ? upstream java_api{server 127.0.0.1:8080; } server {listen 80;server_name web01.fadewalk.com; ?access_log /var/log/nginx/host.access.log main;root /opt/app/code/cp4/code; ?location ~ \.jsp$ {proxy_pass http://java_api;index index.html index.htm;} ?location ~ \.(jpg|png|gif)$ {expires 1h;gzip on;} }Tomcat 部署jsp頁(yè)面
[root@web-01 ROOT]# tomcat version Server version: Apache Tomcat/7.0.76 Server built: Mar 12 2019 10:11:36 UTC Server number: 7.0.76.0 OS Name: Linux OS Version: 3.10.0-957.21.2.el7.x86_64 Architecture: amd64 JVM Version: 1.8.0_212-b04 JVM Vendor: Oracle Corporation ? [root@web-01 ~]# cd /usr/share/tomcat/webapps [root@web-01 webapps]# mkdir ROOT [root@web-01 webapps]# cd ROOT/ [root@web-01 ROOT]# pwd /usr/share/tomcat/webapps/ROOT #/usr/share/tomcat/webapps 所有頁(yè)面目錄,沒(méi)有ROOT目錄時(shí),需要自己新建,ROOT目錄為默認(rèn)的網(wǎng)站頁(yè)面目錄 ,項(xiàng)目目錄必須大寫,對(duì)應(yīng)配置 [root@web-01 ROOT]# ll total 4 -rw-r--r--. 1 root root 343 Jun 17 02:14 java_test.jsp view ?訪問(wèn)頁(yè)面 <html lang="en"> <head> <meta charset="UTF-8" /> <title>測(cè)試ajax和跨域訪問(wèn)</title> <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> </head> <script type="text/javascript"> $(document).ready(function(){$.ajax({type: "GET",url: "http://jeson.t.imooc.io/java_test.jsp",success: function(data) {$("#get_data").html(data)},error: function() {alert("fail!!!,請(qǐng)刷新再試!");}}); }); </script> <body><h1>測(cè)試動(dòng)靜分離</h1><img src="http://jeson.t.imooc.io/img/nginx.png"/><div id="get_data"><div> </body> </html> test_mysite.html ?處理動(dòng)態(tài)頁(yè)面請(qǐng)求 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <HTML><HEAD><TITLE>JSP Test Page</TITLE></HEAD><BODY><%Random rand = new Random();out.println("<h1>Random number:</h1>");out.println(rand.nextInt(99)+100);%></BODY> </HTML> java_test.jsp ?測(cè)試
總結(jié)
以上是生活随笔為你收集整理的Nginx动静分离-tomcat的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: nginx之Geoip读取地域信息模块
- 下一篇: nginx 静态资源WEB服务