0

I have deployed a simple Django application in the AWS server and created a config file in the Nginx as follows. But its static files are not detecting. Location of my static folder location: /path/static.

This application checks for static files by the URL HTTP://public_ip/static, but I need to achieve the same in HTTP://public_ip/portal this URL

                  server {
                     listen 80;
                     server_name 127.0.0.1;
                     location /portal {
                     include proxy_params;
                     proxy_pass http://localhost:8000/en;
                        }
                    }
aks
  • 37
  • 1
  • 6

1 Answers1

1

You need to have following server config:

server {
    listen 80;
    server_name 127.0.0.1;

    location ~ /portal(?<djangouri>.*) {
        alias /home/ubuntu/en/static;

        try_files $uri $uri/ @django;
    }

    location @django {
        include proxy_params;

        proxy_pass http://localhost:8000/en$djangouri;
    }
}

First location block checks if a file exists in the path, and serves it. If no file exists, then request is passed to django.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • thank you for the reply. Now I am getting this error in Nginx by adding the above config file. nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block – aks Feb 21 '21 at 06:06
  • Ah yes, one needs to explicitly specify `proxy_pass` destination in the named location. Please try updated answer. – Tero Kilkanen Feb 21 '21 at 13:11