PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 11, 2022

[FIXED] How to return Django project's error messages when I use nginx in production mode

 July 11, 2022     django, message, nginx, response     No comments   

Issue

I have developed the Django project and deployed it to the Amazon's free tier EC2 services. Everything is fine except errors message that are not returning back. I am using the project in production mode.

Explanation for above image [Console Log]:

  1. Successful request and response - it was made for existing url

  2. Second request is made intentionaly to non existing url and did not receive any response.

I want to get at least 404 response, the problem I have is not having any response from server. When I run it on server I saw it is logging the results to the server.

Question: How to return the response that Django is generating when something is wrong. Extra Info: Those error messages and response are getting generated in djangorestframework's built in template.

Extra Details:

djangorestframework Diagram Explanation

Let me know if I am missing anything.


Solution

Brain does really interesting things when it is tired. Thanks to @iklinac. He was right, better I would have used django-cors-headers correctly. It was already installed and working on heroku, when I moved to amazon aws I thought anything was related to NGINX.

Notes to take.

  1. pip install django-cors-headers

  2. Make sure it is in your installed apps.

INSTALLED_APPS = [

    ...
    'corsheaders',
    ...
 ]
  1. You will also need to add a middleware class to listen in on responses: # which I have missed
MIDDLEWARE = [  # Or MIDDLEWARE_CLASSES on Django < 1.10
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]
  1. A list of origins that are authorized to make cross-site HTTP requests
CORS_ORIGIN_WHITELIST = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000"
]

Then there are few other things you can tweak and use as you want.

Eventually I have changed my nginx.conf to following

upstream hello_django {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://hello_django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /home/app/web/staticfiles/;
    }

    location /mediafiles/ {
        alias /home/app/web/mediafiles/;
    }

}

Happy coding.) credits to testdriven.io and django-cors-headers



Answered By - Madiyor
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing