Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

vue.js - A problem about deploying with fastAPI+nginx+unvicorn+gunicorn

I got a problem about deploying with nginx + uvicorn + gunicorn

My code list below, please give me some help:

gunicorn.socket under /etc/systemd/system/gunicorn.socket:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock
User=www-data


[Install]
WantedBy=sockets.target

gunicorn.service under /etc/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn fastapi daemon
Requires=gunicorn.socket
After=network.target

[Service]
Type=notify

User=www-data
Group=root

RuntimeDirectory=gunicorn

# WorkingDirectory 是项目路径目录
WorkingDirectory=/var/www

ExecStart=/usr/local/bin/gunicorn -c /var/www/gunicorn.py run:app
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target

The gunicorn.py:

debug = True
daemon = True

bind = '0.0.0.0:8089'    
chdir = '/var/www/var'  
timeout = 30   
worker_class = 'uvicorn.workers.UvicornWorker'

workers = 4  

keyfile = '/etc/letsencrypt/live/demo.com/privkey.pem'
certfile = '/etc/letsencrypt/live/demo.com/fullchain.pem'

loglevel = 'debug'  
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'  

accesslog = "/var/www/var/gunicorn_fasttest_access.log"    
errorlog = "/var/www/var/gunicorn_fasttest_error.log"    

and run.py:

from app import create_app
import logging
from fastapi.logger import logger as fastapi_logger
from logging.handlers import RotatingFileHandler

app = create_app()


formatter = logging.Formatter(
    "[%(asctime)s.%(msecs)03d] %(levelname)s [%(thread)d] - %(message)s",
    "%Y-%m-%d %H:%M:%S")
handler = RotatingFileHandler('/var/www/fastapi.log', backupCount=0)
logging.getLogger().setLevel(logging.NOTSET)
fastapi_logger.addHandler(handler)
handler.setFormatter(formatter)
fastapi_logger.info('****************** Starting Server *****************')

Here is the config of ngnix.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    
    #client_max_body_size    32m;
    
    default_type application/octet-stream;

    
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    
    gzip on;

    server {
        server_name demo.com;        
        root /var/www;
        index index.html index.htm index.nginx-debian.html;
        
        charset utf-8;
        client_max_body_size 100M;
        fastcgi_read_timeout 1800;
        
        
        location /api {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            
            proxy_redirect off;
            proxy_buffering off;
            proxy_pass https://unix:/run/gunicorn.sock;
        }
        
        location / {
            try_files $uri $uri/ =404;            
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
            expires       max;
            log_not_found off;
            access_log    off;
        }

        location ~ /.ht {
            deny all;
        }
        
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/demo.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/demo.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    }

    server {
        if ($host = demo.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot

        listen 80;
        listen [::]:80;

        server_name demo.com;
        return 404; # managed by Certbot
    }    
}

and after I start the socket and service: systemctl enable --now gunicorn.socket and systemctl restart gunicorn.service, It got en error:

● gunicorn.service - gunicorn fastapi daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
     Active: failed (Result: protocol) since Tue 2021-01-26 15:04:55 CST; 9s ago
TriggeredBy: ● gunicorn.socket
    Process: 7141 ExecStart=/usr/local/bin/gunicorn -c /var/www/gunicorn.py run:app (code=exited, status=0/SUCCESS)
   Main PID: 7141 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 9164)
     Memory: 332.0K
     CGroup: /system.slice/gunicorn.service

Jan 26 15:04:55 irl6kZ systemd[1]: Starting gunicorn fastapi daemon...
Jan 26 15:04:55 irl6kZ systemd[1]: gunicorn.service: Killing process 7151 (gunicorn) with signal SIGKILL.
Jan 26 15:04:55 irl6kZ systemd[1]: gunicorn.service: Killing process 7151 (gunicorn) with signal SIGKILL.
Jan 26 15:04:55 irl6kZ systemd[1]: gunicorn.service: Failed with result 'protocol'.
Jan 26 15:04:55 irl6kZ systemd[1]: Failed to start gunicorn fastapi daemon.

But, If I start the gunicorn individually by the command:

gunicorn -c /var/www/xojpy/gunicorn.py run:app

then I can visit the script via https://demo.com:8089/api and see the result of api data!

Why? How to correct it?

question from:https://stackoverflow.com/questions/65897957/a-problem-about-deploying-with-fastapinginxunvicorngunicorn

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...