一.什么是动静分离?
将动态请求和静态请求区分访问,
二.为什么要做动静分离?
静态由Nginx处理, 动态由PHP处理或Tomcat处理....
因为Tomcat程序本身是用来处理jsp代码的,但tomcat也能处理静态资源.
tomcat本身处理静态效率不高,还会带来资源开销.
三.如何实现动静分离?
Nginx根据客户端请求的url来判断请求的是否是静态资源,如果请求的url包含jpg、png,则由Nginx处理。
如果请求的url是.php或者.jsp等等,这个时候这个请求是动态的,将转发给tomcat处理。
总结来说,Nginx是通过url来区分请求的类型,并转发给不同的服务端。
四.单机实现动静分离实战
[root@web01 ~]# yum install java tomcat -y
[root@web01 ~]# mkdir /usr/share/tomcat/webapps/ROOT -->主要站点根目录
[root@web01 ~]# vi /usr/share/tomcat/webapps/ROOT/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>Nginx+Tomcat</title>
</head>
<body>
<%
Random rand = new Random();
out.println("<h2>动态资源</h2>");
out.println(rand.nextInt(99)+100);
%>
<h2>静态图片</h2>
<img src="nginx.png" />
</body>
</html>
[root@web01 ~]# wget -O /usr/share/tomcat/webapps/ROOT/nginx.png http://nginx.org/nginx.png
[root@web01 ~]# systemctl start tomcat
tomcat监听在8080端口上:
#配置Nginx
[root@web01 conf.d]# cat ds.oldxu.com.conf
server {
listen 80;
server_name ds.oldxu.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
location ~* \.(png|gif|jpg|mp4)$ {
root /images;
expires 1d;
}
}
五.集群实现动静分离实战
[root@lb01 conf.d]# cat proxy_ds.oldxu.com.conf
upstream java {
server 172.16.1.7:8080;
}
upstream static {
server 172.16.1.8:80;
}
server {
listen 80;
server_name ds.oldxu.com;
location / {
proxy_pass http://java;
include proxy_params;
}
location ~* \.(png|gif|jpeg)$ {
proxy_pass http://static;
expires 2d;
include proxy_params;
}
}
六.rewrite实例
#根据浏览器访问的url跳转相关网页
[root@web01 conf.d]# cat url.oldxu.com.conf
server {
listen 80;
server_name url.oldxu.com.cn url.oldxu.com.jp;
#判断
if ( $http_host ~* cn ) {
set $language zh;
}
if ( $http_host ~* jp ) {
set $language jp;
}
rewrite ^/$ http://url.oldxu.com/$language/;
}
server {
listen 80;
server_name url.oldxu.com;
location / {
root /data;
}
}
中国 url.oldxu.com ---> url.oldxu.com --->中文
english url.oldxu.com ---> url.oldxu.com --->english
#访问相同URL,根据浏览器的语言跳转
server {
listen 80;
server_name url.oldxu.com;
location / {
if ( $http_accept_language ~* "en" ) {
set $language en;
}
if ( $http_accept_language ~* "zh|zh-CN" ) {
set $language zh;
}
root /data/$language;
}
}
return-----------------------------------
#访问URL的特定目录,直接返回其他页面
server {
listen 80;
server_name url.oldxu.com;
location / {
default_type text/html;
if ($request_uri ~* "a1=3526") {
return 200 "https://www.jd.com";
}
if ($request_uri ~* "git"){
return 403;
}
if ($request_uri ~* "^/test") {
return 302 "https://www.jd.com";
}
if ( $http_accept_language ~* "en" ) {
set $language en;
}
if ( $http_accept_language ~* "zh|zh-CN" ) {
set $language zh;
}
root /data/$language;
}
}
break-----------------------------------
#结束跳出,不执行break以下的所有代码
server {
listen 80;
server_name url.oldxu.com;
root /code;
location / {
rewrite /1.html /2.html
break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@web01]# echo "1.html" >/code/1.html
[root@web01]# echo "2.html" >/code/2.html
[root@web01]# echo "3.html" >/code/3.html
[root@web01]# echo "a.html" >/code/a.html
[root@web01]# echo "b.html" >/code/b.html
====================================================================================================================================================
set
if
return
状态码
状态码 字符串
状态码 URL 301 302
break
停止后续操作:
rewrite
# 关键字 正则 替代内容 flag标记
Syntax: rewrite regex replacement [flag];
跳转 :
重定向:
#flag
last #本条规则匹配完成后,继续向下匹配新的location URI规则 (开发| 伪静态)
break #本条规则匹配完成即终止,不再匹配后面的任何规则 (挂维护页)
redirect #返回302临时重定向, 地址栏会显示跳转后的地址
permanent #返回301永久重定向, 地址栏会显示跳转后的地址
=========================================================================
#依据不同语种,跳转到不同语言的页面
server {
listen 80;
server_name url.oldxu.com;
root /data;
set $language /default;
if ( $http_accept_language ~* zh ) {
set $language /zh;
}
if ( $http_accept_language ~* en ) {
set $language /en;
}
if ( $http_accept_language ~* ja ) {
set $language /jp;
}
rewrite ^/$ $language;
location / {
index index.html;
}
}
==========================================================================
######永久维护
server {
listen 80;
server_name url.oldxu.com;
root /data;
rewrite ^(.*)$ /wh.png break;
set $language /default;
if ( $http_accept_language ~* zh ) {
set $language /zh;
}
if ( $http_accept_language ~* en ) {
set $language /en;
}
if ( $http_accept_language ~* ja ) {
set $language /jp;
}
rewrite ^/$ $language;
location / {
index index.html;
}
}
######临时维护(jd)
#error_page 403 404 500 502 /wh.png;
#error_page 403 404 500 502 http://$http_host;
error_page 403 404 500 502 @temperror;
location @temperror {
rewrite ^(.*)$ http://$http_host;
}
==========================================================================
需求: 用户通过手机设备访问url.oldxu.com,跳转至url.oldxu.com/m
server {
listen 80;
server_name url.oldxu.com;
root /data;
if ($http_user_agent ~* "android|iphone|ipad") {
rewrite ^/$ /m;
}
}
需求: 用户通过手机设备访问url.oldxu.com,跳转至m.oldxu.com
server {
listen 80;
server_name url.oldxu.com;
root /data;
if ($http_user_agent ~* "android|iphone|ipad") {
rewrite ^/$ http://m.oldxu.com;
}
}
server {
listen 80;
server_name m.oldxu.com;
root /data/m;
location / {
index index.html;
}
}
#需求: 用户访问oldxu.com/test,跳转至https://xuliangwei.com
location------------------------------------------------------------------
location /test {
#rewrite ^(.*)$ https://www.xuliangwei.com/;
return 302 https://www.xuliangwei.com/;
}
if ------------------------------------------------------------------------
server {
listen 80;
server_name url.oldxu.com;
root /data;
if ($request_uri ~* "^/test") {
#rewrite ^(.*)$ https://www.xuliangwei.com/;
return 302 https://www.xuliangwei.com/;
}
location / {
index index.html;
}
}
3.rewrite场景示例: 开发
需求: 用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html
[root@web01 conf.d]# cat url.oldxu.com.conf
server {
listen 80;
server_name url.oldxu.com;
root /data;
location / {
index index.html;
#用户访问的url #文件真实位置
rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html;
}
}