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

Getting ASGI message error when using an URL prefix #2468

Closed
wdroz opened this issue Jan 30, 2024 · 5 comments · Fixed by #2521
Closed

Getting ASGI message error when using an URL prefix #2468

wdroz opened this issue Jan 30, 2024 · 5 comments · Fixed by #2521
Assignees
Labels
bug Something isn't working
Milestone

Comments

@wdroz
Copy link
Contributor

wdroz commented Jan 30, 2024

Description

Since version higher than 1.4.7, I can no longer make nicegui works when using a custom URL prefix.

Basically it's the same error as #2354, #2368 and #2370.

When I try locally, without prefix, this work perfectly. All dependencies are identical between local and production. The only diff is that in prod, the app is behind a reverse proxy with a prefix for the URL.

wss://company.domain/subdomain/subsubdomain/subsubsubdomain/_nicegui_ws/socket.io/?client_id=REDACTED&transport=websocket.

I'm not knowledgeable with socketio, but the fix that set the socketio_path seem to use absolute URL here.

The current way of using custom prefix is by using the request object here but the same trick seem impossible at startup.

@wdroz
Copy link
Contributor Author

wdroz commented Feb 2, 2024

I wasn't able to reproduce it using the subpath example examples/nginx_subpath. Maybe this is because the middleware intercept X-Forwarded-Prefix but not the root_path?

https://github.com/zauberzeug/nicegui/blob/main/nicegui/middlewares.py

@rodja
Copy link
Member

rodja commented Feb 6, 2024

I finally had time to look into this but could not quickly create a test setup. Could you share a minimal reproducible example? Why do you supply root_path at all? As you said the examples/nginx_subpath works fine without. And so does our docker-compose.yml test-setup which uses a Traefik proxy:

- traefik.http.middlewares.app-prefix.stripprefix.prefixes=/app
- traefik.http.middlewares.app-prefix.stripprefix.forceSlash=false # see https://doc.traefik.io/traefik/middlewares/stripprefix/#forceslash
- traefik.http.routers.app.middlewares=app-prefix

@rodja
Copy link
Member

rodja commented Feb 6, 2024

The error may very well be caused by the same problem as in #2515, which hopefully gets fixed with my pull request in the python-engineio repository.

@rodja
Copy link
Member

rodja commented Feb 7, 2024

@wdroz could you share a minimal demo which we can use to verify that #2521 really fixes your issue?

@rodja rodja added the bug Something isn't working label Feb 7, 2024
@rodja rodja added this to the 1.4.15 milestone Feb 7, 2024
@wdroz
Copy link
Contributor Author

wdroz commented Feb 7, 2024

I tried to make a docker compose that do the same as our k8s and adapt the nginx_subpath example with it.

changes in nginx.conf

server {
        listen 80 default_server;
        server_name _;
        resolver 127.0.0.11; # see https://github.com/docker/compose/issues/3412

        location ~ ^/nicegui/(/|$)(.*) {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            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 $scheme;

            proxy_pass http://app:8080/$2$is_args$args;
        }
    }

changes in docker-compose.yml

    image: zauberzeug/nicegui:1.4.7
    environment:
      PATH_PREFIX: "/nicegui"
    volumes:
      - ./app:/app # mount local app directory
    command: ["uvicorn", "--host", "0.0.0.0", "--port", "8080", "--root-path", "/nicegui", "main:app"]

changes in app/main.py

from nicegui import ui
from starlette.middleware.sessions import SessionMiddleware
from fastapi import FastAPI

app = FastAPI(
    title="Minimal example for prefix root_path",
)

def init(app: FastAPI) -> None:

    app.add_middleware(SessionMiddleware, secret_key="is_this_working?")
    @ui.page('/subpage')
    def subpage():
        ui.label('This is a subpage')
        ui.button('back', on_click=lambda: ui.open('/'))


    @ui.page('/')
    def index():
        ui.button("go to subpage", on_click=lambda: ui.open('/subpage'))


init(app)
ui.run_with(app)

Some relevant fragment of the k8s ingress config

  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$2

rules:
    - host: our.company.com
      http:
        paths:
          - path: /nicegui(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: app-dev
                port:
                  number: 80

The nginx conf still has some issues as moving from http://localhost/nicegui/ to http://localhost/nicegui/subpage doesn't seem to work. Maybe you know better than me about k8s and nginx and can do something.

But the core is running alongside FastAPI and using uvicorn with --root-path. Also not using the proxy_set_header X-Forwarded-Prefix /nicegui;.

@falkoschindler falkoschindler removed the question Further information is requested label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants