标签:变量 mp4 类型 手机 exti regex name print tar
[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
#配置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;
}
}
5.集群实现动静分离实战
[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;
}
}
[root@web02 conf.d]# cat ds.old.com.conf
server {
listen 80;
server_name ds.old.com;
location / {
root /images;
[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;
}
}
将用户的请求url.oldxu.com.cn跳转至url.oldxu.com/zh
将用户的请求urk.oldxu.com.cn跳转至url.oldxu.com/jp
[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;
}
}
if指令场景示例
根据用户的浏览器使用的语言自动判断并跳转至不同的语言界面
中国 url.oldxu.com ---> url.oldxu.com --->中文
english url.oldxu.com ---> url.oldxu.com --->english
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;
}
}
if指令场景示例
nginx过滤请求中包含a1=3526的http请求到10.16.3.5的8080端口处理
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----------------------------------- 停止本次的操作,跳出循环
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
Syntax: rewrite regex replacement [flag];
跳转 :
重定向:
#flag
last #本条规则匹配完成后,继续向下匹配新的location URI规则 (开发| 伪静态)
break #本条规则匹配完成即终止,不再匹配后面的任何规则 (挂维护页)
redirect #返回302临时重定向, 地址栏会显示跳转后的地址
permanent #返回301永久重定向, 地址栏会显示跳转后的地址
中国 url.oldxu.com ---> url.oldxu.com/zh --->中文
english url.oldxu.com ---> url.oldxu.com/cn --->english
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;
? }
}
需求: 用户访问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;
}
}
flag
1.动静分离?
2.单台动静分离?
3.集群动静分离?
4.Rewrite
set
if
return URL跳转 运维
rewrite 改写URL 开发
flag:
redirect
permanent
last
break
标签:变量 mp4 类型 手机 exti regex name print tar
原文地址:https://www.cnblogs.com/baozexu/p/11581817.html