'how to serve static files through nginx and django in windows

I am a newbie. Just got setup the apache server in windows. But now I need nginx to serve my static files and I think I have searched almost everywhere how to configure nginx to serve static files, got many answers, but it was hard to understand. Can someone please explain where do I start from and how to configure nginx and django in windows in a noob's, perspective. Any help will be appreciated. Thank you!



Solution 1:[1]

Try something like this in your server section:

location /static/ {
    root /the_directory_with_your_files/;
}

Solution 2:[2]

edit the nginx.conf file located in the conf folder of the unzipped nginx for windows and add the following directive (You have to update the path to match the folder of your django project)

location /static/ {
        alias 'C:/Users/user1/your_django_project/staticfiles/';
    }

an example of your final nginx.conf could look like this if you are running a django webserver like hypercorn or other on port 8000 on localhost

http{
    server {
    server_name localhost;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host 127.0.0.1;
        proxy_set_header X-Real-IP 127.0.0.1;
        proxy_set_header X-Forwarded-For 127.0.0.1;
      proxy_set_header X-Forwarded-Proto http;
    }

    location /static/ {
        alias 'C:/Users/user1/your_django_project/staticfiles/';
    }
  }
  upstream backend {
      server 127.0.0.1:8000;
      # There could be more than a backend here
  }
}

events {
  worker_connections  1024;
}

tested on nginx 1.20

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wolph
Solution 2 Nwawel A Iroume