1.什么是动静分离?
将动态请求和静态请求区分访问,
2.为什么要做动静分离?
静态由Nginx处理, 动态由PHP处理或Tomcat处理....
因为Tomcat程序本身是用来处理jsp代码的,但tomcat也能处理静态资源.
tomcat本身处理静态效率不高,还会带来资源开销.
3.如何实现动静分离?
Nginx根据客户端请求的url来判断请求的是否是静态资源,如果请求的url包含jpg、png,则由Nginx处理。
如果请求的url是.php或者.jsp等等,这个时候这个请求是动态的,将转发给tomcat处理。
总结来说,Nginx是通过url来区分请求的类型,并转发给不同的服务端。
4.单机实现动静分离实战
1.安装 【 tomcat监听在8080端口】
[[email protected] ~]# yum install java tomcat -y
2.创建主站点目录
[[email protected] ~]# mkdir /usr/share/tomcat/webapps/ROOT -->主要站点根目录
3.写入jsp代码
[[email protected] ~]# 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>
4.下载图片至站点目录
[[email protected] ~]# wget -O /usr/share/tomcat/webapps/ROOT/nginx.png http://nginx.org/nginx.png
5.重启tomcat服务
[[email protected] ~]# systemctl start tomcat
6.浏览器输入10.0.0.7测试验证
4.1配置Nginx 实现动静分离
[[email protected] conf.d]# cat ds.cheng.com.conf
server {
listen 80;
server_name ds.cheng.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
location ~*\.(png|gif|jpg|mp4)$ {
root /images;
expires 1d;
}
}
5.集群实现动静分离实战
1.停止10.0.0.7nginx服务
[[email protected] conf.d]# systemctl stop nginx
2.10.0.0.8操作
[[email protected] conf.d]# cat ds.cheng.com.conf
server {
listen 80;
server_name ds.cheng.com;
expires 2d;
location / {
root /images;
}
}
3.创建静态资源存放的路径以及上传图片
[[email protected] ~]# mkdir /images/
4. 负载配置:
[[email protected] conf.d]# cat proxy_ds.cheng.com.conf
upstream java {
server 172.16.1.7:8080;
}
upstream static {
server 172.16.1.8:80;
}
server {
listen 80;
server_name ds.cheng.com;
location / {
proxy_pass http://java;
include proxy_params;
}
location ~* \.(png|jpg|gif|jpeg)$ {
proxy_pass http://static;
expires 2d;
include proxy_params;
}
}
6.本章课程内容大纲
1.什么是Rewrite?
2.Rewrite使用场景?
3.Rewrite实现原理?
4.Rewrite URL重写配置场景?
10案例
5.Rewrite URL重写falg标记?
6.Rewrite URL重写生产实践?
1.set设置变量
1.将用户请求的url.cheng.com.cn重定向到url.cheng.com/zh
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.com.cn;
set $language zh;
rewrite ^/$ http://url.cheng.com/$language/;
}
server {
listen 80;
server_name url.cheng.com;
location / {
root /data;
}
}
[[email protected] conf.d]# mkdir -p /data/{zh,jp,en}
[[email protected] conf.d]# echo "zh" > /data/zh/index.html
[[email protected] conf.d]# echo "jp"> /data/jp/index.html
[[email protected] conf.d]# echo "en"> /data/en/index.html
重启服务
[[email protected] conf.d]# nginx -t
[[email protected] conf.d]# systemctl restart nginx
域名解析
10.0.0.7 url.cheng.com url.cheng.com.cn
测试【通过域名来实现跳转效果】
2.set结合if示例
1.将用户请求的url.cheng.com.jp跳转至url.cheng.com/ip
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.com.cn url.cheng.com.jp;
#判断
if ($http_host ~* cn) {
set $language zh;
}
if ($http_host ~* jp) {
set $language jp;
}
rewrite ^/$ http://url.cheng.com/$language/;
}
server {
listen 80;
server_name url.cheng.com;
location / {
root /data;
}
}
域名解析:
2.根据用户浏览器使用的语言,自动判断并跳转至不同的语言界面
中国 url.cheng.com ---> url.cheng.com --->中文
english url.cheng.com ---> url.cheng.com --->english
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.com;
location / {
if ($http_accept_language ~* "en") {
set $language en;
}
if ($http_accept_language ~* "zh|zh-CN") {
set $language zh;
}
root /data/$language;
}
}
3.nginx过滤请求中包含a1=3526的http请求到10.0.0.7的8080端口处理
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.com;
location / {
default_type text/html;
if ($request_uri ~* "a1=3526") {
return 200 "OK";
}
if ($http_accept_language ~* "en") {
set $language en;
}
if ($http_accept_language ~* "zh|zh-CN") {
set $language zh;
}
root /data/$language;
}
}
浏览器输入url.cheng.com/?a1=3526 进行测试!
3.return案例
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.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;
}
}
4.break案例
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.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;
}
}
[[email protected]]# echo "1.html" >/code/1.html
[[email protected]]# echo "2.html" >/code/2.html
[[email protected]]# echo "3.html" >/code/3.html
[[email protected]]# echo "a.html" >/code/a.html
[[email protected]]# echo "b.html" >/code/b.html
测试结果:当请求/1.html,最终会访问/2.html
因为在location{ }内部,遇到break,本location{ }内以及后面的所有location{ }内的所有指令都不在执行
set
if
return
状态码
状态码 字符串
状态码 URL 301 302
break
停止后续操作:
rewrite
关键字 正则 替代内容 flag标记
Syntax: rewrite regex replacement [flag];
跳转 :
重定向:
#flag
last #本条规则匹配完成后,继续向下匹配新的location URI规则 (开发| 伪静态)
break #本条规则匹配完成即终止,不再匹配后面的任何规则 (挂维护页)
redirect #返回302临时重定向, 地址栏会显示跳转后的地址
permanent #返回301永久重定向, 地址栏会显示跳转后的地址
中国 url.cheng.com ---> url.cheng.com/zh --->中文
english url.cheng.com ---> url.cheng.com/cn --->english
5.rewrite案例一[根据浏览器不同的语言跳转至不同的域名]
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.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;
}
}
6.永久维护
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.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;
}
}
7.临时维护(跳转至指定的域名)
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.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;
}
#error_page 403 404 500 502 /wh.png;
#error_page 403 404 500 502 $http_host;
error_page 403 404 500 502 @temperror;
location @temperror {
rewrite ^(.*)$ http://yinwucheng.com;
}
}
8.需求: 用户通过手机设备访问url.cheng.com,跳转至url.cheng.com/m
server {
listen 80;
server_name url.cheng.com;
root /data;
if ($http_user_agent ~* "android|iphone|ipad") {
rewrite ^/$ /m;
}
}
9.需求: 用户通过手机设备访问url.cheng.com,跳转至m.cheng.com
server {
listen 80;
server_name url.cheng.com;
root /data;
if ($http_user_agent ~* "android|iphone|ipad") {
rewrite ^/$ http://m.cheng.com;
}
}
server {
listen 80;
server_name m.cheng.com;
root /data/m;
location / {
index index.html;
}
}
10.需求: 用户访问cheng.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.cheng.com;
root /data;
if ($request_uri ~* "^/test") {
#rewrite ^(.*)$ https://www.xuliangwei.com/;
return 302 https://www.xuliangwei.com/;
}
location / {
index index.html;
}
}
11.http协议跳转至https协议
return 302 https://$http_host$request_uri;
11.rewrite场景示例: 开发
需求: 用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html
[[email protected] conf.d]# cat url.cheng.com.conf
server {
listen 80;
server_name url.cheng.com;
root /data;
location / {
index index.html;
#用户访问的url #文件真实位置
rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html;
}
}
原文地址:https://www.cnblogs.com/yinwu/p/11616443.html
时间: 2024-11-08 21:09:58