Elixir 如何在phoenix框架中获取当前url

Elixir 如何在phoenix框架中获取当前url,elixir,phoenix-framework,Elixir,Phoenix Framework,我想知道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;

我想知道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_字符串(conn.scheme)”:/(Enum.into(conn.req_头,%{})|>Map.get(“主机”))conn.request_路径
这个例子返回,我想得到


我处于开发模式。

我不确定哪种方法是最好的

但也许像这样的东西可以作为
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
def索引(conn,params)do
url(带有\u port=Atom.to \u字符串(conn.scheme)”:/“
conn.host“:“整数到字符串(conn.port)
连接请求路径
url=Atom.to_字符串(conn.scheme)”:/“
控制主机
连接请求路径
url_phoenix_helper=Tester.Router.Helpers.index_url(conn,:index,params[“path”])#Map.get(“主机”))
连接请求路径
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}
"""
文本(连接,文本)
结束

如果您只对请求路径感兴趣,可以使用
conn.request\u path
,该路径的值类似于
“/users/1”

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

MyApp.Router.Helpers.url(conn) <> conn.request_path
MyApp.Router.Helpers.url(conn)conn.request\u路径
这将返回类似于
”的结果http://localhost:4000/users/1“

您可以使用:

端点配置将定义URL:

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

对于主机,我得到的是127.0.0.1,而不是“nginx”url,不可能得到这些数据吗?您是在
dev
模式还是
prod
模式下运行服务器?我想上面的一个选项应该在
prod
模式下工作?如果这些选项都不工作,您可以从请求头获取主机<代码>Atom.to_字符串(conn.scheme)”:/“(Enum.into(conn.req_头,%{})|>Map.get(“主机”))conn.request_路径,但我认为这不是一个好方法。使用phoenix helpers可能是在生产模式下运行应用程序的一种方式。请参见
config/prod.exs
在nginx代理服务器中保留原始标头<代码>代理设置头主机$Host我不想要localhost:4000,我想要我在Nginx中配置的域名。有意义吗?
conn.request\u path
只返回路径,不返回查询字符串,因此它不是完整的或甚至是相对的URL。@JeremieGes让Nginx将请求URI作为代理中的头传递。您可以将原始头保留在Nginx代理服务器中<代码>位置/{proxy_passhttp://127.0.0.1:4000;proxy_set_header Host$Host;}但我认为如果在生产模式下运行,其他方法之一也会起作用。老兄,你是最好的,它起作用了!谢谢你找到了解决办法。
current_url(conn)
config :my_app_web, MyAppWeb.Endpoint, url: [host: "example.com"]