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

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.

Related

502 gateway error | sentence-transformer package not working correctly

Hello I'm trying to solve this problem from last 5days if anyone know the solution please reply.
My website is working properly but one page giving 502 gateway error on post request. The code is working properly on local server but not working on nginx live server.
I thought it could be giving error cause that post request take long time to process so i increase timeout in nginx.server file but still it's not working
/etc/nginx/sites-available/myproject
server {
listen 80;
server_name 54.199.217.173;
client_max_body_size 20M;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/bankproject/Live-Banking-Solutions-Applications;
}
location /media/ {
root /home/ubuntu/bankproject/Live-Banking-Solutions-Applications;
}
location / {
include proxy_params;
proxy_read_timeout 600s;
proxy_connect_timeout 600s;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Nginx error.log file giving this error
For that page
2022/12/26 07:21:57 [error] 2197#2197: *2 upstream prematurely closed connection while reading response header from upstream, client: 106.211.97.247, server: 54.199.217.173, request: "POST /admin/scaning_signature HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/admin/scaning_signature", host: "54.199.217.173", referrer: "http://54.199.217.173/admin/get_signature"
I mentioned sentence-tranformers in question cause only that page is giving error where i use sentence-transformer. other pages post requests are working correctly

Allowing post request through Nginx

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?

"(111: Connection refused) while connecting to upstream, client: " | Django, Gunicorn, Nginx - Server Error(500)

I have deployed my django app, but have a 500 error. When i type in the home url "mysite.com", it comes with the error and does not load, but when i type in "mysite.com/careers", or "mysite.com/sell", which happen to be fully static pages, it comes up beautifully. The home page, and other pages which have dynamic data do not serve.
Afer reading some related questions, it seems to be a Gunicorn issue.
What could be the issue here? Below is the error log from Nginx.
Nginx Error log:
2019/12/16 17:30:15 [error] 20605#20605: *1370 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xx.xx, server: xxx.xxx.xxx.xxx, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "mysite.com"
nginx .conf file:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
client_max_body_size 0;
proxy_max_temp_file_size 0;
proxy_buffering off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
...
}
nginx sites available file:
server {
listen 80;
server_name xxx.xxx.xxx.xxx mysite.com www.mysite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/djangoadmin/pyapps/MySite;
}
location /media/ {
root /home/djangoadmin/pyapps/MySite;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Would appreciate any help in figuring this out. I'm almost there!
I believe that you have something from this list of possible troubles:
you don't have gunicorn running because of any error or it was not just started yet.
You have your gunicorn process listening on another unix socket path or ip port.
your socket has incorrect permissions and nginx can't connect to it because of permissions issues
The root of my issue was a failure to run new migrations on my server.
All of the pages that were effected were those which displayed data associated with those models, or for which they were a foreign key.
In finding the answer, I committed a grave production sin, which was to briefly turn Debug:On. I hope the programming gods forgive. Thanks guys.

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;
}

Rails/Nginx/Capistrano/Puma: (111: Connection refused) while connecting to upstream

I keep getting this error in the nginx.error.log:
2016/06/06 20:14:02 [error] 907#0: *1 connect() to unix:///home/user/apps/appname/shared/tmp/sockets/appname-puma.sock failed (111: Connection refused) while connecting to upstream, client: 50.100.162.19, server: , request: "GET / HTTP/1.1", upstream: "http://unix:///home/user/apps/appname/shared/tmp/sockets/appname-puma.sock:/", host: "appname.com"
(here it is with manually added newlines for your convenience)
2016/06/06 20:14:02 [error] 907#0: *1 connect() to
unix:///home/user/apps/appname/shared/tmp/sockets/appname-puma.sock failed
(111: Connection refused) while connecting to upstream, client:
50.100.162.19, server: , request: "GET / HTTP/1.1", upstream:
"http://unix:///home/user/apps/appname/shared/tmp/sockets/appname-
puma.sock:/", host: "appname.com"
This is my nginx.conf:
upstream puma {
server unix:///home/user/apps/appname/shared/tmp/sockets/appname-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/user/apps/appname/current/public;
access_log /home/user/apps/appname/current/log/nginx.access.log;
error_log /home/user/apps/appname/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
What am I doing wrong?
I followed Digital Ocean's tutorial to set up Capistrano, Nginx and Puma.
So the solution was to restart puma.
cap production deploy:restart
Every time I reboot the server, I need to restart puma as well.
My recommendation is to check
~/apps/appname/shared/log/puma.stderr.log
log file. You may find there the answer
looking at log/puma_error.log i saw the error (LoadError while trying to load bundler), doing gem update --system fixed it.