0

I have an extremely simple flask app that works perfectly with gunicorn and nginx over https with a domain. This shows me that all other configurations are working (IP, ports, https, etc.), but when I try and use it with the justpy library, which uses uvicorn, there is no interaction. The strange thing is that when I run it over the public ip of the GCP with the open port, it works just fine, so I am assuming it is a problem with my configuration of nginx.

The configuration and examples are below, along with the documentation I could find on this. This is running on a GCP VM instance, (e2 micro) with Ubuntu 21.04 installed. The public IP has been made static and I am using letsencrypt for HTTPS with the domain.

This might just be because the justpy library is so new, but I wanted to make sure it was not because of configuring nginx incorrectly.

So, using the simple flask app:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World from flaskapp 1!'

We get what we wanted when we go to https://test1.domain.com (test1.domain.com being just an example) which is "Hello World from flaskapp 1!" using the command

gunicorn fl1:app -b 0.0.0.0:5000

and using the following nginx config:

server {                                                                                                                   
        listen 80; 
        listen 443 ssl http2;
        server_name test1.domain.com;
        ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
        ssl_protocols TLSv1.3;

        location / {
            include proxy_params;
            proxy_pass http://0.0.0.0:5000;                     
    }   
}

As well, if we use the code for justpy (which comes directly from their documentation here, and here) this also gives us what we want without any errors if I run it over the public IP with the open port and using a slight modification to the gunicorn command above:

gunicorn -k uvicorn.workers.UvicornWorker fl1:app -b 0.0.0.0:5000
import justpy as jp
app = jp.app

def my_click(self, msg):
    self.text = 'I was clicked'
    self.set_class('bg-blue-500')

def my_mouseenter(self, msg):
    self.text = 'Mouse entered'
    self.set_class('bg-red-500')

def my_mouseleave(self, msg):
    self.text = 'Mouse left'
    self.set_class('bg-teal-500')


def event_demo():
    wp = jp.WebPage()
    d = jp.Div(text='Not clicked yet', a=wp, classes='w-64 text-2xl m-2 p-2 bg-blue-500 text-white',
             click=my_click, mouseenter=my_mouseenter, mouseleave=my_mouseleave)
    return wp

jp.justpy(
  event_demo,
  start_server=False
  )

This will give us a box that turns red with "Mouse entered" when the mouse is inside the box, green with "Mouse left" when the mouse is not in the box, and blue with "I was clicked" when we click on the box.

All that works, but, when I start using nginx (with the config above) to use https and the domain, that is when it starts to fail and the app constantly says "Page needs to be reloaded, click OK to reload" and it just keeps on reloading each time you click on OK. I suppose it has to do with the nginx config with uvicorn, but I have yet to figure it out.

The uvicorn documentation does not help much, and justpy has something in its main.html file for websockets from line 65, but I don't know how that would affect nginx. Any help would be appreciated.

4

1 回答 1

0

我的猜测是你得到那个弹出窗口是因为 websocket 连接没有被正确转发到 uvicorn。我自己也有类似的问题,但设置与您略有不同,也使用 ssl 运行 Uvicorn 后端服务器

但据我所知,我的配置中唯一相关的额外位是:

http {
    ...
    # possibly websockets?
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    ...
    server {
        ...
        location / {
            ...
            
            # definitely for websockets
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            ...
       }
    }
}

看看这个网站 - 那里有大量信息,包括一些关于 websockets 的信息: https ://www.linkedin.com/pulse/how-use-nginx-reverse-proxy-https-wss-self-signed-ramos- da-silva?articleId=6678584723419226112

于 2021-09-04T23:24:44.663 回答