django nginx configuration issue - django

I am encountering an issue with my nginx django setting.
Well, when I load the project using their development server everything is ok.
When I load it from nginx, it looks it's not really loading good project.
I made a test, I edited the settings.py file leaving a syntax error and well it didn't load on the internal server of django, but, it loaded with nginx. So I guess nginx isn't linked to the right config but I can't get why as I only created one project.
Config file is the following (nginx):
server {
listen 80;
server_name sub.example.com;
access_log /var/www/sub.example.com/log/access.log;
error_log /var/www/sub.example.com/log/error.log;
## Default location
location / {
include fastcgi_params;
fastcgi_pass 127.0.0.1:8888;
fastcgi_split_path_info ^()(.*)$;
}
location /static/ { # STATIC_URL
alias /var/www/sub.example.com/static/; # STATIC_ROOT
expires 30d;
}
location /media/ { # MEDIA_URL
alias /var/www/sub.example.com/media/; # MEDIA_ROOT
expires 30d;
}
}

Related

Static files not loading for deployed django rest framework

I have built and successfully deployed a django rest framework using gunicorn and nginx on Ubuntu 18.04. However, the static files are not being pulled up.
Django web app without loaded static files
Here is my nginx configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name [my_server_domain_or_IP];
#root /var/www/html;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
root /home/ubuntu/myprojectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
And in my settings.py file:
STATIC_URL = '/static/'
import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I have checked that DEBUG is set to False.
I have also already run collectstatic and the static files are located in a folder called static in: /home/ubuntu/myprojectdir/static.
Every change I tried to make in the nginx configuration, I restarted nginx with the following command: sudo systemctl restart nginx.
I mainly followed this tutorial, the only difference is that I edited the default nginx configuration instead of creating a new one because it wasn't deploying my django application this way.
I can't seem to figure out what's wrong and have been trying to solve it for days. Am I missing something here?
You don't need equals sign here:
location = /static/ {
root /home/ubuntu/myprojectdir;
}
Instead try this:
location /static/ {
root /home/ubuntu/myprojectdir;
}
Try to giving permissions to your home folder
chmod a+x /home/ubuntu -R

Nginx "upstream" directive is not allowed here

I'm trying to set up a Django website with uwsgi and nginx. I created configuration file mysite.conf:
# the upstream component nginx needs to connect to
upstream django {
server unix:///home/path/to/mysite/mysite.sock;
}
# configuration of the server
server {
listen 80;
server_name mydomain.com www.mydomain.com;
charset utf-8;
# max upload size
client_max_body_size 350M;
# Django media and static files
location /media {
alias /home/path/to/mysite/media;
}
location /static {
alias /home/path/to/mysite/static;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/path/to/mysite/uwsgi_params;
}
}
And it gives this error:
nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/sites-enabled/mysite.conf:2
I don't why it happens. I followed this tutorial: https://tonyteaches.tech/django-nginx-uwsgi-tutorial/
The problem was that i included configuration files before http tag.
It should be:
http {
include /etc/nginx/sites-enabled/*;
...
}
This can also happen if your file is being deployed over the directory /etc/nginx/conf.d/ but the default config is including files from http.d instead:
include /etc/nginx/http.d/*.conf;
so try also to deploy it over /etc/nginx/http.d/

Unable to load staticfiles after deploying django app on aws-ec2

I am a beginner in aws and I am trying to deploy a django app on aws-ec2. I have set-up gunicorn application server and nginx web server and app loads but without static files. I have followed many answers on stackoverflow but I am unable to fix my problem.
I tried with both root and alias but they didn't work. The project structure is as follows:-
/home/ubuntu/myskillhut/
django.conf(nginx configuration file)
server {
...
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myskillhut/app.sock;
}
location /static/ {
autoindex on;
alias /home/ubuntu/myskillhut/static/;
}
}
settings.py
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"), )
...
Configuration below works for my project
location ~* \.(jpe?g|gif|png)$ {
root /home/ubuntu/myskillhut/static/;
expires 1M;
}
location ~* \.(css|js)$ {
root /home/ubuntu/myskillhut/static/;
expires 1d;
}
Also, make sure nginx can access those files in that dir (../static/)

django + Nginx + Gunicorn - 404 for Static file when debug is false

I dont know how nginx serves the static files
/etc/nginx/site-available/default :
upstream example.com {
server unix:/home/www/example/env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
access_log /home/www/example/logs/nginx-access.log;
error_log //home/www/example/logs/nginx-error.log;
location /static/ {
alias /home/www/example/codeab/static/;
}
location /uploads/ {
alias /home/www/example/codeab/uploads/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/www/example/env/run/gunicorn.sock;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/www/example/templates/;
}
error_page 404 /401.html;
location = /401.html {
root /home/www/example/codeab/templates/;
internal;
}
}
Django setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_content/'),
)
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
Also , I did collectstatic before DEBUG = False
It seems that nginx is not considering the above config file, as for 404 page it is not showing my custom 401.html page.
After editing the config, I linked it to site-enabled by
sudo ln -s /etc/site-available/default /etc/site-enabled/default
service nginx reload
sudo service nginx restart
Please help, or let me know If I am missing any things.
I dont know the exact solution, but it started working on changing my gunicorn binding from xxx.xxx.xx.xx:80 to xxx.xx.xx.xx:8000
and editing config as -
location / {
include proxy_params;
#proxy_pass http://unix:/home/www/example/env/run/gunicorn.sock;
proxy_pass http://xxx.xxx.xx.xx:8000;
}
I commented proxy_pass of sock file and added the new line as you can see above.
Nginx serves static files from the dir defined in nginx config file under location /static/ and redirects to custom 404 html page as defined in nginx config by you when it does not find such static resource in the defined dir.
Django displays the custom 404 html page when it does not find given url in any of the urls.py files of Django, provided you set Debug = False in settings.py and also point the 'TEMPLATES'-'DIRS' setting to the dir containing your custom 404 html file as explained in the Django documentation.
So, effectively the way nginx and Django displays the same 404 html page is different and independent of each other.
Just change your the configuration in the nginx server block from
location /static/ {
alias /home/www/example/codeab/static/;
}
To
location /static/ {
alias /home/www/example/codeab/static_content/;
}
Reason, after the command python manage.py collectstatic has executed
the static files are been placed inside of the static_content folder.

How to serve django media files via nginx ?

I'm new at Nginx, I've successfully bound my django project to Nginx. However I can't serve my static files and I guess I set my media folder's location wrongly. Here is my file tree:
root_directory
my_django_project
...
manage.py
app1
app2
media
admin
css
js
...
And my nginx.conf goes like :
server {
listen 192.168.1.9:80;
server_name localhost;
# site_media - folder in uri for static files
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
access_log off; # po co mi logi obrazków :)
expires 30d;
}
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8080;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log;
}
}
When I open my admin page, all css pages give 404 error. Can you tell me that how can I set my media path correctly ?
Here's a example of how I have my nginx servers setup
server {
server_name example.com www.example.com;
location /static {
autoindex on;
alias /home/myusername/myproject/static/;
}
location /media {
autoindex on;
alias /home/myusername/myproject/media/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I serve django with Gunicorn on localhost port 8000. (that's what the proxy_pass is for)
The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use 'root' instead of 'alias' but they are similar.
This ServerFault question may help.
You have set this:
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
This means that the location will be xxx/media/media. Nginx will go to [...]/portal/media and look for a folder called media.
It should rather be like this:
location /media/ {
root /home/nazmi/workspace/portal;
}
Also you should remove the trailing slash.
If the media file isn't serving, try to set media location as below in the conf file inside the sites-enabled directory. This works for me.
location /media {
root /home/username/projectname/;
The following code works for me:
server {
server_name example.com www.example.com;
location /static {
autoindex on;
**alias /home/myusername/myproject/;**
}
location /media {
autoindex on;
**alias /home/myusername/myproject/;**
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I bold the different parts according to the previous answer.
location /media {
alias /home/user/django_app/media; #(locaion of your media folder)
}
Try this code in nginx config to serve static and media files for a django applications (without autoindex on settings)