2016-06-09 7 views
7

elixir/phoenix 프레임 워크가있는 현재 URL을 알고 싶습니다. 어떻게 얻을 수 있습니까?phoenix 프레임 워크에서 현재 URL을 얻는 방법

편집 # 1 :

내의 nginx 설정 파일 :

server { 
     client_max_body_size 100M; 

     listen 80; 

     server_name *.babysittingbordeaux.dev *.babysittingparis.dev 

     access_log /usr/local/var/log/nginx/baby-access.log; 
     error_log /usr/local/var/log/nginx/baby-error.log; 


     location/{ 
      proxy_pass http://127.0.0.1:4000; 
     } 
} 

코드 : 예 http://127.0.0.1:4000/ 반환

Atom.to_string(conn.scheme) <> "://" <> (Enum.into(conn.req_headers, %{}) |> Map.get("host")) <> conn.request_path 

그건, 내가 좀하고 싶습니다 http://www.babysittingbordeaux.dev/

나는 ~ 안에있다 개발 모드.

+2

당신은의 nginx 프록시 서버의 원래 헤더를 유지할 수 있습니다. '''위치/{ proxy_pass http://127.0.0.1:4000; proxy_set_header 호스트 $ 호스트; }''' –

+0

하지만 프로덕션 모드에서 실행하면 다른 방법들 중 하나가 작동한다고 생각합니다. –

+0

너 너는 최고야, 잘됐다! 감사합니다 –

답변

3

나는 어느 것이 가장 좋은 방법인지 모르겠습니다.

아마도 그림이 IndexController 인 그림과 같이 표시 될 수 있습니다.

def index(conn, params) do 

    url_with_port = Atom.to_string(conn.scheme) <> "://" <> 
        conn.host <> ":" <> Integer.to_string(conn.port) <> 
        conn.request_path 

    url = Atom.to_string(conn.scheme) <> "://" <> 
      conn.host <> 
      conn.request_path 

    url_phoenix_helper = Tester.Router.Helpers.index_url(conn, :index, params["path"]) # <-- This assumes it is the IndexController which maps to index_url/3 

    url_from_endpoint_config = Atom.to_string(conn.scheme) <> "://" <> 
           Application.get_env(:my_app, MyApp.Endpoint)[:url][:host] <> 
           conn.request_path 

url_from_host_header = Atom.to_string(conn.scheme) <> "://" <> 
         (Enum.into(conn.req_headers, %{}) |> Map.get("host")) <> 
         conn.request_path 

    text = ~s""" 

     url_with_port :: #{url_with_port} 

     url :: #{url} 

     url_phoenix_helper :: #{url_phoenix_helper} 

     url_from_endpoint_config :: #{url_from_endpoint_config} 

     url_from_host_header :: #{url_from_host_header} 
    """ 

    text(conn, text) 
    end 
+0

글쎄, 호스트에 대해 "nginx"URL보다 127.0.0.1이 나옵니다. 데이터를 가져올 수 없습니까? –

+0

서버를'dev' 모드 또는'prod' 모드로 실행하고 있습니까? 위의 옵션 중 하나가'prod' 모드에서 작동해야한다고 생각합니까? –

+1

그 중 아무 것도 작동하지 않으면 요청 헤더에서 호스트를 가져올 수 있습니다. ("host")) <> conn. {{{{{{{{}}}} request_path''' 그러나 나는 이것을하는 것이 좋은 방법이라고 생각하지 않습니다. phoenix 헬퍼를 사용하는 것은 아마도 프로덕션 모드에서 앱을 실행하는 것과 함께가는 길일 것입니다. 'config/prod.exs'를보십시오 –

7

당신은 단지 당신이 "https://stackoverflow.com/users/1" 같은 값을 유지 conn.request_path을 사용할 수 있습니다 요청 경로에 관심이 있다면.

호스트를 포함한 URL이 "http://localhost:4000/users/1" 같은 결과를 반환

MyApp.Router.Helpers.url(conn) <> conn.request_path 

사용할 수 있습니다 얻으려면.

current_url(conn) 

엔드 포인트 구성이 URL을 정의합니다 :

+1

localhost : 4000을 원하지 않습니다. Nginx에서 설정 한 도메인 이름을 원합니다. 이해가 되니? –

+0

'conn.request_path'는 쿼리 문자열이없는 경로 만 반환하므로 전체 또는 상대 URL이 아닙니다. –

관련 문제