Diego Lopez
Diego Lopez

Reputation: 27

Nginx is not proxying Gunicorn

I'm trying to deploy my Flask project using Gunicorn and Nginx but I'm still struggling.

/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

Then I check if there's any problem, everything seems OK

>>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/project

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;
    }
}

And then I checked for errors.

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

So both services are OK, now when I access to http://localhost I see the Nginx Home Page, not my Flask app home page

I'm not sure why is not proxying

Upvotes: 0

Views: 157

Answers (1)

Fedor Dikarev
Fedor Dikarev

Reputation: 506

Add localhost to server_name directive in Nginx. Nginx use this directive to match server block for each request and in your case localhost doesn't match 127.0.0.1 so it's server by default server. May be this example will help to understand:

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

Upvotes: 0

Related Questions