Allowing post request through Nginx - django

Trying to serve a react app, django with nginx and docker. I am not able to make a post call from docker on port 8080 to nginx on port 8082.
Following is the error thrown by Nginx
iris-frontend-frontend-1 | 2022/10/13 20:16:18 [error] 35#35: *4 open()
"/usr/share/nginx/html/add-to-waiting" failed (2: No such file or directory), client:
172.18.0.1, server: localhost, request: "POST /add-to-waiting HTTP/1.1", host:
"localhost:8080", referrer: "http://localhost:8080/"
Nginx configurations are:
add-to-waiting is the api call.
upstream django {
server website:8000;
}
upstream react_app {
server frontend:8080;
}
server {
listen 80;
client_max_body_size 100M;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
proxy_pass http://react_app;
}
location /admin {
proxy_pass http://django;
}
location /add-to-waiting {
proxy_pass http://django;
}
location /media/ {
alias /app/media/;
}
location /static/ {
alias /app/forex/static/admin/;
}
}
What configurations need to change to make frontend able to call the api?

Related

Nginx with Django and Gunicorn working with IP but not with domain name

I have been trying to setup my Django backend since 2days but i can't get it to work with my domain name. I have the Next Frontend on Nginx(Port :80) too but it seems to work fine with domain name. But i did the same setup in backend with port 8000 i can't access it using the domain name but works fine with IP. I have tried everything found on the internet but nothing seems to work.
Config for the frontend (Working with domain)
server{
listen 80;
listen [::]:80;
listen 443 ssl;
include snippets/snakeoil.conf;
server_name {domainName};
location = /favicon.ico { access_log off; log_not_found off; }
location / {
# reverse proxy for next server
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_headers_hash_max_size 512;
proxy_headers_hash_bucket_size 128;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Config for backend (Not working with domain name)
server {
listen 8000;
listen [::]:8000;
server_name dev.liqd.fi ipaddress;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/backend/lithexBackEnd;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Allowed Hosts
ALLOWED_HOSTS = ['localhost','127.0.0.1','ip address','*.domain.com','domain.com']
The Gunicorn has been setup and tested and seems to be working fine .
The error log of Nginx gives the following error when i try to access the port via the domain name.
/var/log/nginx/error.log
2023/01/22 10:43:58 [error] 33980#33980: *3 connect() failed (111: Connection refused) while connecting to upstream, client:server: dev.domain, request: "GET /app/login HTTP/1.1", upstream: "http://[::1]:3000/app/login", host:domain

How do I configure nginx with backend and frontend?

I have a simple vue.js and django (as REST API) application that I want to combine with nginx. Currently the frontend is working, but the backend is not. Here's my nginx config:
server {
listen 80;
location / {
root /usr/share/nginx/html/;
index index.html index.htm;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Visiting localhost works for the static files, but localhost/api leads to a bad gateway error:
[error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "localhost"
Also, trying to visit localhost/api via the frontend (axios) just returns the 'You need javascript to display this page' site, which is just part of the frontend.
Running the backend seperately, outside of docker and nginx, works fine on localhost:8000.
What can I do to make it work? It doesn't necessarily have to be done this way, as long as the frontend and backend can communicate.
You said you running Docker? Then you need to change localhost to the container name that running your backend.

Nginx: 502 BAD GATEWAY while deploying django to AWS EC2 instance

I am trying to deploy a django website on aws EC2 instance with Nginx. I am getting the following error.
*1 connect() failed (111: Connection refused) while connecting to upstream, client: **.***.***.***, , server: **.**.***.*, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "*.amazonaws.com"
In /etc/nginx/sites-available folder I have a file with the following configurations
server{
listen 80;
server_name your_public_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/mySiteFolder;
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
}
Where is the mistake that I am committing? Thank you in advance.

how to add new django app to deployed django project (using nginx,gunicorn)?

While running the django project locally, I can access my home, admin, app1, app2 directory (i.e
localhost:portnum , localhost:portnum/admin ,
localhost:portnum/app1 , localhost:portnum/app2 )
The problem begins when I deployed the app in a server ( I used nginx and gunicorn for django deployment with the help of this guide )
Problem : -
I'm unable able to access example.com/admin, example.com/app1 , example.com/app2.
I'm able to access my home example.com anyway.
When I trying to access example.com/app1/ the page give an error 403 forbidden
2018/11/17 18:00:55 [error] 28459#28459: *8 directory index of "/home/ubuntu/project/app/" is forbidden, client: 172.68.146.88, server: example.com, request: "GET /events/ HTTP/1.1", host: "www.example.com"
2018/11/17 18:00:58 [error] 28459#28459: *13 open() "/usr/share/nginx/html/app" failed (2: No such file or directory), client: 172.68.146.10, server: example.com, request: "GET /events HTTP/1.1", host: "www.example.com"
Some solutions which I tried to follow before this question::-
Django: when trying domain.com/admin gives 404 with an old unexisting urls file
Nginx 403 error: directory index of [folder] is forbidden
My nginx config
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /home/ubuntu/certs/cert.pem;
ssl_certificate_key /home/ubuntu/certs/cert.key;
server_name example.com;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /static/ {
root /home/ubuntu/example_project/app1;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
}
}
Thank You for trying to solve my problem.
When you use= in a location directive, it only applies for that exact path. Instead you should remove those for both of your locations and let nginx match for all prefixes.
location /static/ {
root /home/ubuntu/example_project/app1;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
}

Django gunicorn nginx "connection reset by peer while reading response header"

trying to set up django with gunicorn and nginx, getting some weird errors from nginx:
2015/10/07 06:17:22 [error] 28817#0: *12 connect() to
unix:/home/david/StockSearch/stocksearch/stocksearch.sock failed (111: Connection refused) while connecting to upstream,
client:79.176.114.181, server: 128.199.36.78, request: "GET / HTTP/1.1",
upstream: "http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock:/",
host: "128.199.36.78" 2015/10/07 06:17:24 [error] 28817#0: *12 recv()
failed (104: Connection reset by peer) while reading response header from upstream,
client: 79.176.114.181, server: 128.199.36.78, request: "GET / HTTP/1.1",
upstream: "http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock:/",
host: "128.199.36.78"
nginx config:
server {
listen 80;
server_name 128.199.36.78;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/david/StockSearch/stocksearch;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
}
}
gunicorn config:
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid david
setgid www-data
chdir /home/david/StockSearch/stocksearch
exec /home/david/Env/ssenv/bin/gunicorn --workers 3 --bind unix:/home/david/StockSearch/stocksearch/stocksearch.sock stocksearch.wsgi:application
I'm very new to nginx and gunicorn, but i think you can't do that:
proxy_pass http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
this is achived with fastCGI not with proxypass:
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
astcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
I can tell how i configure my nginx and gunicorn in my server, i hope my configuration helps:
server {
server_name XX.XX.XX.XX;
access_log /webapps/Project/log/acces.log;
error_log /webapps/Project/log/error.log warn;
location /static/ {
root /webapps/Project;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM N$
}
}
then i start my gunicorn server in my local machine like:
gunicorn Project.wsgi:application --bind 127.0.0.1:8001