Deepankar Bajpeyi
Deepankar Bajpeyi

Reputation: 5869

Serve Django and Angular with same nginx config

I am trying to setup Django and Angular app with Uwsgi and nginx

My config :

upstream django {
    server unix:///home/deepankar/stuff/proj/server/project/mysite.sock; # for a file socket
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80; 
    # the domain name it will serve for
    server_name _; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required                                             
    }   


    location / { 
        root /home/deepankar/stuff/proj/client/build; # your Django project's static files - amend as required
        try_files $uri $uri/ /index.html;
    }   

    # Finally, send all non-media requests to the Django server.
    location /api {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    } 

How do I make sure that all /api gets routed to the uwsgi django server and / should get routed to the angular application which is compiled under the build folder.

Right now Everything gets routed to the angular app

Upvotes: 4

Views: 3912

Answers (2)

Govind Sharma
Govind Sharma

Reputation: 69

I have integrated Django and Angular both together.

# Angular with Django nginx

upstream app_server {
    server unix:/home/<user>/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;

    # add here the ip address of your server
    # or a domain pointing to that ip (like example.com or www.example.com)
    server_name <server_ip>;

    root /home/<user>/angular_project/dist/angular_project;
    index index.html;

    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /home/<user>/logs/nginx-access.log;
    error_log /home/<user>/logs/nginx-error.log;

    location /static/ {
        alias /home/<user>/django_project/static/;
    }

    location / { 
        try_files $uri $uri/ /index.html;
    }  

    location ~^/(admin|api) {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}

Upvotes: 2

ohrstrom
ohrstrom

Reputation: 2970

Nginx tries the locations from 'top to down'. So you have to change the ordering so that /api comes before /:

location /api {
    uwsgi_pass  django;
    include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
} 

location / { 
    root /home/deepankar/stuff/proj/client/build; # your Django project's static files - amend as required
    try_files $uri $uri/ /index.html;
}   

Upvotes: 1

Related Questions