2013-05-11 2 views
4

a.com에서 b.com으로 블로그를 이전하는 중입니다. 이제 모든 블로그 게시물 (약 100 개)을 b.com (으)로 이동했음을 Google/북마크에 알리고 싶습니다. 나는 blogposts 만 리디렉션하고 다른 것은 원하지 않습니다.nginx지도에있는 URL 만 리디렉션하는 방법

의 nginx에서 map 모듈에 대한 책을 읽은 후 나는 다음과 같은 시도 :

map_hash_bucket_size 128; 
map $uri $new { 
    /post http://b.com/post 
    # (repeated for all 100 posts) 
} 

을 그리고 나는 server 블록 내부에 다음 줄을 넣을 때

rewrite^$new redirect; 

그것은 모두 100 개 게시물을 리디렉션하지만 내 도메인의 다른 모든 페이지에 오류 : 302 Found이 표시됩니다. 내가지도를 생각하고

server { 

    listen 80; 
    server_name b.com; 
    root /my/old/path/; 
    index index.html; 

    location = /favicon.ico { 
      log_not_found off; 
      access_log off; 
    } 

    location = /robots.txt { 
      allow all; 
      log_not_found off; 
      access_log off; 
    } 

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). 
    location ~ /\. { 
     deny all; 
     access_log off; 
     log_not_found off; 
    } 

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
      expires max; 
      log_not_found off; 
    } 

    # example.com/index gets redirected to example.com/ 

    location ~* ^(.*)/index$ { 
     return 301 $scheme://$host$1/; 
    } 

    # example.com/foo/ loads example.com/foo/index.html 

    location ~* ^(.*)/$ { 
     try_files $1/index.html @backend; 
    } 

    # example.com/a.html gets redirected to example.com/a 

    location ~* \.html$ { 
     rewrite ^(.+)\.html$ $scheme://$host$1 permanent; 
    } 

    # anything else not processed by the above rules: 
    # * example.com/a will load example.com/a.html 
    # * or if that fails, example.com/a/index.html 

    location/{ 
     try_files $uri.html $uri/index.html $uri @backend; 
    } 

    # default handler 
    # * return error or redirect to base index.html page, etc. 
    location @backend { 
     try_files /404.html 404; 
    } 

    location ~ \.php$ { 
      expires off; 
      try_files $uri =404; 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 

      include fastcgi_params; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_intercept_errors on; 
      fastcgi_pass php; 
    } 

    # apply the rewrite map 
    rewrite^$new redirect; 
} 

(내가 원하는) 위치 \에 대한 try_files 전화 방해 :

다음은 설정 안에 내 전체 서버 블록입니다.

답변

3

내가 문제 on serverfault에 대한 해결책을 발견 :

모든 요청을 리디렉션하려고 노력하고 있기 때문에 그들이 뭔가를 일치 여부 이것은 아마 실패 지도 또는 아닙니다.

이 문제를 방지하려면 먼저 일치하는 항목이 있는지 확인하십시오.

if ($new) { 
    return 301 $new; 
} 

그래서

if ($new) { 
    return 301 $new; 
} 

rewrite^$new redirect; 

교체

을 가고 당신이 잘되어야합니다
0

지도에서이 작업을 수행하는 방법을 아직 알지 못했습니다. 여전히 나에게 맞는 문자열 모듈입니다. 이제 모든 서버 블록 (모든 위치 블록 위)에있는 목록에 모든 재 작성을 넣었습니다. 이는 아마도 매우 느릴 수 있습니다. 이제 파일은 다음과 같습니다

server { 

    listen 80; 
    server_name a.com; 
    root /my/old/path/; 
    index index.html; 

    rewrite /post http://b.com/post 
    # (x100) 

    location = /favicon.ico { 
      log_not_found off; 
      access_log off; 
    } 

    location = /robots.txt { 
      allow all; 
      log_not_found off; 
      access_log off; 
    } 

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). 
    location ~ /\. { 
     deny all; 
     access_log off; 
     log_not_found off; 
    } 

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
      expires max; 
      log_not_found off; 
    } 

    # example.com/index gets redirected to example.com/ 

    location ~* ^(.*)/index$ { 
     return 301 $scheme://$host$1/; 
    } 

    # example.com/foo/ loads example.com/foo/index.html 

    location ~* ^(.*)/$ { 
     try_files $1/index.html @backend; 
    } 

    # example.com/a.html gets redirected to example.com/a 

    location ~* \.html$ { 
     rewrite ^(.+)\.html$ $scheme://$host$1 permanent; 
    } 

    # anything else not processed by the above rules: 
    # * example.com/a will load example.com/a.html 
    # * or if that fails, example.com/a/index.html 

    location/{ 
     try_files $uri.html $uri/index.html $uri @backend; 
    } 

    # default handler 
    # * return error or redirect to base index.html page, etc. 
    location @backend { 
     try_files /404.html 404; 
    } 

    location ~ \.php$ { 
      expires off; 
      try_files $uri =404; 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 

      include fastcgi_params; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_intercept_errors on; 
      fastcgi_pass php; 
    } 

    # apply the rewrite map 
    rewrite^$new redirect; 
} 
관련 문제