2012-05-03 3 views
19

서버 선언 : nginx.conf에서https 프록시 뒤의 레일 앱으로 리디렉션 하시겠습니까? 내 nginx.conf에서

listen  1.2.3.4:443 ssl; 
    root /var/www/myapp/current/public; 
    ssl on; 
    ssl_certificate /etc/nginx-cert/server.crt; 
    ssl_certificate_key /etc/nginx-cert/server.key; 
    location/{ 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      proxy_redirect off; 

      if (!-f $request_filename) { 
      proxy_pass http://upstreamy; 
      break; 
      } 
    } 

상류 선언 : HTTP

upstream upstreamy { 
    server unix:/var/www//myapp/shared/sockets/unicorn.sock fail_timeout=0; 
} 

이 잘 작동 myapp와는 https://somehost

로 연결할 수 있지만 응용 프로그램이 생성됩니다 url은 리다이렉트 때문에, 예를 들어 devise로 인증 할 때 /는 https가 아닌 http://somehost/user/sign_in으로 리디렉션됩니다 (레일스 앱의 관점에서 볼 때 모든 http입니다).

나는
proxy_pass https://upstreamy; 

을 시도했지만 그것은 단지의 nginx와 레일 응용 프로그램을 실행 유니콘 사이의 트래픽을 암호화하는 시도합니다.

또한 application_helper.rb에, 시도 :

# http://stackoverflow.com/questions/1662262/rails-redirect-with-https 
def url_options 
    super 
    @_url_options.dup.tap do |options| 
    options[:protocol] = Rails.env.production? ? "https://" : "http://" 
    options.freeze 
end 

했지만 작동하지 않는 것 같다.

어떻게 해결하나요?

편집 : 목표는 레일 앱이 ssl을 요구하도록하거나 ssl을 사용하도록 강요하지 않는 것입니다. 목표는 리다이렉션 할 때 레일 앱에서 https : // URL을 생성하도록하는 것입니다. (다른 모든 URL은 상대적이라고 생각합니다.)

답변

42

당신은 다음 줄을 추가해야합니다

proxy_set_header의 X-전달 - 프로토 HTTPS;

location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_set_header X-Forwarded-Proto https; 
     proxy_redirect off; 

     if (!-f $request_filename) { 
     proxy_pass http://upstreamy; 
     break; 
     } 
} 
+1

감사합니다, 그것은 완벽하게 작동합니다! –

+0

굉장! nginx 설정은 매우 고통 스러울 수 있습니다. 매우 고통스러운 ... :) –

+0

나는 proxy_set_header를 사용할 수 없지만 방금 "게시"방법을 사용하고 get URL을 다시 작성하기 위해 모든 것을 내 프록시에 전달했습니다. 지금까지 아주 잘 작동합니다. – nembleton

-2

에서 같이 redirect_to하는, :protocol => "https"를 전달할 수 있습니다.

당신은

Rails.application.routes.default_url_options[:protocol]= 'https' 

참조 application.rb 다음을 추가하여 기본적으로 이것을 설정할 수 있습니다 https://stackoverflow.com/a/6101147/446203

+0

이것은 투명하지 않으며 특정 배포 구성에 앱 코드를 연결합니다. – eprothro

관련 문제