Nginx 413 Request Entity Too Large, Docker, Django - django

I get hit with the error
413 Request Entity Too Large
when I try to upload any file larger than 1.5MB.
I have seen various answers out there regarding this issue but nothing seems to work with my situation:
my nginx default.conf:
upstream django {
server store:27038;
}
server {
listen 27036;
location /static {
alias /vol/static;
}
location / {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
my nginx Dockerfile:
FROM nginxinc/nginx-unprivileged:1-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf
USER root
RUN mkdir -p /vol/static
RUN chmod 777 /vol/static
USER nginx
I keep on seeing responses saying that just adding something like
server {
client_max_body_size 100M;
...
}
somewhere in Nginx should solve the issue but I keep getting an error when I try to put it on my conf file so am not entirely sure where this code is supposed to go. If anyone has an answer for this it would be much appreciated

So funny enough there seemed to be some sort of caching issue on my dockerfile so the changes didn't properly take effect, thus the reason why the solution wasn't working:
upstream django {
server store:27038;
}
server {
client_max_body_size 100M;
listen 27036;
location /static {
alias /vol/static;
}
location / {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $
proxy_set_header Host $host;
proxy_redirect off;
}
}
This did the trick for me. Simply adding the client_max_body_size 100M and rebuilding the image from scratch.

Related

Django swagger ui not accessible on nginx

I have setup Django using nginx, gunicorn and postgres as per below url.
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7
Now I am trying to access swagger ui.
Nginx is up and running however showing default page.
When I run the same project using,
python manage.py runserver myip:8000
and then access the same url I can see actual swagger ui with rest end points.
I am not sure what I am doing wrong here.
Here is what I have added to nginx file.
server {
listen 80;
server_name <myipaddress>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/threat-dashboard/backend;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/tmp/backend.sock;
}
}
There was a mistake in sock file path. I corrected it from /tmp/backend.sock to /tmp/backend/backend.sock and it resolved the issue.

Trouble with deploy django channels using Daphne and Nginx

I got a 502 error when I'm trying to open a website. I used the instructions from the official website link
Added new file lifeline.conf at /etc/supervisor/conf.d/
lifeline.conf
[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000
# Directory where your site's project files are located
directory=/home/ubuntu/lifeline/lifeline-backend
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-head$
# Number of processes to startup, roughly the number of CPUs you have
numprocs=4
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=/home/ubuntu/asgi.log
redirect_stderr=true
Setup nginx conf
upstream channels-backend {
server localhost:8000;
}
server {
listen 80;
server_name staging.mysite.com www.staging.mysite.com;
client_max_body_size 30M;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
I checked the asgi log file and it contains an error .
daphne: error: the following arguments are required: application
I'm guessing a mistake in lifeline.conf.
I am assuming you are not passing asgi application to daphne, because configuration you pasted in question has missing line. You have to pass it correctly. Assuming you have conf package with asgi.py module inside it containing asgi application instance, you have to do
command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock conf.asgi:application
conf.asgi:application should be at the end.

Django nginx 502 bad gateway

I am learing nginx for django, i am running the server in locally with docker.
this is my django_nginix.conf file :
server {
listen 80;
listen [::]:80 default_server;
server_name musicaldd.com;
client_max_body_size 90M;
location /media/ {
root /home/pyking/cpd/musicaldd_back/storage/;
}
location /static/ {
root /home/pyking/cpd/musicaldd_back/storage/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://djangoapp:8000/;
}
}
When I run the server creating docker images and hit the URLs, it returns me 502-bed getaway timeout | nginix Can anyone tell me why this errors occuse, how can I fix this error
It may stupid question but this is very serious to me, coz, i am new on Nginx, it will really much be appreciated if you help me to fix this
If you are using uwsgi add this to your nginx.conf file based on your uwsgi params file location.
include /etc/nginx/uwsgi_params;
uwsgi_pass unix://tmp/uwsgi.sock;
uwsgi_param SCRIPT_NAME /;
If you are not i suggest use that. there is another option called gunicorn but uwsgi has better performance.

Nginx reverse proxy configuration for subdomain with multiple paths

I have a situation here with my Nginx reverse proxy configuration. My distribution is Ubuntu 14.04
I have a domain, let's call it foo.bar.net, and I want the /grafana endpoint to redirect to my grafana server (localhost:3000), the /sentry endpoint to redirect to my sentry server (localhost:9000) and finally, the /private endpoint to redirect to my django server (localhost:8001). I am using gunicorn for the tuneling between django and nginx.
Here is what I tried :
server {
# listen on port 80
listen 80 default_server;
# for requests to these domains
server_name foo.bar.net;
location /sentry {
# keep logs in these files
access_log /var/log/nginx/sentry.access.log;
error_log /var/log/nginx/sentry.error.log;
# You need this to allow users to upload large files
# See http://wiki.nginx.org/HttpCoreModule#client_max_body_size
# I'm not sure where it goes, so I put it in twice. It works.
client_max_body_size 0;
proxy_pass http://localhost:9000;
proxy_redirect off;
proxy_read_timeout 5m;
allow 0.0.0.0;
# make sure these HTTP headers are set properly
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /grafana {
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_read_timeout 5m;
allow 0.0.0.0;
# make sure these HTTP headers are set properly
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /private {
proxy_pass http://127.0.0.1:8001;
}
location /private/static/ {
autoindex on;
alias /home/user/folder/private/static/;
}
}
The server won't even start correctly, the config is not loading.
I would also like the / path to redirect to the private endpoint if possible.
Additionally, I am not even sure where to put this configuration (sites-available/??)
Can anyone help me with that ?
Thanks a lot,
There are some missing semicolons and other syntax errors. Look at main nginx error log for details and fix them one by one.
Where to put that config file depends on your distribution. For some of them it should be sites-available directory and symlink to that file inside sites-enabled directory for quick enabling and disabling sites, if you don't have sites-available and sites enabled directory, you should put it into conf.d dir in your distribution.

Nginx + Gunicorn POST request error

Im using nginx as a proxy for a django application that uses gunicorn, the problem is that at some point I receive a POST request from another site.
The problem seems to be that nginx does not redirect the POST request properly to the gunicorn daemon.
What can I do to fix this, what I need is to be able to send the POST request as it arrives to the gunicorn daemor for my django app to process it... thank you...
This is my nginx conf
server {
server_name www.rinconcolombia.com;
access_log /var/log/nginx/rinconcolombia.log;
location / {
ssi on;
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
autoindex on;
root /home/rincon/sites/rinconcolombia/checkouts/rinconcolombia/;
}
location /static/admin_media/ {
autoindex on;
root /home/rincon/sites/rinconcolombia/checkouts/rinconcolombia/;
}
}
server {
server_name www.rinconcolombia.com;
rewrite ^(.*) http://www.rinconcolombia.com$1;
}
UPDATE The app sending the POST is receiving a BAD REQUEST error... if I manually make a POST with resty or curl It does pass the post message to my server...
Your nginx configuration is slightly wrong as you're missing the fail_timeout bits. See here for the gunicorn/nginx example: https://github.com/benoitc/gunicorn/blob/master/examples/nginx.conf
Specifically lines 58 and 115.
If that doesn't help do you get anything in the nginx error.log?