nginx not serving static files on a public server - django

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

Related

Gunicorn not loading react frontend in django react nginx configuration digitalocean

i am trying to deploy django react app on digitalocean with nginx gunicorn. i have created the virtualenv and when i trying to run:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi
its doesn't load the react front but django backend loading. i am aware of that gunicorn does not static but it loading white page with error:
You need to enable JavaScript to run this app.
since react needs the js library file to server, and it not rendered due to that it might be giving the error. server all the static file through nginx
server {
listen 80;
server_name example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/sample;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/django/sample/sample.sock;
}
}

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

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.