【问题标题】:How to get the current url in phoenix framework如何在phoenix框架中获取当前url
【发布时间】:2016-10-10 15:51:38
【问题描述】:

我想知道elixir / 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;
        }
}

代码:

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

那个例子返回http://127.0.0.1:4000/,我想得到http://www.babysittingbordeaux.dev/

我处于开发模式。

【问题讨论】:

  • 您可以将原始标头保留在 nginx 代理服务器中。 location / { proxy_pass http://127.0.0.1:4000; proxy_set_header Host $host; }
  • 但我认为如果您在生产模式下运行,其他方法之一将起作用。
  • 哥们,你是最棒的,成功了!谢谢
  • 很高兴您找到了解决方案。

标签: elixir phoenix-framework


【解决方案1】:

如果您只对请求路径感兴趣,您可以使用conn.request_path,它的值类似于"/users/1"

要获取包含您可以使用的主机的 URL

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

这将返回类似"http://localhost:4000/users/1"的结果。

【讨论】:

  • 我不要localhost:4000,我要我在Nginx中配置的域名。有意义吗?
  • conn.request_path 只返回路径,没有查询字符串,因此它不是完整的,甚至不是相对 URL。
  • @JeremieGes 让 Nginx 将请求 URI 作为代理中的标头传递
【解决方案2】:

你可以使用Phoenix.Controller.current_url/1:

current_url(conn)

端点配置将定义 URL:

config :my_app_web, MyAppWeb.Endpoint, url: [host: "example.com"]

【讨论】:

    【解决方案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
    

    【讨论】:

    • 对于主机来说,我得到 127.0.0.1 而不是“nginx” url,是不是不可能得到那个数据?
    • 您的服务器是在dev 模式还是prod 模式下运行?我想上述选项之一应该在prod 模式下工作?
    • 如果这些都不起作用,您可以从请求标头中获取主机。 Atom.to_string(conn.scheme) &lt;&gt; "://" &lt;&gt; (Enum.into(conn.req_headers, %{}) |&gt; Map.get("host")) &lt;&gt; conn.request_path但我认为这不是一个好方法。使用 phoenix 助手可能是在生产模式下运行应用程序的方式。见config/prod.exs
    • 在 nginx 代理服务器中保留原始 header。 proxy_set_header Host $host;
    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2010-09-21
    • 2019-09-23
    • 2018-06-11
    • 2010-12-23
    • 1970-01-01
    相关资源
    最近更新 更多