widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.9k stars 141 forks source link

how to deploy Solara on the server using Nginx? #820

Closed luckfu closed 3 weeks ago

luckfu commented 4 weeks ago

When I deploy Solara on the server using Nginx, my configuration is as follows:

      location /solara/ {
        # the local solara server (could be using Starlette/uvicorn)
        proxy_pass http://localhost:8765/;

        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 https;
        #proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Script-Name /solara;  # informs solara to produce correct urls

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }

I access https://name.com/solara/, I see the following error in browser console:

GET https://name.com/solara/solara/_solara/cdn/@widgetti/solara-vuetify-app@10.0.3/dist/main8.css net::ERR_ABORTED 404 (Not Found)
solara/:156 
GET https://name.com/solara/solara/static/assets/style.css?v=9fd7b36b7c10c9a0e092a3463764ff94 net::ERR_ABORTED 404 (Not Found)
solara/:21 
GET https://name.com/solara/solara/_solara/cdn/font-awesome@4.5.0/css/font-awesome.min.css net::ERR_ABORTED 404 (Not Found)
solara/:498 
GET https://name.com/solara/solara/_solara/cdn/@widgetti/solara-vuetify-app@10.0.3/dist/solara-vuetify-app8.js net::ERR_ABORTED 404 (Not Found)
solara/:496 
 GET https://name.com/solara/solara/_solara/cdn/@widgetti/solara-vuetify-app@10.0.3/dist/fonts.css net::ERR_ABORTED 404 (Not Found)
solara/:528 
GET https://name.com/solara/solara/_solara/cdn/requirejs@2.3.6/require.js net::ERR_ABORTED 404 (Not Found)
solara/:502 Uncaught ReferenceError: solara is not defined
    at solara/:502:9
(anonymous) @ solara/:502
solara/:1070 Uncaught ReferenceError: solara is not defined
    at solara/:1070:9
maartenbreddels commented 4 weeks ago

This is strange, because we've seen this configuration a lot. It seems that the /solara part is being added twice.

That should be the case if you had

        proxy_pass http://localhost:8765;  # without the trailing slash should explain what you see

Are you sure you pasted the nginx configuration file you are using, including that trailing that? Without it, it will not remove the /solara/ prefix from the path. (I am basing this on https://mailman.nginx.org/pipermail/nginx/2016-February/049856.html and other posts I found googling this).

luckfu commented 4 weeks ago

yes my nginx configuration file:

截屏2024-10-16 17 18 41

sol.py

import solara

# Declare reactive variables at the top level. Components using these variables
# will be re-executed when their values change.
sentence = solara.reactive("Solara makes our team more productive.")
word_limit = solara.reactive(10)

@solara.component
def Page():
    # Calculate word_count within the component to ensure re-execution when reactive variables change.
    word_count = len(sentence.value.split())

    solara.SliderInt("Word limit", value=word_limit, min=2, max=20)
    solara.InputText(label="Your sentence", value=sentence, continuous_update=True)

    # Display messages based on the current word count and word limit.
    if word_count >= int(word_limit.value):
        solara.Error(f"With {word_count} words, you passed the word limit of {word_limit.value}.")
    elif word_count >= int(0.8 * word_limit.value):
        solara.Warning(f"With {word_count} words, you are close to the word limit of {word_limit.value}.")
    else:
        solara.Success("Great short writing!")

# The following line is required only when running the code in a Jupyter notebook:
Page()

run command

$ solara run sol.py --host=localhost
New version of Solara available: 1.40.0. You have 1.39.0. Please upgrade using:
        $ pip install "solara==1.40.0"
Solara server is starting at http://localhost:8765
/data/miniconda3/lib/python3.10/site-packages/solara/server/starlette.py:334: UserWarning: Header x-forwarded-proto='https' does not match scheme='http' as given by the asgi framework (probably uvicorn)

This might be a configuration mismatch behind a reverse proxy and can cause issues with redirect urls, and auth.

Most likely, you need to trust your reverse proxy server, see:
    https://solara.dev/documentation/getting_started/deploying/self-hosted

If you use uvicorn (the default when you use `solara run`), make sure you
configure the following environment variables for uvicorn correctly:
UVICORN_PROXY_HEADERS=1  # only needed for uvicorn < 0.10, since it is the default after 0.10
FORWARDED_ALLOW_IPS="127.0.0.1"  # 127.0.0.1 is the default, replace this by the ip of the proxy server

Make sure you replace the IP with the correct IP of the reverse proxy server (instead of 127.0.0.1).

If you are sure that only the reverse proxy can reach the solara server, you can consider setting:
FORWARDED_ALLOW_IPS="*" # This can be a security risk, only use when you know what you are doing

  warnings.warn(f"""Header x-forwarded-proto={forwarded_proto!r} does not match scheme={request.scope['scheme']!r} as given by the asgi framework (probably uvicorn)
/data/miniconda3/lib/python3.10/site-packages/solara/server/starlette.py:404: UserWarning: base url 'http://domain_name.com' does not end with root path '/solara'

This could be a configuration mismatch behind a reverse proxy and can cause issues with redirect urls, and auth.

See also https://solara.dev/documentation/getting_started/deploying/self-hosted
It looks like the reverse proxy sets the x-script-name header to '/solara'

  warnings.warn(msg)
/data/miniconda3/lib/python3.10/site-packages/solara/server/starlette.py:404: UserWarning: base url 'http://domain_name.com' does not end with root path '/solara'

This could be a configuration mismatch behind a reverse proxy and can cause issues with redirect urls, and auth.

See also https://solara.dev/documentation/getting_started/deploying/self-hosted
It looks like the reverse proxy sets the x-script-name header to '/solara'
            It looks like the root path was configured to '/solara' in the settings

  warnings.warn(msg)
......

chrome debug info image

Fetch event handler is recognized as no-op. No-op fetch handler may bring overhead during navigation. Consider removing the handler if possible.
solara/:156 
GET https://domain_name.com/solara/static/assets/style.css?v=9fd7b36b7c10c9a0e092a3463764ff94 net::ERR_ABORTED 404 (Not Found)
solara/:16 
GET https://domain_name.com/solara/_solara/cdn/@widgetti/solara-vuetify-app@10.0.3/dist/main8.css net::ERR_ABORTED 404 (Not Found)
solara/:496 
GET https://domain_name.com/solara/_solara/cdn/@widgetti/solara-vuetify-app@10.0.3/dist/fonts.css net::ERR_ABORTED 404 (Not Found)
solara/:21 
GET https://domain_name.com/solara/_solara/cdn/font-awesome@4.5.0/css/font-awesome.min.css net::ERR_ABORTED 404 (Not Found)
solara/:498 
GET https://domain_name.com/solara/_solara/cdn/@widgetti/solara-vuetify-app@10.0.3/dist/solara-vuetify-app8.js net::ERR_ABORTED 404 (Not Found)
solara/:502 Uncaught ReferenceError: solara is not defined
    at solara/:502:9
(anonymous) @ solara/:502
solara/:528 
GET https://domain_name.com/solara/_solara/cdn/requirejs@2.3.6/require.js net::ERR_ABORTED 404 (Not Found)
solara/:1070 Uncaught ReferenceError: solara is not defined
    at solara/:1070:9
(anonymous) @ solara/:1070
solara/:1076 Uncaught TypeError: Cannot set properties of undefined (setting 'browser_platform')
    at solara/:1076:40
(anonymous) @ solara/:1076
Chrome is moving towards a new experience that lets people make an informed choice with respect to third-party cookies.
favicon.svg:1 
GET https://domain_name.com/solara/static/assets/favicon.svg 404 (Not Found)
favicon.png:1 
GET https://domain_name.com/solara/static/assets/favicon.png 404 (Not Found)
luckfu commented 3 weeks ago

is ok!

location /solara/ {
        proxy_pass http://localhost:8765/;

        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 https;
        proxy_set_header X-Script-Name /solara;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }
maartenbreddels commented 3 weeks ago

I don't see the difference with what you had before, do you know what the problem was?