Home > Back-end >  Nginx returns standard pages and ignores Django urls
Nginx returns standard pages and ignores Django urls

Time:09-20

I've tried everything, and I don't understand what the problem is.

OS: Ubuntu 20.04.5 LTS

Nginx config:

/etc/nginx/sites-available/default

server {
        listen          80;
        server_name     ***.**.***.***;
        charset         utf-8;
        client_max_body_size 10M;

        location /static {
                alias /var/django-backend/static;
        }

        location /media {
                alias /var/django-backend/media;
        }

        location / {
                proxy_set_header Host $http_host;
                proxy_pass http://unix:/run/gunicorn.sock;
        }
}

P.S. Then I run the command sudo ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled

Gunicorn service config:

/etc/systemd/system/gunicorn.service

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

[Service]
User=root
WorkingDirectory=/var/django-backend
ExecStart=/var/django-backend/venv/bin/gunicorn \
          --access-logfile - \
          -k uvicorn.workers.UvicornWorker \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          backend.asgi:application

[Install]
WantedBy=multi-user.target

/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

When I go to any endpoint, nginx returns a 404 page Nginx 404 page

CodePudding user response:

Seeing you can still access the Static files it's probably something with the Gunicorn Settings. Double Check that it's running and not throwing up any errors..

Looking at the Gunicorn Docs, and my own uWsgi.Nginx Settings, it looks like it's normally set up like (snippet) so try giving that a shot
Note: That it's seperated into it's own upstream section that is outside of the server section

upstream django {
  # full path to socket, (what I use)
  server unix:///run/gunicorn.sock;
  # `cd /run/gunicorn.sock` would be the location

  # the doc example is:
  # server unix:/tmp/gunicorn.sock fail_timeout=0;
}

server {
  listen          80;
  server_name     ***.**.***.***;
  charset         utf-8;
  client_max_body_size 10M;

  location /static {
    alias /var/django-backend/static;
  }

  location /media {
    alias /var/django-backend/media;
  }

  location / {
    proxy_set_header Host $http_host;
    proxy_pass http://django;

    # The Docs also has all this extra Junk, idk if it's important 

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # we don't want nginx trying to do something clever with
    # redirects, we set the Host: header above already.
    proxy_redirect off;
  }
}
  • Related