getting nginx to serve static files (django, gunicorn) - django

When DEBUG = True, Django serves the static files and I can see my site, and the admin page, styled correctly and run the correct js. My intuition is I have misconfigured nginx, and I'm struggling to find the cause. Some background:
sudo nginx -t && sudo systemctl restart nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
cat sites-enabled/mysite
server {
listen 80;
server_name IP_ADDR;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /home/ubuntu/mysite;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
which is symlinked to sites-available/mysite
/var/log/nginx/error.log only has the line 2020/03/01 19:57:49 [notice] 20644#20644: signal process started, but the access log has a number of 404s. Upon trying to access the application URL, chrome dev console shows:
Refused to apply style from 'IP_ADDR:8000/static/css/console.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
IP_ADDR/:9 GET IP_ADDR:8000/static/js/jquery.min.js net::ERR_ABORTED 404 (Not Found)
IP_ADDR/:11 GET IP_ADDR:8000/static/js/console.js net::ERR_ABORTED 404 (Not Found)
IP_ADDR/:10 GET IP_ADDR:8000/static/js/content.js net::ERR_ABORTED 404 (Not Found)
IP_ADDR/:11 GET IP_ADDR:8000/static/js/console.js net::ERR_ABORTED 404 (Not Found)
favicon.ico:1 GET IP_ADDR:8000/static/favicon.ico 404 (Not Found)
I have include /etc/nginx/mime.types; in my nginx.conf, and mime.types includes the line
text/html html htm shtml;,
though I think that's correct since my css file should be text/css, but I don't know the best way to get over that hurdle. I've run python manage.py collectstatic and my project structure is this:
(venv) ubuntu#IP_ADDR:~/mysite$ tree -L 1
.
├── README.md
├── manage.py
├── mysite
├── requirements.txt
├── static
├── venv
└── mysite_views_urls_templates
I'm not sure if I've even given enough information, I'm really trying to deepen my understanding of the whole stack.

The answer was that I was running gunicorn manually over port 8000, blocking nginx from delivering the static files.

Related

Nginx server not using project config

I am trying to create a configuration for the Nginx web server for my Django project. I would like to store the configuration of the web server in my project files.
As I am using MacOS, the /sites-enabled directory was not present in the /usr/local/etc/nginx directory, so I've created it. In the /usr/local/etc/nginx/sites-enabled directory I have made a symbolic link to the project which seems to work.
In the nginx.config file in the /usr/local/etc/nginx directory I have updated the file to make it see the sites-enabled directory:
http {
include /usr/local/etc/nginx/sites-enabled/*;
The nginx -t command lists the following if I input incorrect data in my project nginx.conf file:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
and correctly recognizes changes to it if I make some intentional errors:
nginx: [emerg] invalid parameter "server_name" in /usr/local/etc/nginx/sites-enabled/linkem-all:6
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
The problem is that despite my changing the port to listen to in the nginx.conf file located in my project:
server {
# port nginx will be listening to
listen 8000;
# server ip address
server_name 127.0.0.1.;
# logs stored within the project
access_log path/to/nginx-acces.log;
error_log /path/to/nginx-error.log;
# pass traffic from nginx to the gunicorn server
location / {
proxy_pass http://127.0.0.1:5432;
}
# static files location served with alias for explicit path
location /static {
alias ./static;
}
}
after running the nginx server I get the error caused by nginx trying to bind to port 8080, its default setting.

Digital Ocean, Django and Nginx not finding all static files

I have tried a lot of different things but all of the solutions I found are not helping.
I putting my corporate site on a digitalocean site on ubuntu 16.04 by following the digitalocean directions (which have worked well before) but it is only serving some of the static files.
Here are the links to the images.
<h3>Here is the image that doesn't load</h3>
<img src="http://206.189.161.104/static/images/frac_stack_1.jpg" alt="Image that doesn't load">
<h3>Here is the image that does load in the same folder</h3>
<img src="http://206.189.161.104/static/images/coil_pic.jpg" alt="Image that doesn't load" style="width:200px;height:200px;>
Here is my nginx config:
server {
listen 80;
server_name 206.189.161.104;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /home/dmckim/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/dmckim/myproject/myproject.sock;
}
}
I tried removing the trailing slash off the static (as shown above). I also tried changing root to alias and adding the static folder to the path but I had the same results.
Here is the code from my settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/home/dmckim/myproject/static/',
'/home/dmckim/myproject/static/images/',
)
I also tried clearing collectstatic before collectingstatic again and I always run these commands after and make sure my browser cache is cleared.
sudo systemctl restart gunicorn
sudo nginx -t && sudo systemctl restart nginx
My permission on the files are -rw-rw-r--, for the image that does load and the one that doesn't load. I also tried a lot of ways to change permissions (I don't really understand them but they were suggested in other posts). I even destoyed the server and started from scratch to make sure I didn't mess anything up with the permissions.
I don't see anything wrong with the nginx process logs or the access logs but the error logs show the following:
2018/05/31 13:04:19 [error] 11481#11481: *22 open()
"/home/dmckim/myproject/static/images/frac_stack_1.jpg" failed (2: No such
file or directory), client: 12.184.4.50, server: 206.189.161.104, request:
"GET /static/images/frac_stack_1.jpg HTTP/1.1", host: "206.189.161.104",
referrer: "http://206.189.161.104/frac-stacks/"
The gunicorn logs show a 404 for the images that won't load.
Here is the www-data group uid=33(www-data) gid=33(www-data) groups=33(www-data)
Here is my group uid=1000(dmckim) gid=1000(dmckim) groups=1000(dmckim),27(sudo)
The file names are case sensitive. Your image is named "http://206.189.161.104/static/images/frac_stack_1.JPG" not "http://206.189.161.104/static/images/frac_stack_1.jpg".
<h3>Here is the image that loads</h3>
<img src="http://206.189.161.104/static/images/frac_stack_1.JPG" alt="Image that doesn't load" style="width:200px;height:200px;">
<h3>Here is the other image that does load in the same folder</h3>
<img src="http://206.189.161.104/static/images/coil_pic.jpg" alt="Image that doesn't load" style="width:200px;height:200px;>
Note that your results may differ when running this locally. Windows isn't case sensitive, Linux is. See this question for details

my nginx cannot load uwsgi on Ubuntu 16.04

trying to run django app "mysite" through uwsgi with nginx on Ubuntu 16.04, but when I start uwsgi and check in my browser, it just hangs.
i set django upstream socket to on port 8002 and nginx to listen on 8003. In the browser i visit 192.168.0.17:8003 prior to running uwsgi and it throws 502 which is expected, so I start uwsgi with
uwsgi --http :8002 --module mysite.wsgi --logto /tmp/uwsgi.log --master
and 8003 now hangs when I reload in the browser. I looked through /var/log/nginx/error.log but it's blank (so is access.log).
Here is nginx config, which is symlinked to /etc/nginx/sites-enabled:
sudo nano /etc/nginx/sites-available/mysite_nginx.conf
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8002; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8003;
# the domain name it will serve for
server_name 192.168.0.17; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/myusername/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/myusername/uwsgi-tutorial/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/myusername/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
I know that Django is running because in my app's settings.py I have ALLOWED_HOSTS = ['192.168.0.17','localhost','127.0.0.1'] and when I visit port 8002 in the browser I get the django "Congratulations!" page. And when I remove 192.168.0.17 from ALLOWED_HOSTS, django still runs on that machine from localhost or 127.0.0.1, so this seems that it must be something to do with how ngnix and uwsgi are talking to each other.
Any ideas??
It turns out systemd does not like lines in config files to be too long. I removed a couple long comments in /etc/systemd/system/uwsgi.service, restarted uwgsi service and all is well.
I found this out by running sudo journalctl -u uwsgi and finding the following error:
[/etc/systemd/system/uwsgi.service:5] Unbalanced quoting, ignoring: "/bin/bash -c 'mkdir -p /run/uwsgi; chown myusername:myusern
In researching Unbalanced quoting, found this git issue regarding maximum file line length.

404 not found error on Nginx

I'm very new to AWS and Nginx.
When I get an access to my elastic ip address for web server EC2 instance, I get a 404 not found error.
How come it shows 404 not found error although I have uploaded files to the directory I set as root directory in nginx.conf file?
After I edited the nginx.conf file, I did reload nginx as well.
On EC2
$ ls sites/
LICENSE README.md css fonts index.html js
nginx.conf
events {}
http {
server {
listen 80;
server_name MY_IP_ADDRESS_HERE;
root /sites/;
}
}
Additional Information
I get a default nginx page when I set the root directory to "/usr/share/nginx/html/" in nginx.conf file, so the server is working ok.

Why is static content request going to uwsgi?

I'm setting up my Django project to work with uwsgi and nginx. For static content, I have the following in my nginx.conf:
location /static {
alias /Users/me/mystatic; # your Django project's static files - amend as required
}
I have set STATIC_ROOT to /Users/me/mystatic and called collectstatic to copy all the static files into that directory. In my uwsgi log, I see GET requests for the static content. Since nginx is supposed to serve the static content, why is the GET request sent to uwsgi?
Make sure that STATIC_URL is set as "/static/".
The nginx conf should look something like this:
server {
listen 80 ;
server_name XXXX;
client_max_body_size 4G;
location /static/ {
alias <path-to-collectstatic>
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_web.sock;
}
}
Notice the appending / in location block for static
Last thing, make sure that application server is running with DEBUG as False
The issue was that I had placed my own nginx.conf in /usr/local/etc/nginx/sites-enabled, not /usr/local/etc/nginx/servers. In /usr/local/etc/nginx/nginx.conf, include servers/*; is at the end. Therefore, UWSGI was still serving the static files. I followed the django-nginx-uwsgi tutorial, and I assumed I had to create a new sites-enabled directory. I am using OS X.
I see on Linux, though, /etc/nginx/nginx.conf has include include /etc/nginx/sites-enabled/*; at the end, so the steps in the tutorial are applicable on Linux.
The extra / at the end of /static doesn't make a difference, but it doesn't hurt to have it at the end of all paths.