詳細nginxの(F)

著作権:ようこそ転載し、ソースを明記してください!https://blog.csdn.net/miss1181248983/article/details/89749417

7. nginxの書き換え設定(II)

コンフィグレーション書き換えnginxののnginxの構成は、URL書き換え(疑似静的)、分離移動(ジャンプドメイン、及び加速アクセスCDNを達成するために)、書き換えジャンプドメイン(リダイレクト)実装することができる、複数の中央部分です。PCREライブラリモジュールが使用ngx_http_rewrite_moduleある頼る書き換えます。


ルールを書き換えます

格式:rewrite regex replacement [flag]

* rewrite配置可以在server、location以及if配置段内生效

* regex是用于匹配的正则表达式,其不会匹配到$host(域名)

* replacement是目标跳转的uri,可以以http://或https://开头,也可以省略掉$host,直接写$request_uri部分(即请求链接)

* flag,用来设置rewrite对uri的处理行为,其中有break、last、redirect、permanent。redirect和permanent的区别在于,redirect是临时重定向(302),而permanent是永久重定向(301)。
  对于用户访问来说,两者效果一致;但对于搜索引擎爬虫来说,使用301更利于SEO。所以,建议replacement是以http://或https://开头的,flag使用permanent
  • 例1:
location / {
    rewrite /(.*) http://www.123.com/$1 permanent;
}

注意:*正規表現として、と()ように、最初の2回目の出現を呼び出すために$ 2 $ 1()の呼び出しを、()が表示されます呼び出すことができるURLの後ろに囲まれた、と。

  • 例2:
location / {
    rewrite /.* http://www.123.com$request_uri permanent;
}

説明:交換では、支持変数は、ここではリンクの$ REQUEST_URIがクライアントによって要求されるのです。

  • 例3:
server {
    listen 80;
    service_name www.123.com;
    root /tmp/123.com;
    index index.html;
    rewrite /(.*) /abc/$1 redirect;
}

説明:連続ループになり、問題がある。この場合のルールを書き換え、及びnginxのは上限50時間を有し、50回以上のサイクルが失敗します。

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.2.com.conf 

server {
    listen 80;
    server_name www.2.com;
    index index.html;
    root /data/wwwroot/www.2.com;

    location / {
        rewrite /(.*) /abc/$1 redirect;
    }
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.2.com/1.html

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

# curl -x127.0.0.1:80 www.2.com/1.html -L
curl: (47) Maximum (50) redirects followed
# curl -x127.0.0.1:80 www.2.com/1.html -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 22 Apr 2019 13:41:15 GMT
Content-Type: text/html
Content-Length: 154
Location: http://www.2.com/abc/1.html
Connection: keep-alive

# curl -x127.0.0.1:80 www.2.com/abc/1.html -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 22 Apr 2019 13:41:27 GMT
Content-Type: text/html
Content-Length: 154
Location: http://www.2.com/abc/abc/1.html
Connection: keep-alive

あなたは50倍以上のサイクルまで、それは、循環/ ABCにされている、見ることができます。

  • 例4:
server {
    listen 80;
    service_name www.123.com;
    root /tmp/123.com;
    index index.html;
    rewrite /(.*) /abc/$1 break;
}

注:リライトでのブレークを使用し、サイクルを回避することができます。

  • 例5:
server {
    listen 80;
    service_name www.123.com;
    root /tmp/123.com;
    index index.html;
    if ($request_uri !~ '^/abc/')
    {
        rewrite /(.*) /abc/$1 redirect;
    }
}

説明:条件付きの増加は、サイクルを回避することができます。

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.2.com.conf 

server {
    listen 80;
    server_name www.2.com;
    index index.html;
    root /data/wwwroot/www.2.com;

    if ($request_uri !~ '^/abc/')
    {
        rewrite /(.*) /abc/$1 redirect;
    }
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.2.com/1.html -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 22 Apr 2019 13:48:42 GMT
Content-Type: text/html
Content-Length: 154
Location: http://www.2.com/abc/1.html
Connection: keep-alive

# curl -x127.0.0.1:80 www.2.com/abc/1.html -I

HTTP/1.1 404 Not Found
Server: nginx
Date: Mon, 22 Apr 2019 13:50:21 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

あなたは条件付きたら、もはや適格、直接リダイレクト場合、循環していません。


nginxのグローバル変数

nginxの一般的に使用されるグローバル変数:

変数 説明
$ argsを などの要求パラメータ、www.123.com/1.php?a=1&b=2 $引数の= 1&B = 2であります
$のCONTENT_LENGTH 「コンテンツ長」でHTTP要求情報
$のCONTENT_TYPE 「Content-Typeの」におけるHTTPリクエスト情報
$のcontent_root 仮想ホストの設定ファイルに対応したnginxのルートパラメータ値
$ DOCUMENT_URI 現在の要求は、次のような説明書URI、含まれていませんwww.123.com/1.php?a=1&b=2 $ DOCUMENT_URIのバックのパラメータを指定せずに、1.phpです
$ホスト ホストヘッダー、そのドメイン
$ HTTP_USER_AGENT クライアントの詳細は、ブラウザのすなわちアイデンティティはカールで指定することができ-A
$ HTTP_COOKIE クライアントのクッキー情報
$ limit_rate limit_rateのnginxのサーバーは、ネットワーク構成表示レートを使用している場合は、表示され、表示が0に設定されていません
$ REMOTE_ADDR クライアントパブリックネットワークのIP
$ REMOTE_PORT クライアントのポート
$のREMOTE_USER あなたはnginxの認証を設定している場合、変数は、クライアント認証のユーザー名を表し、
$ request_body_file リバースプロキシを行うバックエンドサーバーに送信されたローカルリソースの名前
$のREQUEST_METHOD 要求されたリソースの道、GET / PUT / DELETEなど
$のREQUEST_FILENAME 現在のリクエストのリソースファイルのパス名、$ DOCUMENT_ROOT / $ DOCUMENT_URIの組み合わせの同等
$ REQUEST_URI $のDOCUMENT_URIと$ argsを含む要求へのリンク、
$スキーム FTPなどの要求されたプロトコル、HTTP、HTTPS
$のSERVER_PROTOCOL クライアントのリソース要求のプロトコルバージョンは、HTTP / 1.0、HTTP / 1.1、HTTP / 2.0などのように、使用します
$のSERVER_ADDR サーバのIPアドレス
$のSERVER_NAME サーバーのホスト名
$のSERVER_PORT サーバーのポート番号
$ sを 同じと$ DOCUMENT_URI
$ HTTP_REFERER リファラ時にクライアントの要求、人気の話は、要求がスキップすることによってリンクされ、あなたはカールを指定することができるということである-e

戦闘を書き換えます

この例では、nginxの本番環境でのシーンの一部を使用しています。

ジャンプのドメイン名(ドメイン名のリダイレクト)

  • (条件なし)実施例1:
server {
    listen 80;
    server_name www.1.com;
    rewrite /(.*) http://www.2.com/$1 permanent;
    ......
}

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf 

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    rewrite /(.*) http://www.2.com/$1 permanent;
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.1.com -I

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 24 Apr 2019 12:47:15 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.2.com/                 #301跳转到www.2.com
  • 実施例2(該当する場合):
server {
    listen 80;
    server_name www.1.com 1.com;
    if ($host != 'www.1.com') {
        rewrite /(.*) http://www.2.com/$1 permanent;
    ......
    }
}

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf 

server {
    listen 80;
    server_name www.1.com 1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    if ($host != 'www.1.com') {
    rewrite /(.*) http://www.2.com/$1 permanent;
    }
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.1.com -I

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 24 Apr 2019 12:52:24 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Sat, 06 Apr 2019 09:42:39 GMT
Connection: keep-alive
ETag: "5ca8748f-a"
Accept-Ranges: bytes                #是www.1.com时照常访问

# curl -x127.0.0.1:80 1.com -I

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 24 Apr 2019 12:52:33 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.2.com/                     #是1.com时301跳转到www.2.com
  • 実施例3(HTTPからHTTPSへのジャンプ):
server {
    listen 80;
    server_name www.1.com;
    rewrite /(.*) https://www.2.com/$1 permanent;
    ......
}

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf 

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    rewrite /(.*) https://www.2.com/$1 permanent;
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.1.com -I

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 24 Apr 2019 12:59:28 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.2.com/                 #301跳转到https://www.2.com

HTTPSを所有している場合、要求はなく、80ポートのHTTPSポート443であるため、要求は、心配しないでください。

  • 例4(二次ディレクトリドメイン名):
server {
    listen 80;
    server_name www.1.com;
    rewrite /(.*) https://www.2.com/aaa/$1 last;
    ......
}

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf 

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    rewrite /(.*) http://www.2.com/aaa/$1 last;
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.1.com -I

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Wed, 24 Apr 2019 13:05:18 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://www.2.com/aaa/                 #302跳转到http://www.2.com/aaa/
  • 実施例5(分離静的要求):
server {
    listen 80;
    server_name www.1.com;
    location ~* ^.+.(jpg|jpeg|gif|css|png|js)$
    {
        rewrite /(.*) https://www.2.com/$1 permanent;
    }
    ......
}

若しくは

server {
    listen 80;
    server_name www.1.com;
    if ( $uri ~* (jpg|jpeg|gif|css|png|js)$)
    {
        rewrite /(.*) https://www.2.com/$1 permanent;
    }
    ......
}

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf 

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    location ~* ^.+.(jpg|jpeg|gif|css|png|js)$
    {
        rewrite /(.*) http://img.2.com/$1 permanent;
    }
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 www.1.com/1.jpg -I

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 24 Apr 2019 13:22:30 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://img.2.com/1.jpg                    #301跳转到http://img.2.com/1.jpg

# curl -x127.0.0.1:80 www.1.com/abc/1.jpg -I

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 24 Apr 2019 13:21:42 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://img.2.com/abc/1.jpg                 #301跳转到http://img.2.com/abc/1.jpg

ホットリンク保護

  • 例6:
server {
    listen 80;
    server_name www.1.com;
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
    {
        valid_referers none blocked server_names *.1.com 1.com *.2.com 2.com;
        if ($invalid_referer)
        {
            rewrite /(.*) http://img.1.com/images/forbidden.png;            #或者直接 return 403;
        }
    }
    ......
}

説明:

* 这里是通配,和正则里面的 * 不是一个意思;

none 指的是referer不存在的情况(curl -e 测试);

blocked 指的是referer头部的值被防火墙或代理服务器删除或者伪装的情况,
        该情况下,referer头部的值不以http:// 或 https://开头(curl -e 后面跟的referer不以http:// 或 https://开头)。
        
curl -e 指定来源网址

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
    {
        valid_referers none blocked server_names *.1.com 1.com *.2.com 2.com;
        if ($invalid_referer)
        {
            return 403;
        }
    }
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -e "http://www.2.com/1.html" -x127.0.0.1:80 www.1.com/abc/1.jpg -I

HTTP/1.1 404 Not Found
Server: nginx
Date: Wed, 24 Apr 2019 13:50:42 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

提示404 Not Found说明没有问题

# curl -e "http://www.3.com/1.html" -x127.0.0.1:80 www.1.com/abc/1.jpg -I

HTTP/1.1 403 Forbidden
Server: nginx
Date: Wed, 24 Apr 2019 13:50:47 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

从www.3.com过来的请求直接返回403,因为http://www.3.com不是白名单中的referer

擬似静的

  • 実施例7(例えば、擬似静的清華として):
location /  {
    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 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 ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
}

書き換え条件の複数

  • 例8:
location / {
    set $rule 0;
    if ($document_uri !~ '^/abc')
    {
        set $rule "${rule}1";
    }
    if ($http_user_agent ~* 'ie6|firefox')
    {
       set $rule "${rule}2";
    }
    if ($rule = "012")
    {
        rewrite /(.*) /abc/$1 redirect;
    }
}

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;
    if ($request_uri ~ "^/abc/")
    {
        if ($http_user_agent ~ 'IE|chrome')
        {
            return 406;                 #任意定义一个状态码
        }
    }
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

nginx: [emerg] "if" directive is not allowed here in /usr/local/nginx/conf/vhost/www.1.com.conf:11
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

ので、私たちは、この書き込みが文句を言うだろう、見ることができますnginxのは、場合であれば、ネストされたサポートされていない条件の数を達成するために、そしてそうすることがあります。

設定を変更します。

# vim /usr/local/nginx/conf/vhost/www.1.com.conf

server {
    listen 80;
    server_name www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;

    rewrite_log on;

    set $rule 0;
    if ($request_uri ~ "^/abc/")
    {
        set $rule "${rule}1";
    }
    if ($http_user_agent ~ 'IE|chrome')
    {
        set $rule "${rule}2";
    }
    if ($rule = "012")
    {
        return 406;
    }   
}

コンフィギュレーションを再ロードします。

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

アクセステスト:

# curl -x127.0.0.1:80 -A "kdjshd" www.1.com/abc/1.html -I

HTTP/1.1 404 Not Found                  #返回404
Server: nginx
Date: Wed, 24 Apr 2019 14:00:47 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
# curl -x127.0.0.1:80 -A "kdjshdchrome" www.1.com/abcd/1.html -I

HTTP/1.1 404 Not Found                  #返回404
Server: nginx
Date: Wed, 24 Apr 2019 14:04:31 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
# curl -x127.0.0.1:80 -A "kdjshdchrome" www.1.com/abc/1.html -I

HTTP/1.1 406 Not Acceptable             #返回406
Server: nginx
Date: Wed, 24 Apr 2019 14:07:22 GMT
Content-Type: text/html
Content-Length: 172
Connection: keep-alive

あなたはそれが406を返します、条件の定義が満たされなければならない、見ることができます。

設定の場所

  • 文法規則:

nginxの場所の構文規則:location [=|~|~*|^~] /uri/ { … }可変ロケーションマッチングnginxのは、$ URIです。

シンボル 説明
= 完全一致
^〜 ウリは、指定した文字または文字列の先頭を表し、
これは、大文字と小文字を区別し、通常の試合を表し
〜* 大文字と小文字を区別しない正規の一致を示します
/ 一般的な一致は、任意の要求がに一致しています
  • ルールの優先順位:
=  高于  ^~  高于  ~* 等于 ~  高于  /
  • ルールの例:
location = "/12.jpg" { ... }
如:
www.1.com/12.jpg 匹配
www.1.com/abc/12.jpg 不匹配

location ^~ "/abc/" { ... }
如:
www.1.com/abc/123.html 匹配
www.1.com/a/abc/123.jpg 不匹配

location ~ "png" { ... }
如:
www.1.com/aaa/bbb/ccc/123.png 匹配
www.1.com/aaa/png/123.html 匹配

location ~* "png" { ... }
如:
www.1.com/aaa/bbb/ccc/123.PNG 匹配
www.1.com/aaa/png/123.html 匹配


location /admin/ { ... }
如:
www.1.com/admin/aaa/1.php 匹配
www.1.com/123/admin/1.php 不匹配

注意:

有些资料上介绍location支持不匹配 !~,
如: location !~ 'png'{ ... }
这是错误的,location不支持 !~

如果有这样的需求,可以通过if来实现,
如: if ($uri !~ 'png') { ... }

location优先级小于if

おすすめ

転載: blog.csdn.net/miss1181248983/article/details/89749417