Nginx: 기본 도메인에서 하위 디렉터리로 다시 쓰기

Nginx: 기본 도메인에서 하위 디렉터리로 다시 쓰기

내가 호스팅하는 블로그가 있습니다.http://site.com/blog.

site.com에서 으로 요청을 다시 쓰도록 nginx에 어떻게 지시합니까 site.com/blog?

이는 영구적이어서는 안 됩니다.

답변1

location = / {
    rewrite ^ http://site.com/blog/ redirect;
}

이는 루트에 대한 요청만 수행합니다. 모든 것을 포착해야 하는 경우( http://site.com/somearticle/something.html로 리디렉션 http://site.com/blog/somearticle/something.html) 더 관련된 내용이 필요합니다.

location /blog/ {
    # Empty; this is just here to avoid redirecting for this location,
    # though you might already have some config in a block like this.
}
location / {
    rewrite ^/(.*)$ http://site.com/blog/$1 redirect;
}

답변2

대신 이것을 시도해 보십시오:

location = / {
      return 301 /blog/;
 }

여기서 핵심은'='상징.

답변3

이것은 나에게 효과가 없었습니다. 이것이 효과가 있었던 것입니다:

  1. 사이트의 NGINX 구성 파일을 엽니다. 서버 블록 내부에 루트 디렉터리에 대한 경로를 추가하고 파일의 우선 순위를 설정합니다.

    root /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    
  2. 빈 위치 블록 생성~ 전에다른 모든 위치 블록:

    location /latest {
    # Nothing in here; this is to avoid redirecting for this location
    }
    
  3. location / {} 블록의 루트 디렉터리 지시문을 주석 처리하고 다음과 같이 리디렉션을 추가합니다.

    location / {
    # root   /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    rewrite ^/(.*)$ http://www.domainname.com/latest/$1 redirect;
    }
    
  4. 귀하의 위치 ~.php$ 블록이 루트를 가리키는지 확인하십시오.

    root /mnt/www/www.domainname.com;
    

이로 인해 문제가 해결되었습니다.

답변4

홈페이지만 만들고 다른 것은 사용하지 않으려면 다음을 사용합니다.

location / {           
    rewrite ^/$ /blog/ redirect;
}

/foo/와 같은 다른 항목은 /blog/로 리디렉션되지 않습니다.

관련 정보