Running multiple django projects using docker and nginx - django

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

Related

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)?

Trouble getting nginx to serve 'media' files

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.

How can I use my godaddy domain for my Django App

This situation is some kind of hard to describe. I use nginx and uwsgi to deploy my Django blog on my VPS, it was function well, but I can only use my IP to get access to the app, how can I use my domain without www to setup my app? I set the server_name breakwire.me and set a A record HOST is '#', point to my VPS's IP, but if I visit the breakwire.me, I can only see
"Welcome to nginx"
Here is my nginx conf
# my_blog__nginx.conf
# configuration of the server
server {
the port your site will be served on
listen 106.186.29.228:8000;
# the domain name it will serve for
server_name breakwire.me; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# logs
access_log /home/chen/DjangoProjects/my_blog/logs/access.log;
error_log /home/chen/DjangoProjects/my_blog/logs/error.log;
# Django media
location /media {
alias /home/chen/DjangoProjects/my_blog/media; # your Django project's media files - amend as required
}
location /static {
alias /home/chen/DjangoProjects/my_blog/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 106.186.29.228:8001;
include /home/chen/DjangoProjects/my_blog/uwsgi_params; # the uwsgi_params file you installed
}
}
Change the
listen 106.186.29.228:8000
to
listen 106.186.29.228
or: 106.186.29.228:80
The browsers are always connecting on port 80. Your virtual server in nginx is listening on port 8000, so if you try to open your website with http://breakwire.me:8000/
it's working because you've set the port to 8000 in the listen directive.

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.