Issues serving static files with nginx (Django) - django

I'm a bit new to this but I am trying to deploy a website I build using Django to DigitalOcean using nginx/gunicorn.
My nginx file looks as so:
server {
listen 80;
server_name xxx.xxx.xxx.xx;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
alias ~/dev/WebPortfolio/static/;
}
}
And my settings.py file looks as so:
STATIC_ROOT = '~/dev/WebPortfolio/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
Every time I run python managy.py collect static the errors look as so:
You have requested to collect static files at the destination
location as specified in your settings:
/root/dev/WebPortfolio/~/dev/WebPortfolio/static
Looking at the nginx error log I see (cut out the repetitive stuff):
2015/10/08 15:12:42 [error] 23072#0: *19 open() "/usr/share/nginx/~/dev/WebPortfolio/static/http:/cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.e asing.min.js" failed (2: No such file or directory), client: xxxxxxxxxxxxxx, server: xxxxxxxxxxxxxx, request: "GET /static/http%3A//cdnjs.cloudflare.com/aj ax/libs/jquery-easing/1.3/jquery.easing.min.js HTTP/1.1", host: "XXXXXXXX.com", referrer: "http://XXXXXXXX.com/"
2015/10/08 15:14:28 [error] 23072#0: *24 connect() failed (111: Connection refused) while connecting to upstream, client: xxxxxxxx, server: 104.236.174.46, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "xxxxxxxx.com"
1) I'm not entirely sure why my destination for static files is '/root/dev/WebPortfolio/~/dev/WebPortfolio/static'

Because you've used '~' in a path. That's a shell thing, not a general path thing, and unless you tell Python specifically, it won't know what to do with it. Use a full absolute path in both Django settings and nginx.

Related

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?

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

DigitalOcean add domain to django instance with nginx and gunicorn

I've setup the one click install django on digitalocean and added a domain to it. I'm also trying to add a sub domain before the site goes live. I've edited the nginx conf file as below
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name beta.kazi-connect.com;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
and restarted both nginx and gunicorn however when I visit the sub domain I get a 502 bad gateway error.
Nginx log states there's an issue with gunicorn.
2017/01/24 16:24:19 [error] 6258#6258: *2 upstream prematurely closed connection while reading response header from upstream, client: 105.230.203.101, server: beta.kazi-connect.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "beta.kazi-connect.com"
2017/01/24 16:24:20 [error] 6258#6258: *2 upstream prematurely closed connection while reading response header from upstream, client: 105.230.203.101, server: beta.kazi-connect.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/favicon.ico", host: "beta.kazi-connect.com", referrer: "http://beta.kazi-connect.com/"
2017/01/24 16:24:22 [error] 6258#6258: *2 upstream prematurely closed connection while reading response header from upstream, client: 105.230.203.101, server: beta.kazi-connect.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "beta.kazi-connect.com"
2017/01/24 16:24:23 [error] 6258#6258: *2 upstream prematurely closed connection while reading response header from upstream, client: 105.230.203.101, server: beta.kazi-connect.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/favicon.ico", host: "beta.kazi-connect.com", referrer: "http://beta.kazi-connect.com/"
2017/01/24 16:25:00 [error] 6258#6258: *23 upstream prematurely closed connection while reading response header from upstream, client: 105.230.203.101, server: beta.kazi-connect.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "beta.kazi-connect.com"
2017/01/24 16:25:01 [error] 6258#6258: *23 upstream prematurely closed connection while reading response header from upstream, client: 105.230.203.101, server: beta.kazi-connect.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/favicon.ico", host: "beta.kazi-connect.com", referrer: "http://beta.kazi-connect.com/"
Samuel's answer was right. The problem is that the domain names are not included in the ALLOWED_HOSTS. In the django_project/settings.py, search for the code below.
# Discover our IP address
ALLOWED_HOSTS = ip_addresses()
Add your domain name to ALLOWED_HOSTS, e.g.
ALLOWED_HOSTS.extend(["xyz.com"])

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.

sprockets failed to prepare assets for nginx

I want to let nginx serve the static files for cache task.
But I got No such file or directory from nginx error log.
It seems the css files included in root-c452663d516929cd4bb4c1cd521971eb.css could not be served.
How could I fix the bug
production.rb
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
#config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.serve_static_files = false
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css
/* Summernote */
#import url('plugin/summernote/summernote.css');
#import url('plugin/summernote/summernote-bs3.css');
/* Sweet Alert */
#import url('plugin/sweet-alert/sweet-alert.css');
/* Data Tables */
#import url('plugin/datatables/datatables.css');
/* Chartist */
#import url('plugin/chartist/chartist.min.css');
/* Rickshaw */
#import url('plugin/rickshaw/rickshaw.css');
#import url('plugin/rickshaw/detail.css');
#import url('plugin/rickshaw/graph.css');
#import url('plugin/rickshaw/legend.css');
nginx setting
location / {
try_files $uri #sample;
gzip_static on;
expires max;
add_header Cache-Control public;
}
location #sample {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://sample;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
Error log
2015/07/22 17:27:02 [error] 9891#0: *9 open() "/var/public/assets/kode/css/plugin/date-range-picker/daterangepicker-bs3.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/date-range-picker/daterangepicker-bs3.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
2015/07/22 17:27:02 [error] 9891#0: *10 open() "/var/public/assets/kode/css/plugin/rickshaw/legend.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/rickshaw/legend.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
2015/07/22 17:27:02 [error] 9891#0: *11 open() "/var/public/assets/kode/css/plugin/rickshaw/detail.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/rickshaw/detail.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
2015/07/22 17:27:02 [error] 9891#0: *5 open() "/var/public/assets/kode/css/plugin/fullcalendar/fullcalendar.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/fullcalendar/fullcalendar.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
puma.config
app_path = File.expand_path('../', File.dirname(__FILE__))
pidfile "#{app_path}/tmp/pids/puma.pid"
bind "unix:///tmp/puma.lazyair.sock"
stdout_redirect "#{app_path}/log/puma.stdout.log", "#{app_path}/log/puma.stderr.log", true
workers Integer(ENV['WEB_CONCURRENCY'] || 6)
threads_count = Integer(6)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3457
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
on_worker_boot do
ActiveRecord::Base.establish_connection
end
activate_control_app