0
点赞
收藏
分享

微信扫一扫

四层负载均衡 动静分离和资源分离 Rewrite rewrite伪静态实例


文章目录

  • ​​四层负载均衡​​
  • ​​四层负载均衡特点​​
  • ​​查看四层负载均衡语法​​
  • ​​四层负载均衡配置​​
  • ​​优化配置文件​​
  • ​​四层负载均衡日志配置​​
  • ​​nginx的TCP负载均衡---端口转发​​
  • ​​动静分离​​
  • ​​单台的动静分离​​
  • ​​多台机器动静分离​​
  • ​​做动静分离和资源隔离的网站。​​
  • ​​一、动静分离的网站​​
  • ​​0.准备环境​​
  • ​​1)环境准备​​
  • ​​1.静态资源​​
  • ​​1.1.上传静态资源​​
  • ​​1.2.配置静态资源网站​​
  • ​​1.3.本地hosts访问​​
  • ​​2.配置动态资源​​
  • ​​2.1.安装tomcat​​
  • ​​2.2.配置动态资源网站​​
  • ​​2.3.本地hosts访问​​
  • ​​3.负载均衡设置​​
  • ​​3.1创建站点目录并授权​​
  • ​​3.2.编辑html文件​​
  • ​​3.3配置负载均衡文件​​
  • ​​3.4本地hosts访问​​
  • ​​二、资源分离的网站​​
  • ​​1.准备环境​​
  • ​​2.配置web01服务器​​
  • ​​2、创建站点目录​​
  • ​​3、访问测试​​
  • ​​3.配置web02服务器​​
  • ​​2、创建站点文件​​
  • ​​3、访问测试​​
  • ​​4.配置web03服务器​​
  • ​​2、创建站点文件​​
  • ​​3、访问测试​​
  • ​​5.配置负载均衡​​
  • ​​2、访问测试​​
  • ​​Rewrite​​
  • ​​rewrite概述​​
  • ​​rewrite使用场景​​
  • ​​rewrite配置语法​​
  • ​​rewrite标记 flag​​
  • ​​last和break的区别​​
  • ​​redirect和permanent的区别​​
  • ​​rewrite规则匹配实例​​
  • ​​用户访问`/abc/1.html`实际上真实访问的是`/ccc/bbb/2.html`​​
  • ​​用户访问`/2018/ccc/2.html`实际上真实访问的是 `/2014/ccc/bbb/2.html`​​
  • ​​用户访问`/test`实际上真实访问的是`www.baidu.com`​​
  • ​​用户访问 couese-11-22-33.html 实际上真实访问的是 /course/11/22/33/course_33.html​​
  • ​​将http请求跳转到https​​
  • ​​rewrite伪静态实例​​
  • ​​搭建discuz论坛​​
  • ​​配置hosts,访问论坛,发表帖子​​
  • ​​配置rewrite伪静态​​
  • ​​rewrite全局变量​​
  • ​​rewrite可以开启日志​​
  • ​​rewrite规则补充​​
  • ​​rewrite匹配的优先级​​
  • ​​uri和url的区别​​

四层负载均衡

四层负载均衡    动静分离和资源分离   Rewrite    rewrite伪静态实例_nginx

四层负载均衡    动静分离和资源分离   Rewrite    rewrite伪静态实例_nginx_02

1、四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;
2、负载均衡可以做端口转发
3.四层可以做:
mysql读从库的负载均衡
跳板机的端口映射

四层负载均衡    动静分离和资源分离   Rewrite    rewrite伪静态实例_负载均衡_03

四层负载均衡特点

1>、四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
2>、四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
3>、四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同事的使用)
4>、四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5>、通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。

查看四层负载均衡语法

#四层负载均衡stream模块跟http模块同级别,不能配置在http里面,重新配置
stream {
upstream backend {
server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
}

server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
}

四层负载均衡配置

#配置主配置文件里面一个包含的语句
[root@lb4 nginx]# vim nginx.conf
events {
worker_connections 1024;
}

include /etc/nginx/conf.c/*.conf;

http { ... }

#创建四层负载配置的目录
[root@lb4 nginx]# mkdir /etc/nginx/conf.c

#配置四层负载
[root@lb4 nginx]# vim /etc/nginx/conf.c/lb_proxy.conf
stream {
upstream lbserver {
server 172.16.1.4:8080;
server 172.16.1.5:80;
}

server {
listen 80;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass lbserver;
}
}

#重启nginx
[root@lb4 nginx]# systemctl restart nginx
#如果重启报错
[root@lb4 nginx]# rm -rf /etc/nginx/conf.d/*

#配置hosts访问测试

优化配置文件

[root@Nginx ~]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

四层负载均衡日志配置

[root@lb4 nginx]# vim /etc/nginx/conf.c/lb_proxy.conf
stream {
log_format main '$remote_addr $remote_port - [$time_local] $status $protocol
'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;

access_log /var/log/nginx/lb4_access.log main;

upstream lbserver {
server 172.16.1.4:80;
server 172.16.1.5:80;
}

server {
listen 80;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass lbserver;
}
}

nginx的TCP负载均衡—端口转发

#请求负载均衡的5555端口,跳转至172.16.1.7的22端口

stream {
#转发ssh的22端口
upstream ssh_7 {
server 172.16.1.7:22;
}

server {
listen 5555;
proxy_pass ssh_7;
}
}

#请求负载均衡的6666端口,跳转至172.16.1.51的3306端口
stream {
#转发mysql的3306端口
upstream mysql_51 {
server 172.16.1.51:3306;
server 172.16.1.52:3306;
server 172.16.1.53:3306;
server 172.16.1.54:3306;
server 172.16.1.55:3306;
server 172.16.1.56:3306;
server 172.16.1.57:3306;
}

server {
listen 6666;
proxy_pass mysql_51;
}
}

# nginx必须启用--with-stream

## 编写nginx配置
mkdir /etc/nginx/stream.d

## 编写配置文件
[root@lb01 stream.d]# cat mysql.conf
stream {
upstream mysql_conn {
server 172.16.1.51:3306;
}
server {
listen 33060;
proxy_pass mysql_conn;
}
}

## 重启nginx
[root@lb01 stream.d]# systemctl restart nginx

## 利用nginx转发连接MySQL
[root@db01 ~]# mysql -uyangge -pyangge -h172.16.1.5 -P 33060

动静分离

动:动态页面(php、python)
静:静态页面(html、gif)CDN(可以提高访问页面或者图片的速度)

单台的动静分离

[root@web01 conf.d]# vim blog.conf 
server {
listen 80;
server_name blog.linux.com;

location / {
root /code/wordpress;
index index.php;
}

#如果请求的是以 .jpg结尾的静态文件 就去/code/images 目录下访问
location ~* \.jpg$ {
root /code/images;
}

location ~* \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#创建目录
[root@web01 conf.d]# mkdir /code/images/

#实现动静分离
方式一:把文件挪到/code/images/
cp -r /code/wordpress/wp-content /code/images/
方式二:做软连接
cd /code
ln

多台机器动静分离

#web01静态内容
#配置nginx
[root@web01 conf.d]# vim jt.conf
server {
listen 80;
server_name dj.linux.com;

location ~* \.(jpg|png|gif)${
root /code/picture;
}
}

#重启nginx
[root@web01 conf.d]# systemctl restart nginx

#上传图片
[root@web01 conf.d]# mkdir /code/picture
[root@web01 conf.d]# cd /code/picture/
[root@web01 picture]# rz 1.jpg

#配置hosts ,访问图片

#web02动态内容

#安装tomcat
[root@web02 code]# yum install -y tomcat

#配置动态内容
[root@web02 code]# cd /usr/share/tomcat/webapps/
[root@web02 webapps]# mkdir ROOT
[root@web02 webapps]# vim ROOT/java_test.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>

#启动tomcat
[root@web02 webapps]# systemctl start tomcat

#访问页面
http://10.0.0.8:8080/java_test.jsp


#负载均衡上配置页面
#配置负载均衡的nginx
[root@lb01 conf.d]# vim dj.linux.com.conf
upstream jt {
server 172.16.1.7:80;
}
upstream dt {
server 172.16.1.9:8080;
}

server {
listen 80;
server_name dj.linux.com;

location / {
root /code/dj;
index index.html;
}

location ~* \.(jpg|gif|png)$ {
proxy_pass http://jt;
proxy_set_header HOST $http_host;
}

location ~ \.jsp$ {
proxy_pass http://dt;
proxy_set_header HOST $http_host;
}
}


#重启nginx
[root@lb01 conf.d]# systemctl restart nginx

#配置host,访问页面
http://dj.linux.com/java_test.jsp
http://dj.linux.com/1.jpg


#整合静态内容和动态内容代码
[root@lb01 conf.d]# mkdir /code/dj -p
[root@lb01 conf.d]# 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_test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src="http://dj.linux.com/1.gif">
<div id="get_data"></div>
</body>
</html>

#授权站点目录
[root@lb01 conf.d]# chown -R nginx.nginx /code/

#访问域名,测试,静态内容和动态内容关闭其一,互不影响

做动静分离和资源隔离的网站。

一、动静分离的网站

0.准备环境

1)环境准备

主机

作用

服务

地址

lb01

负载均衡

nginx proxy

172.16.1.5

web01

静态资源

nginx static

172.16.1.7

web02

动态资源

tomcat server

172.16.1.8

1.静态资源

1.1.上传静态资源

## 创建目录 并授权
[root@web01 ~]# mkdir /yeg/picture -p
[root@web01 ~]# chown -R www.www /yeg/
[root@web01 ~]# cd /yeg/picture/
[root@web01 picture]# rz上传

1.2.配置静态资源网站

[root@web01 conf.d]# vim linux.dj.conf 
server {

listen 80;
server_name linux12.dj.com;

location ~* \.(jpg|png|mp4|gif)$ {
root /yeg/picture;
}
}
## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx

1.3.本地hosts访问

1.本地配置hosts

192.168.15.7 linux12.dj.com

2、访问静态资源
http://linux12.dj.com/1.jpg

2.配置动态资源

2.1.安装tomcat

[root@web02 ~]# yum install -y tomcat

2.2.配置动态资源网站

[root@web02 ~]# cd /usr/share/tomcat/webapps

[root@web02 webapps]# mkdir ROOT
[root@web02 webapps]# vi ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>测试动态的资源</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>

[root@web01 ~]# systemctl restart tomcat

2.3.本地hosts访问

1、配置本地hosts
192.168.15.8 linux12.dj.com

2、访问动态资源
http://linux12.dj.com:8080/java_test.jsp

3.负载均衡设置

3.1创建站点目录并授权

[root@lb01 opt]# mkdir -p /yeg/dj
[root@lb01 opt]# chown -R www.www /yeg/

3.2.编辑html文件

[root@lb01 ~]# vim /yeg/dj/index.html
<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://linux12.dj.com/java_test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("小姐姐,断网了,重新检查网络再来哦~");
}
});
});
</script>
<body>
<h1>测试动静分离---霉霉</h1>
<img src="http://linux12.dj.com/1.jpg">
<div id="get_data"></div>
</body>
</html>

3.3配置负载均衡文件

[root@lb01 conf.d]# vim linux.dj.conf 
server {
listen 80;
server_name linux12.dj.com;

location / {
root /yeg/dj;
index index.html;
}

location ~* \.(jpg|png|gif)$ {
proxy_pass http://192.168.15.7;
include proxy_params;
}

location ~* \.(php|jsp)$ {
proxy_pass http://192.168.15.8:8080;
include proxy_params;
}
}

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx

3.4本地hosts访问

1、配置本地hosts

192.168.15.5 linux12.dj.com

四层负载均衡    动静分离和资源分离   Rewrite    rewrite伪静态实例_html_04

二、资源分离的网站

1.准备环境

主机

IP

主机角色

条件

web01

192.168.15.7

Android页面

关闭防火墙和selinux

web02

192.168.15.8

iPhone页面

关闭防火墙和selinux

web03

192.168.15.9

PC端页面

关闭防火墙和selinux

lb01

192.168.15.5 172.16.1.5

负载均衡

关闭防火墙和selinux

2.配置web01服务器

# 1、配置nginx
[root@web01 conf.d]# cat linux.sj.com.conf
server {
listen 80;
server_name linux12.dj.com;
charset utf8;

location / {
root /yang/android;
index index.html;
}
}

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx

2、创建站点目录

[root@web01 ~]# mkdir -p /yang/android
[root@web01 ~]# echo "我是android" >> /yang/android/index.html
[root@web01 ~]# chown -R www.www /yang/android/

3、访问测试

# 1.配置hosts

3.配置web02服务器

1、配置nginx
[root@web02 conf.d]# cat linux12.sj.com.conf
server {
listen 80;
server_name linux12.dj.com;
charset utf8;

location / {
root /yang/iphone;
index index.html;
}
}
## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx

2、创建站点文件

[root@web02 ~]# mkdir -p /yang/iphone
[root@web02 ~]# echo "我是Iphone" >> /yang/iphone/index.html
[root@web02 ~]# chown -R www.www /yang/iphone/

3、访问测试

# 1.配置hosts

4.配置web03服务器

# 1、配置nginx
[root@web03 conf.d]# cat linux12.sj.com.conf
server {
listen 80;
server_name linux12.dj.com;
charset utf8;

location / {
root /yang/pc;
index index.html;
}
}

## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx

2、创建站点文件

[root@web03 ~]# mkdir -p /yang/pc 
[root@web03 ~]# echo "我是pc端" >> /yang/pc/index.html
[root@web03 ~]# chown -R www.www /yang/pc

3、访问测试

# 1.配置hosts

5.配置负载均衡

# 1.配置nginx  -01
[root@lb01 conf.d]# vim linux12.sj.com.conf
upstream android {
server 192.168.15.7:8081;
}

upstream iphone {
server 192.168.15.8:8082;
}

upstream pc {
server 192.168.15.9:8083;
}

server {
listen 80;
server_name linux12.dj.com;

location / {
if ($http_user_agent ~* "Android") { #判断如果是安卓端
proxy_pass http://android; #代理到android虚拟主机池
}
if ($http_user_agent ~* "iPhone") { #判断如果是苹果端
proxy_pass http://iphone; #代理到iphone虚拟主机池
}
if ($http_user_agent ~* "Trident") { #判断如果是IE浏览器
return 403; #直接返回403
}
proxy_pass http://pc; #如果没有匹配到以上内容,默认都代理到pc虚拟主机池
}
}

# 1.配置nginx -02
[root@lb01 conf.d]# vim linux12.sj.com.conf
server {
listen 80;
server_name linux12.dj.com;

location / {
if ($http_user_agent ~* "Android") { #判断如果是安卓端
proxy_pass http://192.168.15.7; #代理到android虚拟主机池
}
if ($http_user_agent ~* "iPhone") { #判断如果是苹果端
proxy_pass http://192.168.15.8; #代理到iphone虚拟主机池
}
if ($http_user_agent ~* "WOW64") { #判断如果是IE浏览器
return 403; #直接返回403
}
proxy_pass http://192.168.15.9; #如果没有匹配到以上内容,默认都代理到pc虚拟主机池
include proxy_params;
}
}


## 检查nginx -t 并重启
[root@web01 ~]# systemctl restart nginx

2、访问测试

# 1.配置hosts

四层负载均衡    动静分离和资源分离   Rewrite    rewrite伪静态实例_html_05

Rewrite

rewrite概述

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

rewrite使用场景

1、地址跳转,用户访问www.drz.com这个URL是,将其定向至一个新的域名mobile.drz.com
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入

rewrite配置语法

Syntax:  rewrite regex replacement [flag];
Default: —
Context: server, location, if

#一般用于切换维护场景
rewrite ^(.*)$ /page/404.html last;

rewrite标记 flag

flag

作用

last

本条规则匹配完成后,停止匹配,不再匹配后面的规则

break

本条规则匹配完成后,停止匹配,不再匹配后面的规则

redirect

返回302临时重定向,地址栏会显示跳转后的地址

permanent

返回301永久重定向,地址栏会显示跳转后的地址

last和break的区别

#配置nginx
[root@web01 conf.d]# vim rewrite.con
server {
listen 80;
server_name rw.linux.com;
root /code/rewrite;

location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 "ok";
}
}

#重启nginx
[root@web01 conf.d]# systemctl restart nginx

#访问页面测试




break 只要匹配到规则,就回去本地路径目录中寻找请求的文件;
last 匹配到规则,跳转后没有内容,则带着跳转后的请求,重新的向server发起一次请求

break请求:
1.请求rw.linux.com/break;
2.首先,会去查找本地的/code/rewrite/test/index.html;
3.如果找到了,则返回/code/rewrite/test/index.html内容;
4.如果没有找到则返回404,找到目录却没有主页,则返回403;

last请求:
1.请求rw.linux.com/last;
2.首先,会去查找本地的/code/rewrite/test/index.html;
3.如果找到了,则返回/code/rewrite/test/index.html内容;
4.如果没找到,会带着新跳转的URI再向server发起一次请求,请求rw.linux.com/test;

四层负载均衡    动静分离和资源分离   Rewrite    rewrite伪静态实例_负载均衡_06

redirect和permanent的区别

[root@web01 conf.d]# cat rewrite.conf 
server {
listen 80;
server_name rw.linux.com;
root /code/rewrite;

location /test {
rewrite ^(.*)$ http://www.mumusir.com redirect;
#rewrite ^(.*)$ http://www.mumusir.com permanent;
}
}

#配置两种跳转,关闭nginx测试,查看结果

rewrite规则匹配实例

1、通过192.168.15.7/index/1/2/3/4/5/6.html 访问更目录下的index-1-2-3-4-5-6.html
server {
listen 80;
server_name _;
root /www/resources;
location / {
rewrite ^/index/([0-9])/([0-9])/([0-9])/([0-9])/([0-9])/([0-9]) /index-$1-$2-$3-$4-$5-$6.html break;
}
}

2、使用192.168.15.7/jd访问www.jd.com
server {
listen 80;
server_name _;
root /www/resources;
location / {
rewrite ^/(.*) http://www.$1.com redirect;
}
}



3、根目录有index-test.html和xxx-abc.html, 怎样通过192.168.15.7/index/test访问index-test.html, 使用192.168.15.7/xxx/abc访问xxx-abc.html
server {
listen 80;
server_name _;
root /www/resources;
location / {
rewrite ^/(.*)/(.*) /$1-$2.html break;
}

}
# 格式
rewrite [匹配规则] [转发内容]

用户访问​​/abc/1.html​​​实际上真实访问的是​​/ccc/bbb/2.html​

[root@web01 conf.d]# vim rw.conf 
server {
listen 80;
server_name rw.linux.com;
root /code;

location ~ /abc {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
#rewrite /abc/1.html /ccc/bbb/2.html redirect;
}
}

#/abc/1.html /ccc/bbb/1.html
location ~ /abc {
rewrite /abc/(.*)\.html /ccc/bbb/$1.html redirect;
}

用户访问​​/2018/ccc/2.html​​​实际上真实访问的是 ​​/2014/ccc/bbb/2.html​

[root@web01 conf.d]# mkdir /code/2014/ccc/bbb/ -p
[root@web01 conf.d]# echo "2014-ccc-bbb-222" > /code/2014/ccc/bbb/2.html
server {
listen 80;
server_name rw.linux.com;
root /code;

location ~ /2018 {
rewrite /2018/ccc/2.html /2014/ccc/bbb/2.html redirect;
}
}

#/2018/ccc/bbb/2.html 跳转 /2014/ccc/bbb/2.html
location ~ /2018 {
rewrite /2018/(.*) /2014/$1 redirect;
}

用户访问​​/test​​​实际上真实访问的是​​www.baidu.com​

server {
listen 80;
server_name rw.linux.com;
root /code;

location ~ /test {
rewrite (.*) https://www.baidu.com redirect;
}
}

用户访问 couese-11-22-33.html 实际上真实访问的是 /course/11/22/33/course_33.html

[root@web01 conf.d]# mkdir /code/course/11/22/33/ -p
[root@web01 conf.d]# echo "course-111-222-333" > /code/course/11/22/33/course_33.html
[root@web01 conf.d]# vim rw.conf
server {
listen 80;
server_name rw.linux.com;
root /code;

location / {
#rewrite ^/course-11-22-33.html /course/11/22/33/course_33.html redirect;
rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html redirect;
}
}

将http请求跳转到https

server {
listen 80
server_name www.mumusir.com;
#rewrite (.*) https://www.mumusir.com redirect;
return 302 https://www.mumusir.com;
}

http://www.mumusir.com --> https://www.mumusir.com

server {
listen 443;
server_name www.mumusir.com;
ssl on;
ssl...... *.key;
ssl..... *.crt;
}

rewrite伪静态实例

搭建discuz论坛

#创建站点目录
[root@web01 ~]# mkdir /code/discuz
[root@web01 code]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/

#授权站点目录
[root@web01 code]# chown -R www.www /code/discuz/

#配置discuz论坛的配置文件
[root@web01 conf.d]# cp blog.conf discuz.linux.com.conf
server {
listen 80;
server_name discuz.linux.com;

location / {
root /code/discuz/upload;
index index.php;
}

location ~* \.php$ {
root /code/discuz/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

#配置hosts访问

#创建数据库
[root@db02 ~]# mysql -u root -p123456
。。。。。。

MariaDB [(none)]> create database discuz charset utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on discuz.* to discuz@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]>

配置hosts,访问论坛,发表帖子

#查看帖子地址http://discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=

配置rewrite伪静态

[root@web01 conf.d]# vim /etc/nginx/conf.d/discuz.linux.com.conf 

server {
listen 80;
server_name discuz.linux.com;

location / {
root /code/discuz/upload;
index index.php;
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
if (!-e $request_filename) {
return 404;
}
}

location ~* \.php$ {
root /code/discuz/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

[root@web01 conf.d]# systemctl restart nginx

http://discuz.linux.com/thread-1-1-1.html

rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

discuz.linux.com/thread-1-1-1.html
discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=page%3D1&page=1

rewrite全局变量

$server_name        #当前用户请求的域名
server {
listen 80;
server_name rw.linux.com;
root /code;
rewrite ^(.*)$ https://$server_name;
}

$request_filename #请求的文件路径和名字(带着网站站点目录的路径和文件 /code/images/1.jpg)
$request_uri #请求的文件路径和名字(不带网站站点目录的路径和文件 /images/1.jpg)

server {
listen 80;
server_name rw.linux.com;
root /code;
rewrite ^(.*)$ https://$server_name$request_uri;
}


#很久很久以前,网站优化
server {
listen 80;
server_name www.baidu.com baidu.com;
root /code;
if ($http_host = baidu.com) {
rewrite (.*) http://www.baidu.com;
}
}

#实际写法
server {
listen 80;
server_name baidu.com;
rewrite (.*) http://www.baidu.com;

}
server {
listen 80;
server_name www.baidu.com;
root /code;
}

rewrite可以开启日志

#NGINX主配置文件,错误日志级别改成notice
error_log /var/log/nginx/error.log notice;

#http层开启rewrite日志
rewrite_log on;

rewrite规则补充

rewrite匹配的优先级

1.先执行server模块的rewrite指令
2.其次执行location匹配规则
3.最后执行location里面的rewrite

server {
listen 80;
server_name rw.linux.com;
#rewrite ^(.*)$ http://www.jingdong.com;

location /test {
rewrite ^(.*)$ http://www.mumusir.com;
}

location =/ {
rewrite ^(.*)$ http://www.baidu.com;
}
}

uri和url的区别

http://192.168.15.7/video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html


url : http://192.168.15.7/video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html
uri :


举报

相关推荐

0 条评论