2017-12-21 9 views
0

제대로 쿼리 유형에 대한 위치재 작성로 만들려면 Nginx에 위치

site.com/b/?g=qwerty 

재 작성 :

/b/?g=qwerty 

요청 :

site.com/anyshorttext/?h=qwerty 

재 작성 :

server { 

     location ~ /a/?(.*)$ { 

      rewrite ^/([-\w]+) /a/?m=$1 break; 

      index index.php index.html index.htm; 
       try_files $uri $uri/ =404; 

       location ~ \.php$ { 
         include snippets/fastcgi-php.conf; 
         fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
       } 
     } 

     location ~ /b/?(.*)$ { 

      rewrite ^/([-\w]+) /b/?m=$1 break; 

      index index.php index.html index.htm; 
       try_files $uri $uri/ =404; 

       location ~ \.php$ { 
         include snippets/fastcgi-php.conf; 
         fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
       } 
     } 

     location ~* ^/[a-zA-Z0-9/_$/]+$ { 

      rewrite ^/([-\w]+) /special/?h=$1 break; 

      index index.php index.html index.htm; 
       try_files $uri $uri/ =404; 

       location ~ \.php$ { 
        include snippets/fastcgi-php.conf; 
        fastcgi_pass unix:/run/php/php7.1-fpm.sock; 
       } 
     } 
    } 
:
/special/?h=qwerty 

내가이 솔루션을 적용그러나이 경우 요청은 /special/에만 전달됩니다.

고맙습니다.

+1

'?'다음에 오는 것은 모두 쿼리 문자열이며'rewrite' 및'location' 지시어에서 사용하는 정규화 된 URI의 일부가 아닙니다. 요구 사항 :'/site/a/? f = qwerty' ~'/ a /? f = qwerty'는 NOP입니다. –

+0

예제는 각 경우에 대해 쿼리 문자열이 변경되지 않는다는 것을 의미합니다. 그것이 사실이라면, 나는 당신을위한 해결책을 가지고있다. –

답변

0

귀하의 예는 쿼리 문자열이 각 경우마다 변경되지 않는다는 것을 의미합니다. 그 때문에의 경우 다음을 시도 할 수 :

map $uri $special_query { 
    # $special_query will be true, for paths like: //site.com/xyz/?stuff 
    ~^/[^/]+/$ $query_string; 
} 

server { 
    location/{ 
     if ($special_query) { 
      return 301 "/special/?$query_string"; 
     } 
    } 

    # these locations will override the default/location 
    location = /a/ {} 
    location = /b/ {} 
    location = /special/ {} 
} 

이 솔루션은 어느 정규식 위치를 사용하거나 구성에 불필요한 복잡성을 추가하고 자연의 nginx 흐름을 방해 할 수 있습니다 둘 다 다시 씁니다. (nginx 작성자 Igor Sysoev의 확장 가능한 구성 참조)

+0

Cole Tierney에게 감사드립니다. 나는 완전한 구성을 보여준다. 다시 봐주세요. 이전 답장이 도움이되지 못했습니다. 고맙습니다. –

+0

게시하기 전에 솔루션을 테스트했습니다. 내 솔루션을 regex 위치와 결합하려고하면 제대로 작동하지 않을 것입니다. 내 제안은 모든 정규식 위치를 제거하고 가능한 경우 다시 작성하지 않는 것입니다. 정규식을 사용해야하는 경우 맵에 넣거나 접두어 위치로 래핑하여 나머지 구성에 미치는 영향을 줄이십시오. 자세한 내용은 게시 한 비디오를 확인하십시오. –