【问题标题】:Nginx is not proxying GunicornNginx 没有代理 Gunicorn
【发布时间】:2023-03-12 23:12:01
【问题描述】:

我正在尝试使用 Gunicorn 和 Nginx 部署我的 Flask 项目,但我仍在苦苦挣扎。

/etc/systemd/system/gunicorn3.service

[Unit] Description=Gunicorn service After=network.target

[Service]
User=www-data
Group=adm 
WorkingDirectory=/home/project/
Environment="PATH=/home/project/env/bin" 
ExecStart=/home/project/env/bin/gunicorn --workers 3 --bind unix:cima.sock -m 007 run:app

[Install] WantedBy=multi-user.target

然后我检查是否有任何问题,一切似乎都正常

>>sudo systemctl status gunicorn3
gunicorn3.service - Gunicorn service
   Loaded: loaded (/etc/systemd/system/gunicorn3.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-01-17 20:25:01 UTC; 18min ago
 Main PID: 18451 (gunicorn)
    Tasks: 4 (limit: 4915)
   CGroup: /system.slice/gunicorn3.service
           ├─18451 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
           ├─18453 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
           ├─18454 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
           └─18456 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo

Jan 17 20:25:01 acelerathon-cima-grupo-3 systemd[1]: Started Gunicorn service.
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18451] [INFO] Starting gunicorn 19.9.0
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18451] [INFO] Listening at: unix:cima.sock (18451)
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18451] [INFO] Using worker: sync
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18453] [INFO] Booting worker with pid: 18453
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18454] [INFO] Booting worker with pid: 18454
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18456] [INFO] Booting worker with pid: 18456

/etc/nginx/sites-enabled/项目

server {
    listen 80;
    server_name 127.0.0.1;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cima.sock;
    }
}

然后我检查了错误。

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

所以这两个服务都可以,现在当我访问 http://localhost 时,我看到的是 Nginx 主页,而不是我的 Flask 应用主页

我不确定为什么不代理

【问题讨论】:

    标签: nginx flask gunicorn


    【解决方案1】:

    在 Nginx 中将 localhost 添加到 server_name 指令。 Nginx 使用这个指令来匹配每个请求的服务器块,在你的情况下 localhost 不匹配 127.0.0.1 所以它是默认服务器的服务器。 可能这个例子有助于理解:

    server {
            listen 127.0.0.1:80;
            server_name 127.0.0.1;
            return 200 "At 127.0.0.1\n";
    }
    
    server {
            listen 127.0.0.1:80;
            server_name localhost;
            return 200 "At localhost\n";
    }
    
    > curl -4 127.0.0.1:80
    At 127.0.0.1
    > curl -4 localhost:80
    At localhost
    

    【讨论】:

    • 谢谢,现在我的输出和你一样,但是我的应用程序托管在 VM 中,我的 url publicIP 仍然指向 Ngnix 主页 P.D.为什么 curl 必须使用“-4”?
    猜你喜欢
    • 2021-01-29
    • 2017-07-17
    • 2018-01-28
    • 2019-06-15
    • 2012-11-22
    • 2021-05-02
    • 1970-01-01
    • 2013-06-04
    • 2013-10-13
    相关资源
    最近更新 更多