Home:ALL Converter>Serve Django and Angular with same nginx config

Serve Django and Angular with same nginx config

Ask Time:2016-06-13T13:59:38         Author:Deepankar Bajpeyi

Json Formatter

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

Author:Deepankar Bajpeyi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37782700/serve-django-and-angular-with-same-nginx-config
Govind Sharma :

I have integrated Django and Angular both together.\n\n# Angular with Django nginx\n\nupstream app_server {\n server unix:/home/<user>/run/gunicorn.sock fail_timeout=0;\n}\n\nserver {\n listen 80;\n\n # add here the ip address of your server\n # or a domain pointing to that ip (like example.com or www.example.com)\n server_name <server_ip>;\n\n root /home/<user>/angular_project/dist/angular_project;\n index index.html;\n\n keepalive_timeout 5;\n client_max_body_size 4G;\n\n access_log /home/<user>/logs/nginx-access.log;\n error_log /home/<user>/logs/nginx-error.log;\n\n location /static/ {\n alias /home/<user>/django_project/static/;\n }\n\n location / { \n try_files $uri $uri/ /index.html;\n } \n\n location ~^/(admin|api) {\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n proxy_set_header Host $http_host;\n proxy_set_header X-Forwarded-Proto $scheme;\n proxy_set_header X-Forwarded-Host $host;\n proxy_set_header X-Real-IP $remote_addr;\n add_header P3P 'CP=\"ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV\"';\n proxy_redirect off;\n proxy_pass http://app_server;\n }\n}\n",
2019-05-18T05:59:54
yy