nginxの書き換え書き換えを実現

基本的な概要を書き直し

リライトとは何ですか

URLリライトの主な成果を書き換え、着信をすることですリダイレクトweb他に要求をリダイレクトするurlプロセス。


利用シナリオを書き直し

1、転送、ユーザアクセスwww.drz.com新しいドメイン名mobile.drz.comに導く、このURL
httpプロトコルによって2、プロトコルジャンプ、ユーザーが要求したときに、サイトを再ジャンプになるまでhttpsの合意
より高いセキュリティを強化するためにあまりにも多くのパラメータへの外部被ばくの動的なURLアドレスに基づいて構築しながら、3、擬似静的、動的なページは、静的なページのための技術として、検索エンジンを簡単に入力するための方法を表示します。
4、検索エンジン、SEOの最適化は、知恵の検索エンジンのエントリを覚えへのURLが簡単、URLパスに依存します


書き換え構成例

句法:Syntax:  rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if

#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;

書き換えフラグFlag

rewrite発現リダイレクトするための命令URL列を変更または、に適用することができるserver,location,if環境、各列rewriteと最後の命令flagタグ、サポートされているflag次の表に示した数字:

効果
最終 このルールの試合が終了した後、試合を停止し、もはやバックルールに一致しません
ブレーク このルールの試合が終了した後、試合を停止し、もはやバックルールに一致しません
リダイレクト ジャンプアドレスバーが表示された後、302の一時的なリダイレクトのリターンアドレス
恒久 301永久リダイレクト、ジャンプアドレスバーが表示された後、アドレスを返却

比較例は、最後の休憩は異なり

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

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

[root@web01 conf.d]#  mkdir  /code
[root@web01 conf.d]# echo gjy.test_web01 > /code/test/index.html
#重启nginx服务
[root@web01 conf.d]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload

Webブラウザをご覧ください

あなたは、2つのスクリプトに似ているシェルスクリプトを、知っている場合は、壊れて続行

ブラウザアクセスブレーク


最後にアクセスするためのブラウザ


最後の休憩違い

限りマッチがルールを破るように、それは要求されたドキュメントのパスを見つけるために、ローカル設定ディレクトリに移動します。
そしてルールに限り試合を最後に、それがどこのサーバ(...)は、ラベルの再起動要求されます。

break请求:
1、请求rewrite.gjy.com/break
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403

last请求:
1、请求rewrite.gjy.com/last
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到,会对当前server重新的发起一次请求,rewrite.gjy.com/test/
5、如果有location匹配上,则直接返回该location的内容。
4、如果也没有location匹配,再返回404;

対応する要求ディレクトリ/テストは存在しなかったものの、/休憩および/最後の要求にアクセスしたときしたがって、理論的には、404を返す必要がありますが、実際の要求/最後の時間に、後ろの場所にあり照合されます結果は、これはそれである理由が返されます。


比較例は、永久的なリダイレクトを異なります

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

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

永久的な差をリダイレクト(HTTPSを達成します)

redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。

permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

練習のルールを書き換え

書き換えルールを書く前に、私たちは、デバッグのためのルールに一致するログを書き換える開く必要があります。

[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;

http{
    rewrite_log on;
}

ケースワン

ユーザー・アクセスは/abc/1.html、実際に真でアクセスできます/ccc/bbb/2.html

#http://www.drz.com/abc/1.html  ==>  http://www.drz.com/ccc/bbb/2.html

#1.准备真实访问路径
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html

#2.Nginx跳转配置
[root@web03 ~]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;
        server_name  www.ccbb.com;
        
        location / {
                root /code;
                index index.html;
        }
        location /abc {
                rewrite (.*) /ccc/bbb/2.html redirect;
                #return 302 /ccc/bbb/2.html;
        }
}

#3.重启Nginx服务
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload


ケースII

ユーザー・アクセスは/2018/ccc/2.html、実際に真でアクセスできます/2014/ccc/bbb/2.html

##http://www.ccbb.com/2018/ccc/bbb/2.html  ==>  http://www.ccbb.com/2014/ccc/bbb/2.html

#1.准备真是的访问路径
[root@web03 conf.c]# mkdir /code/2014/ccc/bbb -p 
[root@web03 conf.c]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html

#2.Nginx跳转配置
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;

        location / {
                root /code;
                index index.html;
        }
        location /2018 {
                rewrite ^/2018/(.*)$ /2014/$1 redirect;
        }
}

#3.重启nginx服务
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload


ケースIII

ユーザーアクセス/テスト・アクセスは、実際には真であるhttps://www.baidu.com

#1.Nginx跳转配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;

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

#2.重启nginx服务
[root@web03 conf.d]# nginx -s reload


ケース四

ユーザー・アクセスはcourse-11-22-33.html、実際に真でアクセスできます/course/11/22/33/course_33.html

#http://www.drz.com/course-11-22-33.html  ==>  http://www.drz.com/course/11/22/33/course_33.html

#1.准备真是的访问路径
[root@web03 ~]# mkdir /code/course/11/22/33 -p
[root@web03 ~]# echo "curl docs.etiantian.org" > /code/course/11/22/33/course_33.html

#2.Nginx跳转配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
        root /code;
        index index.html;
        location / {
                #灵活配法
                rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
                #固定配法
                #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }
}

#3.重启nginx服务
[root@web03 conf.d]# nginx -s reload


ケース5

httpジャンプするための要求https

#Nginx跳转配置
server {
        listen 80;
        server_name www.drz.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;
}       

server {
        listen 443;
        server_name www.driverzeng.com;
        ssl on;
}

シナリオの例を書き換え

#部署discuz论坛
[root@web01 conf.d]# vim discuz.drz.com.conf
server {
        listen 80;
        server_name discuz.gjy.com;

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

        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 ~]# mkdir /code/discuz
[root@web01 ~]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/

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

#创建数据库
[root@db01 ~]# yum install -y mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# mysqladmin -uroot password '123'
[root@db01 ~]# mysql -uroot -p123
#创建库
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)
#授权库
MariaDB [(none)]> grant all on discuz.* to discuz@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

#测试远程连接数据库
[root@web01 ~]# yum install -y mariadb
[root@web01 ~]# mysql -udiscuz -p123 -h172.16.1.51
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> \q
Bye





設定擬似静的なページをチェックしてください

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

        location / {
                root /code/discuz/upload;
                index index.php index.html;
                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;
                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;
        }
}
将http请求跳转到https
    

discuz解释rewrite

thread-1-1-1.html

rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ 

$1/forum.php?mod=viewthread&tid=1&extra=page%3D1&page=1 last;

書き換えルールサプリメント

マッチングの優先順位を書き換え

1.最初のブロックの書き換え命令実行サーバ
2.次に実行位置マッチングルール
最後3、実行場所書き換え


nginxの書き換えとグローバル変数

マッチング処理に書き換え、nginxのは、いくつかのグローバル変数を使用します

$server_name    #当前用户请求的域名

server {
        listen 80;
        server_name test.drz.com;
        rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)

#大多数用于http协议转gttps协议
server {
        listen 80;
        server_name php.drz.com;
        return 302 https://$server_name$request_uri;
}
$scheme 用的协议,比如http或者https

より標準化の書き方のルールを書き換え

server {
        listen 80;
        server_name www.drz.com drz.com;
        if ($http_host = drz.com){
            rewrite (.*) http://www.drz.com$1;
        }
}

#推荐书写格式
server {
        listen 80;
        server_name drz.com;
        rewrite ^ http://www.drz.com$request_uri;
}
server {
        listen 80;
        server_name www.drz.com;
}

優先度の戦闘を書き換えます

と環境

[root@web01 conf.d]# vim youxianji.conf
server {
        listen 80;
        server_name tz.gjy.com;

        rewrite (.*) http://www.baidu.com break;
      
        location / {
                rewrite (.*) http://www.jd.com redirect;
        }
        location /test {
                rewrite (.*) http://www.driverzeng.com;
        }

}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload

サーバ層においては、時間の書き換えで構成され、位置層が有効になりません。

ブラウザアクセス、tz.gjy.com、外観があるhttps://www.baidu.com

ブラウザアクセス、tz.gjy.com /テスト、外観やhttps://www.baidu.com

サーバの層構成・リライトは、有効にする場所層コメント2

[root@web01 conf.d]# cat youxianji.conf 
server {
        listen 80;
        server_name tz.gjy.com;

        #rewrite (.*) http://www.baidu.com break;

        location / {
                rewrite (.*) http://www.jd.com redirect;
        }
        location /test {
                rewrite (.*) http://www.driverzeng.com;
        }

}

アクセスtz.gjy.com再び、場所層にアクセスする必要がありますhttps://www.jd.com/

再び、https://www.driverzeng.com/にアクセスする必要がありますtz.gjy.com/testアクセス

おすすめ

転載: www.cnblogs.com/gongjingyun123--/p/11432521.html