하위 디렉터리의 페이지 캐싱을 위해 Rails with Passenger에 대한 Nginx 재작성 규칙은 무엇이어야 합니까?

하위 디렉터리의 페이지 캐싱을 위해 Rails with Passenger에 대한 Nginx 재작성 규칙은 무엇이어야 합니까?

저는 Nginx 0.7.64, Passenger 2.2.9, Rails 2.3.5를 사용하고 있습니다. 내 페이지 캐싱 디렉터리가 /public/cache로 설정되어 있고 HTTP를 통해 요청하면 캐시된 페이지를 제공할 수 있기를 원하지만 HTTPS를 통해 요청하면 항상 Rails 앱을 실행합니다.

내 구성의 대부분은 다음과 같습니다.

server {
  listen 80;
  server_name website.com www.website.com;
  proxy_set_header X-Forwarded-Proto http;
  root /home/deploy/website/current/public;
  passenger_enabled on;

  if (-f $document_root/cache/$request_filename.html) { 
    rewrite (.*) $document_root/cache/$1.html break;
  }
}

server {
  listen       443;
  server_name website.com www.website.com;
  root /home/deploy/website/current/public;
  passenger_enabled on;
  proxy_set_header X-Forwarded-Proto https;

  ssl                  on;
  ssl_certificate      /home/deploy/website/shared/ssl/www.website.com.combined.crt;
  ssl_certificate_key  /home/deploy/website/shared/ssl/www.website.com.key;
}

나는 website.com/about을 요청하면 /public/cache/about.html이 제공되어야 한다고 예상하지만 대신 Rails 서버에 접속합니다(로그를 추적하면 이를 보여줍니다).

부적절한 슬래시가 있을 수 있다고 생각하여( $document_root대부분의 예에서는 표시되지 않음) 다음 변형을 모두 시도했지만 어느 것도 작동하지 않았습니다.

if (-f cache$request_filename.html) { 
  rewrite (.*) cache$1.html break;
}

if (-f /cache$request_filename.html) { 
  rewrite (.*) /cache$1.html break;
}

if (-f cache/$request_filename.html) { 
  rewrite (.*) cache/$1.html break;
}

if (-f /cache/$request_filename.html) { 
  rewrite (.*) /cache/$1.html break;
}

root또한 , passenger_enabled및 다시 쓰기 규칙을 별도의 블록에 넣었 location /지만 그것도 작동하지 않습니다. 나는 또한 그것이 passenger_enabled끝에 올 수 있도록 진술을 재정렬했습니다. 을 사용해 보기도 했습니다 $uri. 분명히 나는 ​​뭔가를 오해하고 있습니다!

이는 약간 단순화되었습니다. 왜냐하면 제 위치에 캐시된 XML API도 있고(아마도 재작성 규칙은 부분을 제외하고 동일할 것임 ) 루트가 요청될 때 .html서비스를 제공해야 하기 때문입니다 . 나는 단지 그것의 단 하나의 조각이라도 작동시키고 싶습니다. :)public/cache/index.htmlwebsite.com

도움을 주시면 감사하겠습니다!

업데이트

조건부

if (-f $document_root/cache$request_uri.html)

작동하는 것 같습니다! 그러나 다시 작성하면 작동하지 않을 것이라고 생각합니다! 견딜 수 없는

if (-f $document_root/cache$request_uri.html) {
  rewrite (.*) /cache$1.html break;
  break;
}

URL을 다음과 같이 다시 작성하여 /cache/cache/about.html.htmlRails로 보냅니다. Rails는 즉시 barfs를 실행합니다. 두배로 보이는군요, 그렇죠! 하지만 다시 작성하면 Rails로 /cache$1보내고 , Rails로 보내고 , Rails로 이동하고 캐시에 도달하지 않는 것만 보냅니다 . 분명히 이것은 올바른 동작이 아닙니다. Nginx가 이를 다시 작성하고 승객도 이를 다시 작성하고 있습니까?/cache/cache/about$1.html/about.html.html$1/about

답변1

대답은 여기에서 찾았습니다. https://stackoverflow.com/questions/1177979/nginx-rewrite-rules-with-passenger

구성은 다음과 같습니다.

# root
if (-f $document_root/cache/$uri/index.html) {
  rewrite (.*) /cache/$1/index.html break;
}

# pages like /about, cached with .html but accessed without
if (-f $document_root/cache/$uri.html) {
  rewrite (.*) /cache/$1.html break;
}

# pages like /api/v1/something.xml, cached as xml
if (-f $document_root/cache/$uri) {
  rewrite (.*) /cache/$1 break;
}

답변2

try_files버전:

server {
  listen 80;
  server_name website.com www.website.com;
  location / {
    root /home/deploy/website/current/public;
    try_files $uri /cache/$uri/index.html /cache/$uri.html /cache/$uri @passenger;
  }
  location @passenger {
    root /home/deploy/website/current/public;
    proxy_set_header X-Forwarded-Proto http;
    passenger_enabled on;
  }
}

승객에게 작동할 수도 있고 작동하지 않을 수도 있습니다. 그래도 유니콘, 잡종 등과 함께 작동한다고 확신합니다.

관련 정보