FastAPI Proxy Difficulties

I have been searching, asking AI and trying so many, many different things but I just absolutely cannot get a FastAPI proxy up and running.

I have the backend up and running 127.0.0.1:8000 (Uvicorn), I have the frontend up and running, static-ish page but every thing that I try causes nginx to not restart and then i roll back the change of the proxy template to default and nginx will start again but still no proxy on the web page.

I tried using the create-proxy-template.sh, that actually put it in the php-fpm folder instead of the proxy folder but anyways, that didn’t work either plus for some reason it put apache directives in the .tpl file.

Any kind of fairly straight forward type of instructions would be amazing! I honestly didn’t believe it should be that difficult to set up a FastAPI proxy, either it isn’t or I am just retarded…

Thanks for your help, I appreciate it!

Hi,
If i understood your problem correctly, Your problem might be related to Windows and Unix line endings. Unix/Linux (LF), Windows (CRLF). It will cause Nginx syntax errors.

Cleanest way, creating a dedicated Nginx proxy template. I use the exact same structure to deploy my Go (Golang) backend applications behind Nginx, and it works flawlessly.

Log into your server via SSH as root and navigate to the Hestia web templates directory:

cd /usr/local/hestia/data/templates/web/nginx
nano FastAPI.tpl

Paste this for HTTP:

# Fastapi NGINX Template for HTTP
server {
    listen %ip%:80;
    server_name %domain_idn% %alias_idn%;
    error_log /var/log/%web_system%/domains/%domain%.error.log error;
    location ~ /\.(?!well-known\/|file) {
        deny all;
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # WebSocket support
        proxy_set_header Connection "upgrade"; # WebSocket support
        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;
    }
    location /error/ {
        alias %home%/%user%/web/%domain%/document_errors/;
    }
    include %home%/%user%/conf/web/%domain%/nginx.conf_*;
}

CTRL+O, Enter, CTRL=X

and

nano FastAPI.stpl

Paste this for HTTPS:

# Fastapi NGINX Template for HTTP
server {
    listen %ip%:443 ssl;
    server_name %domain_idn% %alias_idn%;
    error_log /var/log/%web_system%/domains/%domain%.error.log error;
    ssl_certificate %ssl_pem%;
    ssl_certificate_key %ssl_key%;
    ssl_stapling on;
    ssl_stapling_verify on;
    location ~ /\.(?!well-known\/|file) {
        deny all;
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_ssl_server_name on;
        proxy_ssl_name $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # WebSocket support
        proxy_set_header Connection "upgrade"; # WebSocket support
        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;
    }
    location /error/ {
        alias %home%/%user%/web/%domain%/document_errors/;
    }
    include %home%/%user%/conf/web/%domain%/nginx.ssl.conf_*;
}

CTRL+O, Enter, CTRL=X

Then Log into your Hestia Control Panel. Go to Web and edit your target domain. Under the Advanced Options, look for the Proxy Template dropdown menu. You should now see FastAPI listed there. Select it and Save.