Trouble getting nginx to serve 'media' files - django

I've been working through a turotial and got stuck at this specific part and can't seem to figure it out. This is exactly where I am stuck.
Here is my 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:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
root / ;
listen 8000;
# the domain name it will serve for
server_name .81.*.*.*; # 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/ {
root /home/mysite/mysite1/blog/media; # your Django project's static files - amend as required
}
# Django static
location /static/ {
#alias /home/mysite/mysite1/blog/templates; # your Django project's media files - amend as required
root /home/mysite/mysite1/blog/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/mysite/mysite1/uwsgi_params; # the uwsgi_params file you installed
}
}
Here is the file structure of mysite on pastebin - Sorry for it being a total mess. I am aware of redundant static files. If you need any other files, I am happy to provide. Thank you.

Related

Running multiple django projects using docker and nginx

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"**

nginx stopped serving static folder for django app by using WSL

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.

uwsgi nginx django ubuntu 14.04 virtualenv

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?

Django nginx returning 502 Bad Gateway status code

Edit: I re-followed the instructions and now this is the issue I am having: Django Nginx not serving media files
I'm following this tutorial:
http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
Everything was going well until I reached the "Basic nginx test" section. When I stopped and started nginx and then added "media.png" to my media folder and went to 192.168.** *.** *:8000/media/media.png it gave me a 403 Forbidden status code.
When I visit 192.168.** *.** *:8000 it gives a 502 Bad Gateway status code.
this is 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: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 192.168.***.***; # 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/a/Documents/media; # your Django project's media files - amend as required
}
location /static {
alias /home/a/Documents/CMS/CMSApp/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/a/Documents/CMS/CMS/uwsgi_params; # the uwsgi_params file you installed
}
}
Any idea why nginx is returning those status code's and how I can fix the issue (how I can get it to show me the "media.png" image)?

NGINX and UWSGI not communicating

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.