django+nginx+gunicorn - subdomain joy - django

i'm trying to setup django on nginx + gunicorn on a centos6 server (firewall off, selinux disabled). the project works locally on the server (tested running gunicorn on 127.0.0.1:8221), but not on the whole network. the project should be accessible from a subdomain project.mydomain.com
the project itself is located on a server centos6.mydomain.com and the dns server is main.mydomain.com
my ngnix conf for the project:
upstream project {
server 127.0.0.1:8221 fail_timeout=0;
}
server {
listen 80;
server_name project.mydomain.com;
access_log /var/log/nginx/project.mydomain.com.log;
error_log /var/log/nginx/project.mydomain.com.log;
root /home/USER/djangosites/project;
location / {
proxy_set_header Host $host;
if (!-f $request_filename){
proxy_pass http://project;
break;
}
}
location /media {
alias /home/USER/djangosites/project/media;
}
location /static {
alias /home/USER/djangosites/project/static;
}
}
nginx conf for the centos6 (working)
server {
listen 80 default_server;
server_name centos6.mydomain.com;
access_log /var/log/nginx/centos6.mydomain.com.access.log main;
error_log /var/log/nginx/centos6.mydomain.com.error.log;
location / {
root /var/www/centos6.mydomain.com;
index index.html;
}
}
gunicorn conf
import multiprocessing
bind = "127.0.0.1:8221"
logfile = "/home/USER/djangosites/project/gunicorn.log"
workers = multiprocessing.cpu_count() * 2 + 1
would i be better off giving a new ip (to the outside) to the project that is different from centos6.mydomain.com or i can just use the same ip with different local port?
how should i configure hosts.db on main.mydomain.com then?
centos6 A xxx.xxx.xxx.220
project A xxx.xxx.xxx.221
or
centos6 A xxx.xxx.xxx.220
project A xxx.xxx.xxx.220:8221
or
centos6 A xxx.xxx.xxx.220
project CNAME centos6
i'm kind of more inclined to give a new ip because everything is behind a m0n0wall, so a new ip could perhaps be easier to manage.
so basically, i'm guessing that my nginx conf for the project is flawed. what should i do with it?

ok. got it working :)
hosts.db on main.mydomain.com
project CNAME centos6
gunicorn runnig on 127.0.0.1:8221
and edited the nginx conf as stated above.

Related

Django + Nginx save uploded file to other server and reverse file

I have 2 servers: first for Nginx and Django and second server for storage
Nginx and app server IP: 192.168.1.1
storage server IP: 192.168.1.2
Nginx installed on two servers.
In Django config media path:
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
Nginx config is in the first server:
server {
listen 80;
server_name 192.168.1.1;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log ;
location = /favicon.ico { access_log off; log_not_found off; }
location /media/ {
proxy_pass http://192.168.1.2/;
}
location / {
uwsgi_pass unix:/tmp/uwsgi/app.sock;
include uwsgi_params;
}
location /static/ {
alias /home/ubuntu/app/static/;
}
}
and storage server Nginx config:
server {
listen 80;
server_name 192.168.1.2;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log ;
location /media/ {
alias /home/ubuntu/app/media;
}
}
Questions:
How can Django save (upload file) to the storage server (192.168.1.2)?
better if suggest solutions with minimum changes in code.
How Nginx can reverse files from a storage server?
the end user just typing 192.168.1.1
Solution 1: Network File System (NFS)
An example of NFS is GlusterFS.
What it does is, it makes multiple disks or servers available as a single directory. So you can configure it to show your other server as media directory and any files you put in there, it will be automatically stored in your other server. And you can fetch those files as if they are in the media directory even though they are on another machine. No changes required to your django code, or nginx config.
Solution 2: Custom File Storage backend
Another solution is to write your own File Storage backend and save the images to your second server from there. In fact, there's a library called django-storages which supports uploading files to another server using FTP. See docs: http://django-storages.readthedocs.io/en/latest/backends/ftp.html
Personally, second solution seems better to me because you don't really need NFS right now. And even if you do later on, you can install Gluster on your second server and scale out from there.

Nginx redirects to default page

I am setting up a domain for my Django/Gunicorn/Nginx server. It works fine with IP address instead of domain name in server_name but when I add domain name it redirects to default Ubuntu Nginx page. My Nginx file looks like this (please note that I replaced my domain with example.com):
Path : /etc/nginx/sites-available/projectname
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
location = /favicon.ico {access_log off; log_not_found off;}
location /static/ {
root /path/to/static/dir;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unix:/path/to/gunicorn.sock;
}
}
I have run the command sudo nginx -t and sudo service nginx restart but no effect. Please let me know if I am doing anything wrong.
1- see main nginx.conf how include all config files. if it is including site-enabled path then go to path and see is a shortcut to config file of this site under site available?
or if all sites are enabled in nginx config file include directly available
include /etc/nginx/sites-available/*;
2-mix two server define code once and with rule forward non www to with www
3-if not work check dns config problem and see result from inside of server via putty not from outside of server with browser to see it is nginx problem or dns config problem.
note: changing dns name servers taken some hours to work and effect on clients.

nginx not serving static files on a public server

I am trying to deploy a django project using nginx and gunicorn. I followed this tutorial.
Gunicorn is able to run my project but nginx is not serving static files.
My nginx conf:
server {
listen 80;
server_name 69.12.74.39;
location / {
proxy_pass http://0.0.0.0:8001;
}
location /static/ {
autoindex on;
alias /home/ekodev/accounts/arham/ekomerz/static/;
}
}
I checked the location of my static folder and its right. The gunicorn command i am using is:
gunicorn ekomerz.wsgi:application --bind=0.0.0.0:8001
EDIT: On my local machine it works fine.
server {
listen localhost:8000;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
autoindex on;
alias /home/manish/Desktop/ekomerz/ekomerz/static/;
}
}
What is wrong with the configuration?
You have Apache running on your Port 80 and nginx can't bind to Port 80.
View your log files.
You can stop apache with: sudo service apache2 stop
And start nginx with: sudo service nginx start

Nginx conf for Gunicorn on another vm?

I have hosted my Django project on Ubuntu using Gunicorn as a web server.
Now I want to serve my requests from Nginx but it should be on a different vm.
Normally my nginx project.conf would be like:
server {
listen 80;
server_name server_domain_or_IP;
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
What changes should be made here to let Nginx route requests to my Gunicorn server.
You need to bind Gunicorn to an IP address and port instead of a UNIX socket.
Then in your Nginx config, change proxy_pass to the IP address and port that you are running gunicorn on.
proxy_pass http://1.2.3.4:8000;

nginx not working with gunicorn for external IP's

I am using nginx and gunicorn for a django application on AWS.
Here is my /etc/nginx/sites-enabled/mywebsite
server {
listen 80;
server_name mywebsite.com;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
autoindex on;
alias /home/ubuntu/mywebsite/staticfiles/;
}
}
The Gunicorn command I am running.
gunicorn mywebsite.wsgi:application --bind=127.0.0.1:8001
All this is on AWS
The problem is I can access the website by going to mywebsite.com and it works as expected on any machine on my home network but other people(not on my home network) still get welcome to nginx.
I have the domain mywebsite.com pointed to my was elastic IP
I also have port 80 open on AWS.
There are high posibility that the other person outside your network will access www.mywebsite.com. Change the server_name into.
server {
listen 80;
server_name mywebsite.com www.mywebsite.com;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
autoindex on;
alias /home/ubuntu/mywebsite/staticfiles/;
}
}