Nginx动静分离基本概述
动静分离,通过中间件将动静分离和静态请求进行分离;
通过中间件将动态请求和静态请求分离,可以建上不必要的请求消耗,同事能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
动静分离只有好处:动静分离后,即使动态服务不可用,但静态资源不会受到影响。
1.单台机器动静分离
1)配置nginx
server {
listen 80;
server_name blog.linux.com;
location / {
root /code/wordpress;
index index.php;
}
location ~* .*\.(jpg|png|mp4|gif)$ {
root /data/wordpress;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2.多台机器动静分离
0)环境准备
环境准备
| 系统 | 作用 | 服务 | 地址 |
|---|---|---|---|
| Centos7.5 | 负载均衡 | nginx proxy | 10.0.0.5 |
| Centos7.5 | 静态资源 | nginx static | 10.0.0.7 |
| Centos7.5 | 动态资源 | tomcat server | 10.0.0.8 |
1)在web01上面配置静态资源
#配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/dj.linux.com.conf
server {
listen 80;
server_name dj.linux.com;
location ~* .*\.(jpg|png|gif) {
root /data/file;
}
}
#上传静态资源
[root@web01 ~]# mkdir /data/file
[root@web01 ~]# cd /data/file/
[root@web01 file]# rz 1.jpg
#配置host访问测试
http://dj.linux.com/1.jpg
#查看错误日志验证配置
2)在web02上面配置动态资源
#安装tomcat
[root@web02 ~]# yum install -y tomcat
#配置动态程序
[root@web02 ~]# mkdir /usr/share/tomcat/webapps/ROOT
[root@web02 ~]# vim /usr/share/tomcat/webapps/ROOT/java.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>JSP Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
#访问测试
http://10.0.0.8:8080/java.jsp
3)在lb01上配置负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/dj.linux.com.conf
upstream jt {
server 172.16.1.7:80;
}
upstream dt {
server 172.16.1.8:8080;
}
server {
listen 80;
server_name dj.linux.com;
location ~* \.(jpg|png|gif)$ {
proxy_pass http://jt;
proxy_set_header Host $http_host;
}
location ~ \.jsp$ {
proxy_pass http://dt;
proxy_set_header Host $http_host;
}
}
4)把静态资源和动态资源整合在一个页面
[root@lb01 ~]# mkdir /code/dj -p
[root@lb01 ~]# vim /code/dj/index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</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://dj.linux.com/java.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>带你测试动静分离</h1>
<img src="http://dj.linux.com/1.jpg">
<div id="get_data"></div>
</body>
</html>
3.Nginx资源分离场景实践
0)环境准备
| 主机名 | 主机角色 | 外网IP | 内网IP | 提供端口 |
|---|---|---|---|---|
| lb01 | 负载均衡 | 10.0.0.5 | 172.16.1.5 | 80 |
| web01 | 提供Android页面 | 172.16.1.7 | 80 | |
| web02 | 提供Iphone页面 | 172.16.1.8 | 80 | |
| web03 | 提供pc页面 | 172.16.1.9 | 80 |
1)配置web01
#配置nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/sj.linux.com.conf
server {
listen 80;
server_name sj.linux.com;
charset 'utf-8';
location / {
root /code/android;
index index.html;
}
}
[root@web01 ~]# systemctl restart nginx
#配置站点
[root@web01 ~]# mkdir /code/android
[root@web01 ~]# echo "我是android" > /code/android/index.html
2)配置web02
[root@web02 ~]# vim /etc/nginx/conf.d/sj.linux.com.conf
server {
listen 80;
server_name sj.linux.com;
charset 'utf-8';
location / {
root /code/iphone;
index index.html;
}
}
[root@web02 ~]# systemctl restart nginx
#配置站点
[root@web02 ~]# mkdir /code/iphone
[root@web02 ~]# echo "我是iphone" > /code/iphone/index.html
3)配置web03
[root@web03 ~]# vim /etc/nginx/conf.d/sj.linux.com.conf
server {
listen 80;
server_name sj.linux.com;
charset 'utf-8';
location / {
root /code/pc;
index index.html;
}
}
[root@web03 ~]# systemctl restart nginx
#配置站点
[root@web03 ~]# mkdir /code/pc
[root@web03 ~]# echo "我是computer" > /code/pc/index.html
4)配置负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/sj.linux.com.conf
upstream anzhuo {
server 172.16.1.7:80;
}
upstream iphone {
server 172.16.1.8:80;
}
upstream pc {
server 172.16.1.9:80;
}
server {
listen 80;
server_name sj.linux.com;
include proxy_params;
location / {
if ($http_user_agent ~* Android) {
proxy_pass http://anzhuo;
}
if ($http_user_agent ~* iPhone) {
proxy_pass http://iphone;
}
proxy_pass http://pc;
}
}
5)生产中的配置
server {
listen 80;
server_name www.jd.com;
if ($http_user_agent ~* "Android|Iphone") {
rewrite ^(.*)$ https://m.jd.com/ redirect;
}
}