Is it necessary to include sites-enabled directory within nginx.conf? - django

I am following Setting up Django and your web server with uWSGI and nginx tutorial. I was stuck on part Configure nginx for your site. I tried everything that I have found searching the internet, mainly I was trying to fix my config mysite_nginx.conf file, but it seems now that it was correct all the time.
This is my config file mysite_nginx.conf which of course is symlinked from /usr/local/etc/nginx/sites-enabled/.
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8001;
}
# configuration of the server
server {
listen 8000;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /Users/username/dev/my_site/mysite/media;
}
location /static {
alias /Users/username/dev/my_site/mysite/static;
}
location / {
uwsgi_pass django;
include /Users/username/dev/my_site/mysite/uwsgi_params;
}
}
The problem I faced
Visiting localhost:8000/media/img.jpg or localhost:8000/static/img.jpg always was returning 404 Not Found. In nginx logs all of the requests were mapped to /usr/local/Cellar/nginx/version/html/ which is a symlink to the /usr/local/var/www/ where index.html and 5xx.html files are present.
My static/ and media/ were added to the /usr/local/Cellar/nginx/version/html/ so in nginx error logs I saw requests to /usr/local/Cellar/nginx/version/html/static/img.jpg.
The solution that worked for me
In nginx.conf file I have included path to the sites-enabled. After this all of my requests where mapped to the absolute paths that I have added as an alias of the location and everything worked as expected.
...
http {
include mime.types;
include /usr/local/etc/nginx/sites-enabled/*.conf;
default_type application/octet-stream;
...
The Question
In the tutorial that I mentioned there was no mention about this, so I suppose it should work without editing nginx.conf file? But it didn't worked for me, am I missing something?

First of all there is no restriction or necessity to follow one approach. You can have all your config in a single nginx.conf or you can split it into multiple files and include them, or you can include specific patterns from a directory.
Different OS distribution may use different config type of default config setup. The common one being usage of a nginx.conf using include site-enabled/*.conf; and include conf-enabled/*.conf;
The logic is that your create your actual config in sites-available and conf-available directory and you just symlink them in the sites-enabled and conf-enabled directory respectively. So tomorrow if you want to disable a config then instead of renaming or deleting it you just delete the symlink from xxxx-enabled directory. This is just a convention so it easier for you to manage virtual hosts related to different sites in their own files.
By default these have some default configs which are for the demo nginx page. Before setting up your config you should disable this, so that it doesn't mess up with your config.
Now sometimes the include may not be part of the default nginx.conf. Because you installed nginx using brew the config used in that installation is different. If you check the file it has below include
include servers/*;
So the expectation is to put it at different place. So you have to be aware that the base config is nginx.conf (that also can be changed by compiling nginx from source). It can then include other configs or choose not to and just have a default server in nginx.conf itself. Before following any tutorial do read your nginx.conf file first.

Related

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.

Django project deployment: cannot load static files

I'm deploying a Django project using:
virtualenv
nginx
gunicorn
following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps
My configuration
django settings
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATIC_URL = '/static/'
/etc/nginx/sites-available/esmart2
GNU nano 2.0.9 File: /etc/nginx/sites-available/esmart2
server {
server_name 192.168.30.17;
access_log off;
location /static/ {
alias /new_esmart/esmart2/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
Running
(esmart_env) [root#eprssrv09 esmart2]# /new_esmart/esmart_env/bin/gunicorn --bind 192.168.30.17:8001 esmart2.wsgi:application
My Django project is running but:
Not Found: /static/admin/css/login.css
Any advice?
UPDATE 1
I think that nginx settings are been avoided:
deleting any reference in 'sites-enabled' folder, django project runs and the problem concerning static files remain.
UPDATE 2
if in Django settings debug=True I get
Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/login.css
Not Found: /static/admin/css/base.css
It seems I solved BUT I don't think is the better solution. I mean, it's a good solution depending on the project requirements: mine allows using this solution.
install package
pip install whitenoise
on wsgi.py I added:
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
This way nginx site conf is ignored (but I think they had already ignored!)
Django WhiteNoise documentation
With a couple of lines of config WhiteNoise allows your web app to
serve its own static files, making it a self-contained unit that can
be deployed anywhere without relying on nginx, Amazon S3 or any other
external service. (Especially useful on Heroku, OpenShift and other
PaaS providers.)
It’s designed to work nicely with a CDN for high-traffic sites so you
don’t have to sacrifice performance to benefit from simplicity.
WhiteNoise works with any WSGI-compatible app but has some special
auto-configuration features for Django.
WhiteNoise takes care of best-practices for you, for instance:
Serving compressed content (gzip and Brotli formats, handling
Accept-Encoding and Vary headers correctly) Setting far-future cache
headers on content which won’t change Worried that serving static
files with Python is horribly inefficient? Still think you should be
using Amazon S3? Have a look at the Infrequently Asked Questions.
It looks to me like the issue is two-fold:
It looks like you may have an error in your nginx config. Is new_esmart/ really in your root directory/ or is it somewhere like /home/sam/new_esmart/? If it is not in your root directory, make sure to provide the complete path.
It looks like your STATIC_ROOT will point to path/to/new_esmart/static/ rather than /new_esmart/esmart2/static/. Keep in mind that STATIC_ROOT is the place where your static files will be copied to when you run collectstatic not the place you personally place static files when creating them initially.
So what you probably need to do is confirm that you really want your static files collected to /actual/path/to/new_esmart/static/ and then use that same path in your nginx config.
Also, as discussed in the comments, you could try adding listen 8001; to your nginx config:
server {
listen 8001;
server_name 192.168.30.17;
access_log off;
...

nginx serving passenger on subdirectory

I got some rails 4 apps and I need them to be in /subdirectory of my domain, I am doing this with location directive, the problem is that they don't know that they are in that specific directory and all /link are going to mydomain/link instead of mydomain/app-a/link
this is the configuration that I use for one of the apps, I got it from official passenger docs
location ~ ^/events(/.*|$) {
alias /home/acm/acm-events/public$1;
passenger_base_uri /events;
passenger_app_root /home/acm/acm-events;
passenger_document_root /home/acm/acm-events/public;
passenger_enabled on;
}

Django to serve static files with exceptions

In my production environment I have Nginx serving static files for my Django application, but while developing I let Django so the work.
I need to have Nginx serving all static files but those in a certain subdirectory. So, Django side, I need to intercept that directory and treat it differently.
How can I make Django dev server intercept all the calls to /static/* but not those to /static/myspecialfiles/*´ and hence write a url route to handle the GET calls to said/static/myspecialfiles/*´?
You need to tackle this in two areas, Django and nginx.
For Django, since running a local setup (when DEBUG=True) already serves static content from STATIC_ROOT automatically, just add a URL conf for your special files:
# in urls.py
url(r'^/static/specialfiles/', SpecialView.as_view(), name='special'),
Then, in your nginx conf, make sure you ignore that path so it actually reaches Django:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8000;
}
location /static/specialfiles/ {
proxy_pass http://localhost:8000;
}
location /static/ {
alias /home/ubuntu/dev/example/static/;
}
}
Since the locations are parsed sequentially, your special files will hit the django block before the generic static block.
But really you'd be best to just move your special files somewhere else.

Root directive in nginx for django/uwsgi

Let's say I have a django project in /home/django/project0/ and my setup is:
server {
...
root /usr/share/nginx/www/project0;
...
location / {
uwsgi_pass unix:///tmp/uwsgi/uwsgi_project0.sock;
include uwsgi_params;
...
}
location /static/ {
root /usr/share/nginx/www/project0;
...
}
# similar settings for robots.txt and favicon.ico
}
Since the static files will be served by different folders and django will be served through uwsgi isn't the root directive useless?
What happens if I do not set a value to root?
The main reason I am asking is because in many articles online, I've seen people set the root directory to /home/django/project0, which as far as I understand makes the project's settings.py file public (a practice not recommended by the django community). Isn't this approach wrong security-wise or am I missing something here?