skip to Main Content

I am new in deploy projects my static files not serving with nginx.

thats look my site-available/myprject file

server{
    listen 80;
    server_name mydomain;

    location = /favicon.ico { access_log off; log_not_found off; }
    
    location /static/ {
    autoindex on;
    alias /home/user/project/static;
    }
    
    location /media/ {
    autoindex on;
    alias /home/user/project/media;
    }

    location / {
     proxy_pass myIp:myPort;

    }
}

My static files and media have this path:

/home/user/project/staict files and media files

that’s how it looks my settings.py configurations

STATIC_URL = '/static/'
STATIC_ROOT =os.path.join(BASE_DIR,'static')

my debug variable is false

I run collectstatic.

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem this way:

    First you have to add in youre urlpatterns in urls.py

    static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    
    1. create dir in /var/www/html/static

    2. sudo chmod -R 777 /var/www/html/static

    3. Run collectstatic

    4. set your location in sites-available/project (you nginx config)


  2. Here is what I see that this different from my working Django site:

        server_name mydomain;```
    should be->
    
    ```listen 80;
        server_name myIP;```
    
    ```alias /home/user/project/static;``` 
    should be ->
    
    ```alias /home/user/project/static/;```
    
    ```location / {
         proxy_pass myIp:myPort;``` 
    should be ->
    
    ```locatinon /{ 
          include        proxy_params;
          proxy_pass     http://myIp:myPort; ```
    
    then
    
    ```STATIC_URL = '/static/'
    STATIC_ROOT =os.path.join(BASE_DIR,'static')```
    should be ->
    
    ```STATIC_URL = '/static/'
       STATIC_ROOT = /home/user/project/static;
       STATICFIES_DIRS = [os.path.join(BASE_DIR, 'static/'),
       ]```
    
    
    This is the way my website is setup and it is working totally fine. However. if you change your static files, you will need to rerun "collecstatic."
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search