nginx 의 rewrite

13778 단어 nginx
rewrite 의 break 와 last
두 명령 의 용법 은 기본적으로 같 지만 의미 가 다 르 기 때문에 rewrite 규칙 의 끝 에 두 어야 합 니 다. 다시 쓴 링크 가 nginx 설정 에 의 해 계속 실 행 될 지 여 부 를 제어 할 수 있 습 니 다 (주로 rewrite, return 명령).
편집 www. 1. com. conf

[root@k8s-node01 vhost]# vi www.1.com.conf
server {

       listen 80;
       server_name www.1.com;
       root /data/wwwroot/www.1.com;
       rewrite_log on;
       rewrite /1.html /2.html;
       rewrite /2.html /3.html;
       }

 html 파일 만 들 기
[root@k8s-node01 conf]# cd /data/wwwroot/www.1.com/
[root@k8s-node01 www.1.com]# vi 1.html
[root@k8s-node01 www.1.com]# vi 2.html
[root@k8s-node01 www.1.com]# vi 3.html

방문 하 다.
[root@k8s-node01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@k8s-node01 www.1.com]# curl -x127.0.0.1:80 www.1.com/1.html
333333333333333333

오류 로 그 를 보고 3. html 에 접근 합 니 다.
7.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:24:40 [notice] 16573#0: *2 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:24:40 [notice] 16573#0: *2 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:24:40 [notice] 16573#0: *2 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"

브레이크 까지.
server {

       listen 80;
       server_name www.1.com;
       root /data/wwwroot/www.1.com;
       rewrite_log on;
       rewrite /1.html /2.html;break;
       rewrite /2.html /3.html;
       }

[root@k8s-node01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.1.com/1.html
22222222
#   2  
#   break  last    
#  local       


location
server {

       listen 80;
       server_name www.1.com;
       root /data/wwwroot/www.1.com;
       rewrite_log on;
       location / {
           rewrite /1.html /2.html;
           rewrite /2.html /3.html;
       }
       location /2.html {
           rewrite /2.html /a.html;
       }
       location /3.html {
           rewrite /3.html /b.html;
       }

  }

[root@k8s-node01 logs]# curl -x127.0.0.1:80 www.1.com/1.html

404 Not Found

404 Not Found


nginx/1.14.0
2019/08/21 21:45:08 [notice] 16917#0: *6 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 "/3.html" matches "/3.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 rewritten data: "/b.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 "/1.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [notice] 16917#0: *6 "/2.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:45:08 [error] 16917#0: *6 open() "/data/wwwroot/www.1.com/b.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
#//   ,/1.html     /1.html,  /2.html。/2.html     /2.html,  /3.html。
#//3.html   location /3.html,  /b.html。/b.html            /1.html,/2.html
#//    

브레이크 를 넣다
server {

       listen 80;
       server_name www.1.com;
       root /data/wwwroot/www.1.com;
       rewrite_log on;
       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@k8s-node01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.1.com/1.html
22222222
#   www.2.com
2019/08/21 21:59:17 [notice] 17201#0: *7 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/08/21 21:59:17 [notice] 17201#0: *7 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
#   break      ,    last  location    ,       

rewrite 중 return
#//     
[root@k8s-node01 vhost]# vi default.conf 

server {

      listen 80 default_server;
        return 403;
       }
                                                                                  
             
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 dlfkakf 

403 Forbidden

403 Forbidden


nginx/1.14.0
#//if   
[root@k8s-node01 vhost]# vi www.1.com.conf
server {

       listen 80;
       server_name www.1.com;
       root /data/wwwroot/www.1.com;
       rewrite_log on;
       if ( $request_uri ~ "\.htpasswd|\.bak" )
       {
             return 404;
              rewrite /(.*) /aaa.txt;  #         。 
       }
     }

[root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.1.com/.htppasswd

404 Not Found

404 Not Found


nginx/1.14.0
#//     
server{
    listen 80;
    server_name www.lsy.com;
    return 200 "hello";
}
#//         ,        ,     。
#//     json  

location ^~ /lsy{
    default_type application/json ;
    return 200  '{"name":"lsy","id":"100"}';
}

#//        

location /test {
    return 200 "$host $request_uri";
}
#//  url
server{
    listen 80;
    server_name www.lsy.com;
    return http://www.lsylinux.com/123.html;
    rewrite /(.*) /abc/$1;  //         。
}

rewrite 구체 적 인 규칙
형식: rewrite regex replacement [flag]
* rewrite 설정 은 server, location 및 if 설정 세그먼트 에서 유효 합 니 다.
* regex 는 URI 와 일치 하 는 정규 표현 식 으로 $host (도 메 인 이름) 와 일치 하지 않 습 니 다.
* replacement 은 목표 가 이동 하 는 URI 로 http: / 또는 https: / / 로 시작 할 수도 있 고 $host 를 생략 하고 $request 를 직접 쓸 수도 있 습 니 다.uri 부분 (즉 요청 한 링크) * flag 는 rewrite 가 URI 에 대한 처리 행 위 를 설정 하 는 데 사 용 됩 니 다. 그 중에서 break, last, rediect, permanent 가 있 습 니 다. rediect 와 permanent 의 차 이 는 전 자 는 임시 방향 (302) 이 고 후 자 는 영구적 으로 방향 (301) 을 바 꾸 며 사용자 가 브 라 우 저 를 통 해 방문 하 는 데 이 두 가지 효 과 는 일치 합 니 다.하지만 검색엔진 거미 파충 류 는 다 르 기 때문에 301 을 사용 하 는 것 이 SEO 에 유리 하 다.따라서 replacemnet 은 http: / 또는 https: / / 로 시작 하 는 flag 로 permanent 를 사용 하 는 것 을 권장 합 니 다.
location / {
    rewrite /(.*) http://www.lsy.com/$1 permanent;
}
#  :.*      , ()   ,    URI      ,      () $1  ,
#      () $2  ,    。
location / {
    rewrite /.* http://www.lsy.com$request_uri permanent;
}
# replacement ,    ,   $request_uri          
server{
    listen 80;
    server_name www.123.com;
    root /tmp/123.com;
    index index.html;
    rewrite /(.*) /abc/$1 redirect;
}
#    rewrite     ,      ,     ,          。
#      ,     ,curl    50 ,chrome   80 ,IE   120 ,firefox   20 。

#  
[root@k8s-node01 vhost]# vi www.2.com.conf 
server {

       listen 80;
       server_name www.2.com;
       root /data/wwwroot/www.2.com;
      location /
      {

         rewrite /(.*) /abc/$1 redirect;
       }
       }
                 
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.2.com

302 Found

302 Found


nginx/1.14.0
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.2.com -L curl: (47) Maximum (50) redirects followed [root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.2.com -I HTTP/1.1 302 Moved Temporarily Server: nginx/1.14.0 Date: Tue, 27 Aug 2019 15:11:36 GMT Content-Type: text/html Content-Length: 161 Location: http://www.2.com/abc/ Connection: keep-alive [root@k8s-node01 vhost]# curl -x127.0.0.1:80 www.2.com/abc -I HTTP/1.1 302 Moved Temporarily Server: nginx/1.14.0 Date: Tue, 27 Aug 2019 15:12:01 GMT Content-Type: text/html Content-Length: 161 Location: http://www.2.com/abc/abc Connection: keep-alive # server{ listen 80; server_name www.123.com; root /tmp/123.com; index index.html; rewrite /(.*) /abc/$1 break; } # rewrite break, 。 server{ listen 80; server_name www.123.com; root /tmp/123.com; index index.html; if ($request_uri !~ '^/abc/') { rewrite /(.*) /abc/$1 redirect; } } # ,

rewrite
도 메 인 이름 이동
#      
server{
    listen 80;
    server_name www.lsylinux.com;
    rewrite /(.*) http://www.lsy.com/$1 permanent;
    .......
    
}

#    
server{
    listen 80;
    server_name www.lsylinux.com aminglinux.com;
    if ($host != 'www.lsylinux.com')
    {
        rewrite /(.*) http://www.lsylinux.com/$1 permanent;
    }
    .......
    
}
#http   https
server{
    listen 80;
    server_name www.lsylinux.com;
    rewrite /(.*) https://www.aminglinux.com/$1 permanent;
    .......
    
}
#        
server{
    listen 80;
    server_name bbs.lsylinux.com;
    rewrite /(.*) http://www.lsylinux.com/bbs/$1 last;
    .......
    
}
#      
server{
    listen 80;
    server_name www.lsylinux.com;
    location ~* ^.+.(jpg|jpeg|gif|css|png|js)$ 
    {
        rewrite /(.*) http://img.lsylinux.com/$1 permanent;
    }

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

    .......
    
}

도 난 방지 체인

server{
    listen 80;
    server_name www.lsylinux.com;
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
    {
        valid_referers none blocked server_names *.lsylinux.com lsylinux.com *.lsy.com lsy.com;
        if ($invalid_referer)
        {
            rewrite /(.*) http://img.lsylinux.com/images/forbidden.png;
        }
    }

    .......
    
}

# *     ,      *      ,none   referer      (curl -e   ),
# blocked   referer                        ,
#     ,referer      http://  https://  (curl -e     referer  http://  # https://  )。
#   
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
    {
        valid_referers none blocked server_names *.lsylinux.com *.lsy.com lsylinux.com lsy.com;
        if ($invalid_referer)
        {
            return 403;
        }
    }

의사 정적

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;
}

rewrite 여러 조건 의 그리고
#//nginx  if    
server {

       listen 80;
       server_name www.1.com;
       root /data/wwwroot/www.1.com;
       rewrite_log on;

       set $tmp 0;
       if ( $request_uri !~ "^/abc/" )
       {
         set $tmp "${tmp}1";
       }

       if ( $http_user_agent ~ 'IE|chrome' )
       {
         set $tmp "${tmp}2";
       }
       if ( $tmp = "012" )
       {
         return 406;
       }}


#// -A      
[root@k8s-node01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 -A "dlkfakfchromefjoe" www.1.com/1.html

406 Not Acceptable

406 Not Acceptable


nginx/1.14.0
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 -A "dlkfakfchromefjoe" www.1.com/abc/1.html 404 Not Found

404 Not Found


nginx/1.14.0
[root@k8s-node01 vhost]# curl -x127.0.0.1:80 -A "dlkfakfchrddomefjoe" www.1.com/abc/1.html 404 Not Found

404 Not Found


nginx/1.14.0

좋은 웹페이지 즐겨찾기