Whenever I load my site it gives me a 404 error while loading the images and css.
My nginx config:
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/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static/ {
alias /home/django/django_project/csite/static/csite/;
autoindex on;
}
# Proxy the static assests for the Django Admin panel
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;
}
}
My settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, "csite/static")
STATIC_URL = '/static/'
You can view the directory structure here
My app name is csite.
Try this one
location /static/ {
root /home/django/django_project/csite/;
}
This is an answer according to my case, but you should find an answer to your issue. In my case, I have :
My Django IP server : 172.30.10.92
My Nginx IP server : 172.30.10.93
1- Install and Configure wsgi (located on Django server)
WSGI is a file created with your Django project.
The file is located in /path/to/your/project/Myproject/wsgi.py
We have to edit this file like this :
import os
from django.core.wsgi import get_wsgi_application
import sys sys.path.append('/var/www/html/Myproject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Myproject.settings")
application = get_wsgi_application()
2- Install and Configure gunicorn/supervisor (located on Django server)
In order to install gunicorn/supervisor, you have to execute in your shell :
pip install gunicorn
pip install supervisor
Then, you have to create a new file in /etc/supervisor/conf.d/Myproject.conf which looks like this :
[program:Myproject]
command = /home/valentin/.virtualenvs/MyprojectEnv/bin/gunicorn Myproject.wsgi:application --name "Myproject" --workers=4 --bind=0.0.0.0:8080 -- user="valentin" --group="valentin" ; Command to start app
user = username #You have to replace by your username
stdout_logfile = /var/log/supervisor/supervisor.log
redirect_stderr = true
log
environment=LANG=fr_FR.UTF-8,LC_ALL=fr_FR.UTF-8
I specified port 8080 which is the communication port between my application server and my web server.
3- Edit hosts file on nginx server (located on nginx server)
You have to edit your hosts file located to /etc/hosts and add a new entry to your Django server :
127.0.0.1 localhost
127.0.1.1 valentin
172.30.10.92 Myproject
# The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
4- New config file in nginx repository (located on nginx server)
This new file should be placed in /etc/nginx/sites-available/Myproject.conf
server {
listen 8080;
server_name Myproject;
root /var/www/html/Myproject/;
location /static/ {
root /var/www/html/;
}
location / {
include proxy_params;
proxy_pass http://172.30.10.92:8080;
}
}
The IP address corresponds to my Django server address. I specified the listen port (8080), the path to my Django project & static directory.
Then, you have to create a symbolic link to sites-enabled.
After ths operation, restart nginx service :
sudo service nginx restart
5- Allow nginx IP address in Django (located on Django server)
You have to edit your settings.py file in order to allow nginx IP address in ALLOWED_HOSTS :
ALLOWED_HOSTS = ['localhost', '172.30.10.93', '127.0.0.1', '[::1]']
6- Finally execute gunicorn (located on Django server)
Finally, you have to start gunicorn. You should be inside your Django root project and execute :
gunicorn Myproject.wsgi:application --bind 172.30.10.92:8080
Now, in your browser, try to connect to your nginx server with the port :
http://172.30.10.93:8080
It works !
EDIT
Your static directory haven't be placed in the same directory than your django project.
Create a new static directory, specify the path in your settings.py file and then, make python manage.py collectstatic.
Don't forget to edit your nginx files.
Related
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
I am trying to do the following.
I have 3 django projects (NOT apps) (can be more).
Proj1: On port 8000, Proj2: 8001, and Proj3:8002
This is what I am trying to achieve:
User visits : localhost:8000
All urls under Pr1: Respond
User visits: localhost:8000/Pr2
All urls under Pr2: Respond
User visits: localhost:8000/Pr3
All urls under Pr3: Respond
Goal: Proj 1 is run with Docker and Nginx. Now I have 2 new projects to integrate. Every new sub project should be reached using only one main port (8000). But internally the request is directed towards the correct port of sub-project.
Why this goal: Achieve micro-service style structure. Sub projects (2 & 3) can be reached only after authentication through Proj1.
What have I tried: Honestly no clear idea where to start.
Added new projects to docker-compose...they all work when visited by their own port(localhost:8001/Prj2) ..
With nginx, tried adding a new server. No effect:
server {
listen 8001;
location / {
proxy_pass http://web_socket:8080;
proxy_redirect off;
}
}
If you have "nginx" installed, so you can just create two files (site1.com.conf, site2.com.conf) in the following directory: "/etc/nginx/sites-enabled/"
Then add inside this files codes below:
site1.com.conf
# the upstream component nginx needs to connect to
upstream site1 {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:49151; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name site1.com; # 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 /var/www/site1.com/src/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/site1.com/src/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass site1;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
site2.com.conf
# the upstream component nginx needs to connect to
upstream site2 {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:49152; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name site2.com; # 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 /var/www/site2.com/src/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/site2.com/src/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass site2;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
Then edit "/etc/hosts" and add there two lines:
127.0.0.1 site1.com
127.0.0.1 site2.com
After saving files, restart uwsgi (install if it is not installed) and **nginx**
**service uwsgi restart**
**sudo /etc/init.d/nginx restart**
**Note: all your django applications should be in the folder of "/var/www/site1.com/src" and "/var/www/site2.com/src"**
I am using WSL to run nginx to serve django app on "d" partition, yesterday it worked just fine, today stopped working correctly.
this is my conf file:
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server xxx.xxx.x.xxx:8000;
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name xxx.xxx.x.xxx; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django static
location /static {
alias /mnt/d/backend/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 /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
server and domain name are the same as allowed host in settings.
STATIC_ROOT = os.path.join(BASE_DIR,"static")]
I am not sure if I am referring to the right path for the static files.
as they are in different location, I am little confused about using virtual environment, wsl at the same time.
(venv) root#DESKTOP-XXXXXX:/mnt/d/backend
the problem is that the server doesn't load the static files.
I am trying to run my simple django project on uwsgi and nginx, I use ubuntu 14.04 server.
I have followed the uWSGI doc,since here it works ok,but when it comes to this part it won't work for me ,I do not know why?here is the mysite_nginx.conf:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
#server unix:///home/ubuntu/d18/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 123.123.123.123; # this just my example IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/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/ubuntu/d18/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
I did not do the "python manage.py collectstatic", because I thought I just wanna see the sample mysite run on the nginx and uwsgi, however it just not work. Can someone tell me why?
I'm very new to NGINX, or setting up a server in general. I'm going through the tutorial http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html with a website I'm trying to set up. I'm getting to the point in the tutorial where you use sockets, however, I can't get the socket to work. nginx.conf looks like this.
# the upstream component nginx needs to connect to
upstream django {
server unix:///home/althor/projects/Freebooks/freebooks.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 10.0.0.130; # 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/althor/projects/FreeBooks/media; # your Django project's media files - amend as required
}
location /static {
alias /home/althor/projects/FreeBooks/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/althor/projects/FreeBooks/uwsgi_params; # the uwsgi_params file you installed
}
}
The command i'm doing is this:
uwsgi --socket freebooks.sock --module FreeBooks.wsgi --chmod-socket=664
My File structure looks like this
-home
-projects
-FreeBooks
freebooks_nginx.conf
freebooks.sock=
-FreeBooks
wsgi
I'm not getting any type of error. When I go to 10.0.0.130, all I get is the welcome to nginx thing, same for 0.0.0.0, 127.0.0.1. Except when I got to port 8000 where I get a bad nginx gateway. Any help would be very much appreciated.
remove your "upstream django" line and replace your uwsgi_pass line with
uwsgi_pass unix:///home/althor/projects/Freebooks/freebooks.sock;
so that your file looks like
server {
listen 8000;
server_name 10.0.0.130; # substitute your machine's IP address or FQDN
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /media {
alias /home/althor/projects/FreeBooks/media;
}
location /static {
alias /home/althor/projects/FreeBooks/static;
}
location / {
include /home/althor/projects/FreeBooks/uwsgi_params;
uwsgi_pass unix:///home/althor/projects/Freebooks/freebooks.sock;
}
}
Also make sure the socket file actually gets created when you run uwsgi command and that it is readable by the user running nginx (the properties are right but does the user have access to the full path?)
It turned out be a permissions issue. NGINX didn't have permission to write to the socket.