Django and React app ERR_CONNECTION_REFUSED - django

I'm runing specfic configuration. I got django runing on apache on address localhost:8001 and react app running on nginx on localhost:8005. Also I use ngingx to route proxy. My biggest problem is when I try to call DRF API by React (calling http://127.0.0.1:8001/api-token-auth/ so I got / at the end of the endpoint for sure) I'm getting POST http://127.0.0.1:8001/api-token-auth/ net::ERR_CONNECTION_REFUSED
Backend is running fine, I can use endpoint from HTTPie or POSTMAN
When I call backend by external domain name in React like 'example.com/api-token-auth/' it's working just fine. How can I solve this?

Adding ALLOWED_HOSTS = ['127.0.0.1'] or ALLOWED_HOSTS = ['*'] may solve your issue of connection refused in your django settings.py file.
https://docs.djangoproject.com/en/3.0/ref/settings/#allowed-hosts
EDIT:
http://' + require("os").hostname() + ':8001/ or
http://' + require("ip").address() + ':8001/'
Try using this while building the localhost ip. People have fixed this issue with this change.

Related

Nginx proxy pass is having 502/504 error and cannot reach the backend

I have a react project deployed inside an nginx docker container which is deployed on k8s which is on AWS.
I have a backend service which is deployed also on aws and I have a url to access it. It can be accessed directly on browser, on postman or my react app if I remove the proxy pass for this url, but I need the proxy pass to solve other issues.
It was working at the beginning and stopped working and I got 504, after trying every related solution from stack overflow, now I get 502 instead. And backend shows that nginx could never reach this backend.
There are many other backend Urls being used exactly the same way, and they are totally fine, it is just this one url which is not accessible.
I also have a local nginx running which never gives me issue on this backend Url, but even if I redeploy the react nginx server on aws and redeploy that backend service, it is still giving me 502. Any one have opinion on this weird issue?
Thank you for taking a look.
I tried every possible and related config for the locaton of this url in conf file. But none of them worked

Django in Docker on Nginx request to self fails when in the context of another request

I have a Django application deployed inside a container on top of Nginx using Dokku. One of the view functions of the Django application includes a request:
views.py
def foo(request):
...
response = requests.get(url)
...
It is probably noteworthy that url is the url of the Django application itself, so the request is from the application to itself. The request is to one of the API endpoints (the reasons for doing this are historical). When the view is called then the request to url fails with 504 gateway timeout.
I cannot reproduce this in any other context specifically:
There is no error when running on localhost with the development server, where url is then the url of the development app (localhost to itself works).
There is no error when running on localhost with the development server, where I manually make the url the production url (localhost to production works).
There is no error when running this request on the production server but outside of the view. Specifically, I did a docker exec into the container, started the Django environment (manage.py shell), and ran the exact request that the view was making, and it worked! (production to production works)
It seems that only when the request is made in the context of a view which itself is answering another request I get an issue.
Any ideas?

How to solve ERR_TOO_MANY_REDIRECT when deploying django rest framework web app to Azure?

I deployed an web app which django restframework base on Heroku and Azure.
Same app on Heroku works fine.
But when I access to Azure, it causes ERR_TOO_MANY_REDIRECT error.
I googled and found that turn SECURE_SSL_REDIRECT off solved ERR_TOO_MANY_REDIRECT error.
However, it causes 403 CSRF error instead.
I need to find another way to fix ERR_TOO_MANY_REDIRECT or find a way to fix 403 CSRF error.
Can anyone help me to solve this issue?
If your app is on "Azure App Service", the HTTPS connection will be terminated before it reaches your web worker. Your app wil see an incoming HTTP request instead. In this case you need to set SECURE_SSL_REDIRECT = False indeed. If you want to enforce HTTPS (which is a good practice) you can do so in the Azure settings: https://learn.microsoft.com/en-us/azure/app-service/configure-ssl-bindings#enforce-https
About the CSRF-related error: because Azure translates HTTPS to HTTP, you need to configure Django to allow POST requests from a different scheme (since Django 4.0) by adding this to settings.py:
CSRF_TRUSTED_ORIGINS = ["https://YOUR-DOMAIN.com", "https://www.YOUR-DOMAIN.com"]
If this does not solve your problem, you can temporarily set DEBUG = True in production and try again. On the error page, you will see a "Reason given for failure" that you can post here.

AWS, Django, Apache, ALLOWED_HOSTS not working 400 Bad Request

I have two Django applications working on the AWS Lightsail. First one is working great with www.firstapp.com and firstapp.com, but when I try to visit the second app without www in URL, it returns 400 Bad Request. In both apps, DEBUG set to False, and I have necessary hosts in settings.py like this:
ALLOWED_HOSTS = [
'.secondapp.com'
]
I have tried with '*' and also tried to write down all possible hosts in ALLOWED_HOSTS but it didn't work. I am able to see website with www.secondapp.com but secondapp.com always return Bad Request (400)
After any update in settings.py, I always restart Apache (tried to reload also) nothing changes, still getting 400 Bad Request. Any ideas? Maybe I should set up AWS in some way, this is my first experience with Django
For anyone who will face this kind of issues, check your VirtualHost configurations. In my VirtualHost configurations I had ServerName as www.secondapp.com when I add ServerAlias secondapp.com it works. Now I am able to see my app with www.secondapp.com and secondapp.com.
P.S.: However I don't have ServerAlias for first application but it still working as www.firstapp.com and firstapp.com, not sure why this casing an issue for the second one.

nginx, django and url mapping

The admins where I work have a url http://company.com/app, public facing. They use apache http server. The sysadmins have http://company.com/app pointing to http://internal_ip_address:8080. I have nginx sitting on http://internal_ip:address listening on 8080. I am trying to get nginx to take requests coming into internal_ip_address, route the requests to localhost:9000 which is a django app. Once django is done with the request the resulting page renders with appropriate public facing url (e.g. http://company.com/app . . .) Thanks for any help!
Thanks to all those that tried to help. After spending a notable amount of time on the issue I determined that it was a http server config issue on a server managed elsewhere.