Nginx conf for Gunicorn on another vm? - django

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;

Related

How to configure nginx, gunicorn to run 2 django servers with different domain names

I have DjangoServer1 and DjangoServer2 running a virtualenv, where gunicorn is installed. nginx is installed under user in Ubuntu.
I make DjangoServer1 running under nginx, gunicorn.
Server IP: 12.12.12.12
Web site domain for DjangoServer1 is mydomain1.com
Web site domain for DjangoServer2 is mydomain2.com
This is nginx server config for DjangoServer1.
/etc/nginx/sites-available/DjangoServer1
server {
listen 0.0.0.0:80;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/develop/DjangoServer1;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/develop/DjangoServer1/DjangoServer1.sock;
}
}
I start the DjangoServer1:
1) Under virtualenv, run gunicorn command to start DjangoServer1
gunicorn --daemon --workers 3 --bind unix:/home/user/develop/DjangoServer1/DjangoServer1.sock DjangoServer1.wsgi
2) Then, run:
sudo service nginx restart
3) In router, I portforward port 80, 8000, to server 12.12.12.12
4) In browser, enter: 12.12.12.12. DjangoServer1 works. Enter: mydomain1.com, DjangoServer1 works.
Now, I want to run DjangoServer2 under same server: 12.12.12.12
Question: How to configure DjangoServer1 and DjangoServer2 to different port?
How to run gunicorn command to use different port? Following command uses port 8000? Why?
gunicorn --daemon --workers 3 --bind unix:/home/user/develop/DjangoServer1/DjangoServer1.sock DjangoServer1.wsgi
How to configure nginx file?
Change your Gunicorn command to run the servers on the specified port.
gunicorn --daemon --workers 3 --bind :8080 DjangoServer1.wsgi
Now change your NGINX conf file to forward it to the Application Server.
upstream django-server-1 {
server 0.0.0.0:8080;
}
server {
listen 0.0.0.0:80;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/develop/DjangoServer1;
}
location / {
include proxy_params;
proxy_pass http://django-server-1;
proxy_next_upstream off;
}
}
Restart your NGINX service.
This will forward all the requests coming to 80 port to your application server DjangoServer1.
If you explicitly want to forward requests coming to 8080 to your application server, change the server block in the NGINX configuration or have a new server block with your rules.

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 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/;
}
}

Docker django nginx gunicorn URL dropping port

Firstly apologies if this is a duplicate but i have not found a solution through similar posts shown in SO
I have a Docker Django image which is using nginx and gunicorn.
Gunicorn script:
exec /var/www/venv/bin/gunicorn wsgi:application \
--bind 0.0.0.0:8001 \
--access-logfile /var/log/gunicorn/access.log \
--error-logfile /var/log/gunicorn/error.log
Nginx config:
server {
server_name 172.0.0.1;
access_log off;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host:$server_port;
}
location /static/ {
autoindex on;
alias /var/www/django/assets/;
expires 7d;
}
}
I am exposing port 80 and mapping to 49260.
When browsing to the docker host external ip including the port the site is published and serves the static files.
http://xxx.xx.xx.xxx:49260/
The issue is when i navigate to any other page in the django site, the mapped port is dropped from the URL which is then picked up by the host server ngnix config.
What i am trying to achieve is maintain the port in the URL which i can later reverse proxy from the host server.
Any advice would be really appreciated.
The answer was adding:
proxy_set_header Host $http_host;
to the nginx conf which prints hostname:portnumber
See serverfault.com link here: Original thread

django+nginx+gunicorn - subdomain joy

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.