我试图在 AWS EC2 中部署多个 Flask 应用程序,但没有成功 :(。
我正在跟进本教程,步骤似乎很简单,但我还没有成功。这是我的配置:
实例类型:t2.micro
操作系统:Ubuntu,22.04 LTS,64 位
目录结构:
Ngingx、Gunicorn 和 Flask 均已在实例中全局安装。
app_one gunicorn服务配置:
[Unit]
Description=Gunicorn instance for app_one
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/app_one
ExecStart=/usr/bin/gunicorn3 --workers 2 --bind unix:flaskapp.sock -m 007 app:app
Restart=always
[Install]
WantedBy=multi-user.target
- app_two gunicorn服务配置:
[Unit]
Description=Gunicorn instance for app_two
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/app_two
ExecStart=/usr/bin/gunicorn3 --workers 2 --bind unix:flaskapp.sock -m 007 app:app
Restart=always
[Install]
WantedBy=multi-user.target
- nginx 默认启用文件已删除,并替换为此“flaskapp”文件:
server{
listen 80;
server_name 3.89.19.163;
location / {
proxy_pass http://unix:/home/ubuntu/app_one/flaskapp.sock;
error_log /var/log/nginx/error.3.89.19.163;
}
}
server{
listen 8080;
server_name 3.89.19.163;
location / {
proxy_pass http://unix:/home/ubuntu/app_two/flaskapp.sock;
error_log /var/log/nginx/error.3.89.19.163;
}
}
- nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
- 最后 /var/log/nginx/error.log
2023/10/02 09:17:31 [notice] 2788#2788: using inherited sockets from "6;7;"
据我所知,这个错误文件显示一个更新,因为它是一个“通知”,所以我没有什么可担心的,但是,看起来我的配置文件中有一些奇怪的东西,因为当我运行 curl localhost:80 或 curl localhost:8080 时,我得到了一个“502 Bad Gateway”
当我访问我的公共IP地址(publicIP,publicIP:80,publicIP:8080)时,我得到的结果如下: