Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phoenix routing challenges with a subfolder deployment #296

Closed
joshisa opened this issue Feb 22, 2018 · 2 comments · Fixed by #365
Closed

Phoenix routing challenges with a subfolder deployment #296

joshisa opened this issue Feb 22, 2018 · 2 comments · Fixed by #365

Comments

@joshisa
Copy link

joshisa commented Feb 22, 2018

I'm trying to setup asciinema under a url with a subfolder of https://x.x.x.x/asciinema . I've been able to find and patch most of the rails portions to allow the home page, all of the non-phoenix links and the player to function normally.

Unfortunately, I'm facing 500 error codes with my "x.x.x.x/asciinema/docs" and my login links. I suspect that i need to make some adjustments within my /lib/asciinema_web/router.ex and maybe my nginx file ... but my debug efforts so far haven't pointed me in the right direction.

I've enabled error_log within the nginx "sites-enabled/default" file. The traffic for "/asciinema/docs" seems to be matching to the phoenix server. Here's a snip:

2018/02/22 21:16:59 [debug] 1903#1903: *766 http script var: "/docs"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http script copy: "/index.html"
2018/02/22 21:16:59 [debug] 1903#1903: *766 trying to use file: "/docs/index.html" "/app/public/docs/index.html"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http script var: "/docs"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http script copy: ".html"
2018/02/22 21:16:59 [debug] 1903#1903: *766 trying to use file: "/docs.html" "/app/public/docs.html"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http script var: "/docs"
2018/02/22 21:16:59 [debug] 1903#1903: *766 trying to use file: "/docs" "/app/public/docs"
2018/02/22 21:16:59 [debug] 1903#1903: *766 trying to use file: "@phoenix" "/app/public@phoenix"
2018/02/22 21:16:59 [debug] 1903#1903: *766 test location: "@phoenix"
2018/02/22 21:16:59 [debug] 1903#1903: *766 using location: @phoenix "/docs?"
2018/02/22 21:16:59 [debug] 1903#1903: *766 rewrite phase: 3
2018/02/22 21:16:59 [debug] 1903#1903: *766 post rewrite phase: 4
2018/02/22 21:16:59 [debug] 1903#1903: *766 generic phase: 5
2018/02/22 21:16:59 [debug] 1903#1903: *766 generic phase: 6
2018/02/22 21:16:59 [debug] 1903#1903: *766 generic phase: 7
2018/02/22 21:16:59 [debug] 1903#1903: *766 access phase: 8
2018/02/22 21:16:59 [debug] 1903#1903: *766 access phase: 9
2018/02/22 21:16:59 [debug] 1903#1903: *766 access phase: 10
2018/02/22 21:16:59 [debug] 1903#1903: *766 post access phase: 11
2018/02/22 21:16:59 [debug] 1903#1903: *766 try files phase: 12
2018/02/22 21:16:59 [debug] 1903#1903: *766 http init upstream, client timer: 0
2018/02/22 21:16:59 [debug] 1903#1903: *766 epoll add event: fd:3 op:3 ev:80002005
2018/02/22 21:16:59 [debug] 1903#1903: *766 http map started
2018/02/22 21:16:59 [debug] 1903#1903: *766 http script var: "https"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http map: "https" "https"

After the headers are rewritten per the nginx /etc/nginx/sites-enabled/default file - sample below:

upstream rails-server {
    server 127.0.0.1:3000 fail_timeout=0;
}

upstream phoenix-server {
    server 127.0.0.1:4000 fail_timeout=0;
}

proxy_cache_path /cache levels=1:2 keys_zone=png_cache:10m max_size=10g
                 inactive=14d use_temp_path=off;

map $http_x_forwarded_proto $real_scheme {
    default $http_x_forwarded_proto;
    ''      $scheme;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    root /app/public;
    error_log /app/public/nginx_error.log debug;

    client_max_body_size 16m;

    location ~ ^/(phoenix/|css/|js/|images/|fonts/|docs/?|api/asciicasts|connect/|login/?|users/?|user(/edit)?$|session/?|a/[^/]+/iframe) {
        try_files /maintenance.html $uri/index.html $uri.html $uri @phoenix;
    }

    location ~ ^/a/[^.]+\.png$ {
        proxy_cache png_cache;
        proxy_cache_revalidate on;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_lock on;
        proxy_cache_key $uri;
        proxy_ignore_headers Set-Cookie;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $real_scheme;
        proxy_set_header Host $http_host;
        proxy_pass http://phoenix-server;
        proxy_redirect off;
    }

    location ~ ^/a/[^.]+\.(js|json|cast|gif)$ {
        try_files $uri $uri/index.html $uri.html @phoenix;
    }

    location / {
        try_files $uri $uri/index.html $uri.html @rails;
    }

    location ~ "^/assets/.+-[0-9a-f]{32,}" {
        gzip_static on;
        expires 1y;
        add_header Cache-Control public;

        add_header ETag "";
        break;
    }

    location @rails {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $real_scheme;
        proxy_set_header Host $http_host;
        proxy_pass http://rails-server;
        proxy_redirect off;
        error_page 500 502 504 /500.html; # Rails error pages
    }

    location @phoenix {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $real_scheme;
        proxy_set_header Host $http_host;
        proxy_pass http://phoenix-server;
        proxy_redirect off;
    }
}

Failing Request --> 500 Reponse

2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header:
"GET /docs HTTP/1.0
X-Real-IP: z.z.z.z
X-Forwarded-For: x.x.x.x, y.y.y.y
X-Forwarded-Proto: https
Host: x.x.x.x
Connection: close
X-Forwarded-Host: x.x.x.x
X-Forwarded-Port: 443
X-Original-URI: /asciinema/docs
X-Scheme: https
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
dnt: 1
accept-encoding: gzip, deflate, br
2018/02/22 21:16:59 [debug] 1903#1903: *766 http run request: "/docs?"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http upstream check client, write event:1, "/docs"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http upstream recv(): -1 (11: Resource temporarily unavailable)
2018/02/22 21:16:59 [debug] 1903#1903: *766 delete posted event 000055A82B6B6500
2018/02/22 21:16:59 [debug] 1903#1903: *766 http upstream request: "/docs?"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http upstream send request handler
[....]
2018/02/22 21:16:59 [debug] 1903#1903: *766 http upstream request: "/docs?"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http upstream process header
2018/02/22 21:16:59 [debug] 1903#1903: *766 malloc: 000055A82B625660:4096
2018/02/22 21:16:59 [debug] 1903#1903: *766 recv: fd:9 272 of 4096
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy status 500 "500 Internal Server Error"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header: "server: Cowboy"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header: "date: Thu, 22 Feb 2018 21:16:59 GMT"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header: "content-length: 21"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header: "content-type: text/html; charset=utf-8"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header: "cache-control: max-age=0, private, must-revalidate"
2018/02/22 21:16:59 [debug] 1903#1903: *766 http proxy header: "x-request-id: 602707ndgqqmmvmudiqob1vnc2r8v7jg"

Would welcome any hints/guidance. Absolutely love this project!

@ku1ik
Copy link
Contributor

ku1ik commented Nov 12, 2018

The rest of the codebase was ported to Elixir this year, so there's no dual-stack anymore, which should make what you want to achieve easier. It's been a while since you tried setting this up, so I'm not sure if you're still interested in it. I'll close this one, but feel free to re-open and we'll figure it out.

@ku1ik ku1ik closed this as completed Nov 12, 2018
@lyknode
Copy link
Contributor

lyknode commented Oct 12, 2020

Hi @sickill,

I'm having the same use case as @joshisa. I'm trying to run asciinema-server on http://localhost/asciinema but static files (and websocket?) keep getting served on the root path.

I've had a couple attempt at fixing this but having no knowlegde of erlang or elixir, that task has proven to be difficult.

My configuration is:

config :asciinema, AsciinemaWeb.Endpoint,
  url: [
    scheme: "http",
    host: "localhost",
    path: "/asciinema",
    port: "80"
  ]

And the fix I tried was:

--- a/lib/asciinema_web/endpoint.ex
+++ b/lib/asciinema_web/endpoint.ex
@@ -14,12 +14,15 @@ defmodule AsciinemaWeb.Endpoint do
   socket "/socket", AsciinemaWeb.UserSocket,
     websocket: true
 
+  config = Application.get_env(:asciinema, __MODULE__)
+  url = Keyword.get(config, :url)
+
   # Serve at "/" the static files from "priv/static" directory.
   #
   # You should set gzip to true if you are running phx.digest
   # when deploying your static files in production.
   plug Plug.Static,
-    at: "/",
+    at: url[:path],
     from: :asciinema,
     gzip: true,
     only: ~w(css fonts images js favicon.ico robots.txt)

I tried a couple of variant without any success at compiling.

Any help would be much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants