how to add new django app to deployed django proje

2019-02-26 11:22发布

问题:

While running the django project locally, I can access my home, admin, app1, app2 directory (i.e localhost:portnum , localhost:portnum/admin , localhost:portnum/app1 , localhost:portnum/app2 )

The problem begins when I deployed the app in a server ( I used nginx and gunicorn for django deployment with the help of this guide )

Problem : - I'm unable able to access example.com/admin, example.com/app1 , example.com/app2. I'm able to access my home example.com anyway.

When I trying to access example.com/app1/ the page give an error 403 forbidden

2018/11/17 18:00:55 [error] 28459#28459: *8 directory index of "/home/ubuntu/project/app/" is forbidden, client: 172.68.146.88, server: example.com, request: "GET /events/ HTTP/1.1", host: "www.example.com"
2018/11/17 18:00:58 [error] 28459#28459: *13 open() "/usr/share/nginx/html/app" failed (2: No such file or directory), client: 172.68.146.10, server: example.com, request: "GET /events HTTP/1.1", host: "www.example.com"

Some solutions which I tried to follow before this question::-

  • Django: when trying domain.com/admin gives 404 with an old unexisting urls file
  • Nginx 403 error: directory index of [folder] is forbidden

    My nginx config

server {       
    listen 80;
    listen     443;

    ssl        on;
    ssl_certificate         /home/ubuntu/certs/cert.pem;
    ssl_certificate_key     /home/ubuntu/certs/cert.key;    
    server_name example.com;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    } 

    location = /static/ {
        root /home/ubuntu/example_project/app1;    
    }

    location = / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
    }
}

Thank You for trying to solve my problem.

回答1:

When you use= in a location directive, it only applies for that exact path. Instead you should remove those for both of your locations and let nginx match for all prefixes.

 location /static/ {
   root /home/ubuntu/example_project/app1;    
 }

 location / {
   include proxy_params;
   proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
 }