django+nginx+uwsgi 404 on static files - django

I have a django app, using nginx and uwsgi to serve it. My static files return a 404. I have set ownership of the files to correspond to the nginx username. The path to the static folder was copied directly from terminal, and I ran collectstatic (the static files are successfully served via the django development server). I have also run
chown -R username:username /home
and
chmod -R ug+r /home
I have tried both alias and root.
STATIC_ROOT is in my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Here is my nginx.conf:
user username;
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream app_servers {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to serve static files
location /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
alias /home/myapp/myapp/static/;
}
# Serve a static file (ex. favico)
# outside /static directory
location = /favico.ico {
root /app/favico.ico;
}
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass http://app_servers;
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;
}
}
}

It is a bad idea to chown the /home directory to some user. That directory should belong to root. Change it back to what it was:
chown root:root /home
chmod u=rwx,g=rx,o=rx /home
(The chmod you've run has probably not altered its permissions, but better be certain, therefore the second command above, which reverts it to what it was.)
You say that your project is under /home/username, but the nginx configuration says /home/myapp. Fix that. I assume it's /home/username. The /home/username needs some permissions to be fixed:
chmod o+rx /home/username
See also (my article) http://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/.

Related

django+gunicorn+nginx gives 404 while serving static files

django+gunicorn+nginx gives 404 while serving static files
I am trying to deploy a Django project using nginx + gunicorn + postgresql. All the configuration is done, my admin panel project static file will serve , but other static files; it returns a 404 error.(iam use run python manage.py collectstatic)
my error.log nginx :: "/blogpy/static/home/test.css" failed (2: No such file or directory)"
Structure:
blogpy
-blogpy
-config
-nginx
-nginx.conf
-docker-compose.yml
-Dockerfile
-home
-static
-home
-test.css(not working)
- requirements
-static
-templates
-.env
-docker-compose.yml
-Dockerfile
setting.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
nginx configuration:
---- nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
upstream blogpy{
server blogpy:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
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;
proxy_pass http://blogpy;
}
location /static/ {
alias /blogpy/static/;
}
}
}
Try
In ---- nginx.conf:
location /static/ {
autoindex off;
alias /home/ubuntu/blogpy/static/; #add full path of static file directry
}
To get /blogpy/home/static/ files been copied into /blogpy/static/ by collectstatic command, you need to specify STATICFILES_DIRS setting
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
BASE_DIR / 'home' / 'static',
]
It is recommended to serve static files directly from nginx.
Add the following to your nginx site config.
location /static/ {
alias /path/to/static/directory/;
}

Django static files in live server using nginx

I have a live server running django, the address is http://179.188.3.54/ . As you can see, the app works but looks like the static files arent working. Also if I click in any other link, doesnt work.
This website is running without any problems in development version. Im not sure what I should do to fix this problem.
Here is my nginx config file and my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/cinegloria/cinegloria/cinegloria/static/'
PS: I tried to run collectstatic ;)
server {
root /usr/share/nginx/www;
index index.html index.htm;
access_log /var/log/nginx/domain-access.log;
server_name 0.0.0.0;
location / {
try_files $uri $uri/ /index.html;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://0.0.0.0:8000/;
}
}
Any ideas or sample code will be appreciated!
Add the static serving to the nginx conf before the / pattern:
location /static {
alias /cinegloria/cinegloria/cinegloria/static/;
}
location / {
...
}
Or set the STATIC_ROOT to the directory under the www root:
STATIC_ROOT = '/usr/share/nginx/www/static'
Or add the symlink from www root to you static dir:
$ ln -s /cinegloria/cinegloria/cinegloria/static /usr/share/nginx/www/static
Add another nginx directive for the static files. Static files should be served by nginx, not the Django server.
location /static/ {
alias /cinegloria/cinegloria/cinegloria/static/;
}
If that still doesn't work, you may need to add the mime type directive. I had to do that yesterday, because for some reason nginx wasn't serving the correct mime type when I used an alias.
As a helpful pointer, whenever you run into problems like this, take a look at your nginx error log and paste the last few lines for debugging. It is located at /var/log/nginx/error.log or a similar path.

Setup two websites using Django apps and Nginx, but after enter site1.com it keeps showing content of site2.com

After I type in my browser www.site2.com, it open page of www.site1.com.
I think my configuration for both nginx and gunicorn are fine.
etc/nginx/site-available/site1 - my default website www.site1.com
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
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 _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/site1/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/site1/static_root;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
etc/nginx/site-available/site2 - my new website www.site2.com
upstream app_server2 {
server 127.0.0.1:9500 fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name www.site2.com site2.com;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/test-django/site2/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/test-django/site2/static;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server2;
}
}
etc/init/site1.conf
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectedly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django
exec gunicorn \
--name=site1\
--pythonpath=site1 \
--bind=0.0.0.0:9000 \
--config /etc/gunicorn.d/gunicorn.py \
site1.wsgi:application
etc/init/site2.conf
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectedly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django/test-django/
exec gunicorn \
--name=site2\
--pythonpath=site2 \
--bind=127.0.0.1:9500 \
--config /etc/gunicorn.d/gunicorn.py \
site2.wsgi:application
I didn't forget about service nginx restart and service site2 restart, also after nginx -t I don't have any erros.
In your wsgi.py files of your projects, are you using the same variable for the django.conf.ENVIRONMENT_VARIABLE? Usually the line is: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_site.settings")
Try to change the line for those:
django.conf.ENVIRONMENT_VARIABLE = "DJANGO_SITEA_SETTINGS_MODULE"
os.environ.setdefault("DJANGO_SITEA_SETTINGS_MODULE", "project_site.settings")
Change SITEA for whatever you want for each project and also project_site.settings for the name of the folder where the settings file is. Restart the server after this changes.
I don't know if this is the answer but it may help because it fixed for me the same problem you describe.

How to call a flask application from a web browser?

I have a flask application that works on my PC.
When I deployed it on the virtual server (CentOs 6.5) I used nginx according to the article:
https://www.digitalocean.com/community/tutorials/how-to-deploy-flask-web-applications-using-uwsgi-behind-nginx-on-centos-6-4
I had to change the port in /etc/nginx/nginx.conf because it created conflict with apache port (caused nginx to fail to start because port is already in use). So my nginx.conf is:
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream uwsgicluster {
server 127.0.0.1:8081;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 81;
# Settings to by-pass for static files
location ^~ /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
root /app/static/;
}
# Serve a static file (ex. favico) outside static dir.
location = /favico.ico {
root /app/favico.ico;
}
# Proxying connections to application servers
location / {
include uwsgi_params;
uwsgi_pass uwsgicluster;
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;
}
}
}
My application is located under 12.12.15.16/cgi-bin/My_app (the ip address here is dummy).
I used the following command line to start the app:
env/bin/uwsgi --socket 127.0.0.1:8081 --protocol=http --wsgi-file main.py --callable app
My question is: How can I now call my app from a web browser?
Thank you for your help!
Your Flask app is listening on port 8081 but only for localhost connections (127.0.0.1). Nginx is listening on port 81 for connections and piping them to Flask on 8081. So from your own browser you want to access http://your-digital-ocean-ip:81/

can't view static files with nginx + gunicorn on osx

I am trying to setup a django production server on OSX (Mavericks).
This is my nginx server config file:
server {
listen 80;
server_name localhost;
error_log /pathtoerrorlog;
access_log /pathtoaccesslog;
# serve static files
location ~ ^/static/ {
root /Users/Hello/assets;
expires 30d;
}
# serve media files ~ ^
location ~ ^/media/ {
root /Users/Hello/assets;
expires 30d;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:8000;
}
}
This is the nginx.conf config file
user www-data;
worker_processes 4;
error_log /var/log/nginx/error.log;
events {
worker_connections 4092;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
gzip on;
gzip_disable "msie6";
include /etc/nginx/sites-enabled/*;
}
I have created a user called www-data on the system and gave the folllowing access permissions:
sudo chown -R www-data:www-data /usr/local/etc/nginx
sudo chown -R www-data:www-data /etc/nginx
sudo chown -R www-data:www-data /var/log/nginx
I started gunicorn without any errors and so is the case with nginx too.
In a browser, localhost redirects me to the django app but static media is not displayed. This is a sample error (out of all for static content) I get, as I see in nginx error log:
2014/01/25 20:16:23 [error] 35068#0: *68 open() "/Users/Hello/assets/static/css/base.css" failed (13: Permission denied),
client: 127.0.0.1, server: localhost, request: "GET /static/css/base.css HTTP/1.1", host: "localhost", referrer: "http://localhost/"
I tried changing the permissions of /Users/Hello/assets using sudo chown -R www-data:www-data assets but it didn't help. -R 777 also didn't work.
Please suggest where I am going wrong. Thanks!
It turns out to be that read permissions have to be granted for all parent directories of /Users/Hello/assets. So, I set read access to all of Users, Hello, assets and not just assets as earlier.
Related articles are :
Nginx 403 forbidden for all files
http://nginxlibrary.com/403-forbidden-error/