0

We are running Django app using PM2 with following script:

   apps:
      [{
        name: "app_name",
        script: "manage.py",
        args: ["runserver", "127.0.0.1:8000"],
        exec_mode: "fork",
        instances: "1",
        wait_ready: true,
        autorestart: false,
        max_restarts: 5,
        interpreter : "python3"
      }]
}

And we expect nginx to tunnel it to world. While nginx -t results ok but nginx fails to start.

Following is the nginx configuration:

upstream app_server {
    server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
    listen 8000 default_server;
    listen [::]:8000 default_server;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/app_name/app_name/media;
    }

       # your Django project's static files - amend as required
    location /static {
        alias /home/django/app_name/app_name/static;
    }

    # Proxy the static assests for the Django Admin panel
    # Warning! This location is highly sensitive to how django is installed sys>
    location /static/admin {
       alias /usr/local/lib/python3.8/dist-packages/django/contrib/admin/static>
    }

    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://127.0.0.1:8000/;
        }

}

When we restart nginx with: sudo service nginx restart

We get control process exited with error code. After debugging with journalctl -xe we get:

The unit nginx.service has entered the 'failed' state with result 'exit-code'.
Failed to start A high performance web server and a reverse proxy server.

What could be going wrong?

  • 1
    maybe you have default nginx site already on port 80. remove it with `sudo rm /etc/nginx/sites-enabled/default` then run `sudo service nginx restart` – monim Sep 28 '22 at 06:44
  • I tried `npx kill-port 8000` but there are no processes on that port –  Sep 28 '22 at 12:44
  • But why you're using npx if it is related to django and nginx – monim Sep 28 '22 at 17:29
  • Using npx as a tool to kill process on that port if any exists –  Sep 29 '22 at 07:51

0 Answers0