Nginx not directing to gunicorn socket, returns 404 Not Found

Abeer Sul

I can't seem to get nginx to redirect to my gunicorn socket. I tried many solutions from stackoverflow but code "looks" correct. Keeps giving me 404 page when trying to access https://example.com

enter image description here

Here is the gunicorn socket status, located at /var/www/example.com/example_app.sock:

example_app.service - uWSGI instance to serve the app "example.com"
     Loaded: loaded (/etc/systemd/system/example_app.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-09-29 16:08:45 CST; 26min ago
   Main PID: 154344 (gunicorn)
      Tasks: 3 (limit: 9522)
     Memory: 87.1M
     CGroup: /system.slice/example_app.service
             ├─154344 /usr/bin/python3 /usr/local/bin/gunicorn --workers 1 --bind unix:example_app.sock -m 007 run:app
             └─154363 /usr/bin/python3 /usr/local/bin/gunicorn --workers 1 --bind unix:example_app.sock -m 007 run:app

Sep 29 16:08:45 azompr1 systemd[1]: Started uWSGI instance to serve the app "example.com".
Sep 29 16:08:46 azompr1 gunicorn[154344]: [2020-09-29 16:08:46 +0800] [154344] [INFO] Starting gunicorn 20.0.4
Sep 29 16:08:46 azompr1 gunicorn[154344]: [2020-09-29 16:08:46 +0800] [154344] [INFO] Listening at: unix:example_app.sock (154344)
Sep 29 16:08:46 azompr1 gunicorn[154344]: [2020-09-29 16:08:46 +0800] [154344] [INFO] Using worker: sync
Sep 29 16:08:46 azompr1 gunicorn[154363]: [2020-09-29 16:08:46 +0800] [154363] [INFO] Booting worker with pid: 154363

example_app.service code, located at /etc/systemd/system/example_app.service:

[Unit]
Description=uWSGI instance to serve the app "example.com"
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/example.com
Environment="PATH=/var/www/example.com/env/bin"
#ExecStart=/var/www/example.com/env/bin/gunicorn --workers 2 --bind unix:example_app.sock -m 007 run:app
ExecStart=gunicorn --workers 1  --bind unix:example_app.sock -m 007  run:app

[Install]
WantedBy=multi-user.target

nginx settings, /etc/nginx/sites-available/example.com

server {

    return 301 https://$host$request_uri;


    listen 80;
    server_name example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/example.com;
    }

    location / {
    include proxy_params;
    proxy_pass http://unix:/var/www/example.com/example_app.sock;
   }
}
Abeer Sul

I found the cause, the related ssl certificate for this domain was a part of another certificate on the same server, it should not cause a big trouble if configured correctly I think.

This warning made me think about it:

nginx: [warn] conflicting server name "example.com" on 0.0.0.0:443, ignored

I checked all these to see what's going on:

sudo less /var/log/nginx/error.log: the Nginx error logs.
sudo less /var/log/nginx/access.log: the Nginx access logs.
sudo journalctl -u nginx: the Nginx process logs.
sudo journalctl -u example.com: Flask app’s Gunicorn logs.

But the solution I did was that I removed it from the other domain using this command by updating the certificate, I removed mine from the list:

sudo certbot --cert-name xxxx.org -d xxxxx.com -d yyyy.org -d yyyy.com

then re-created it for this domain.

sudo certbot --nginx -d example.com -d example.co -d www.example.com

And the final nginx config was this:

server {
    server_name example.com www.example.com example.co;
    access_log     /var/log/nginx/domains/main.example.com.log;
    error_log  /var/log/nginx/domains/main.example.com.error.log error;

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/example.com/azom_care.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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 = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


    listen 80;
    server_name example.com www.example.com example.co;
    return 404; # managed by Certbot

}

and the it worked correctly Alhamdullellah!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related