How to make Nginx config file for Django Project? - django

I made Nginx config for My Django project.
For DNS I associate with my IP.
But this config not working, when I'm checking xyz.com in web but getting error ERR_CONNECTION_REFUSED.
server {
listen 80;
server_name xyz.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/project/project.sock;
}
}
Please correct me if i'm doing wrong.

Related

Getting 504 Gateway Time-out nginx/1.18.0 (Ubuntu) in django nginx

I am facing problems launching my django app in digitalocean. I have done all the needful but I am quite sure I made error some where. Below is my nginx configuration file. What could be the problem . Thanks alot .
PLease I would also love to know how to access nginx error log file through command line.
server {
server_name server_Ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/username/myproject/smsdigo;
}
location /media/ {
root /home/username/myproject/smsdigo;
}
location /locale/ {
root /home/ebong/myproject/smsdigo;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
server {
listen 80;
client_max_body_size 3000M;
client_body_buffer_size 3000M;
client_body_timeout 120;
}
Problably you need increase fastcgi timeout directives:
Try 60s or more. For example:
server{
fastcgi_read_timeout 60s;
fastcgi_send_timeout 60s;
}

Django + nginx + socket - url path not working

Whenever I try to go to www.example.com/anything it always redirects to www.example.com.
It works fine if I don't use the domain. Ex: ip.ip.ip.ip/anything works.
here's my sites-available
server {
listen 80;
listen [::]:80;
server_name my_ip_here;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/chris/Portfolio_v2/mysite;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
You need to add your domain name to the server block
server_name www.example.com;

Nginx location doesnt workd

I have a Django app running with Nginx and Gunicorn on mysite.com. I would like it to be accessible only under mysite.com/myapp. This is my nginx configuration:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myprojectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
When I change location / to location /myapp/, I get "Not found error". Nginx error log doesn't seems to have anything. What could be the issue?

Redirect Django site request on multiple ports with nginx

I have a domain say 'mydjango.com' . When its called i want to handle the request on multiple ports on the same IP. 122.34.55.1:8000 , 122.34.55.1:8001, 122.34.55.1:8002
This is expected for load balancing. I am using wsgi, dgango and ngix.
My nginx config file /etc/nginx/sites-available/djwsgi is -
server {
listen 80;
listen 8001;
listen 8002;
listen 8003;
location = /favicon.ico { access_log off; log_not_found off; }
root /home/raka/djwsgi;
server_name mydjango.com;
location /static/ {
root /home/raka/djwsgi;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/djwsgi.sock;
}
}
But by default mydjango.com is mapped with port 80 only.
Other ports are being called when i am mentioning port number like mydjango.com:8002
What i need is - when i call mydjango.com nginx should call next port every time.
Like, 80 then 8001 then 8002, 8003, then 80, 8001, .
Please any body suggest any idea !
Thanks
I'm not sure that's what you want. Tell me
upstream mydjangoback {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
server {
listen 80;
location = /favicon.ico { access_log off; log_not_found off; }
root /home/raka/djwsgi;
server_name mydjango.com;
location /static/ {
root /home/raka/djwsgi;
}
location / {
proxy_pass http://mydjangoback;
}
}
I found the complete solution. I am posting that here.
This is how nginx and wsgi used to balance the load on our sites and avoid manual running of Django application on server.
upstream backend {
server 127.0.1.1:8002;
server 127.0.1.1:8001;
server 127.0.1.1:8000;
}
server {
listen 80;
listen [::]:80;
location = /favicon.ico { access_log off; log_not_found off; }
root /home/raka/djwsgi;
server_name mysite.com;
location /static/ {
root /home/raka/djwsgi;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/djwsgi.sock;
proxy_pass http://backend;
}
}
server {
listen 8000;
listen 8001;
listen 8002;
location = /favicon.ico { access_log off; log_not_found off; }
root /home/raka/djwsgi;
server_name mysite.com;
location /static/ {
root /home/raka/djwsgi;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/djwsgi.sock;
}
}
There are many key which could be set in above configuration to achieve extra benefit.
Like in wsgi configuration file we can mention the number of processes.

Django REST AngularJS NGINX Config

I have set up a server according to this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
Everything is working a-okay, but I would like to change my NGINX set up to incorporate AngularJS for the front end. Right now I have it configured as the tutorial says and when I visit myip/ I get my Django app, and when I go to myip/static/ I get my static files. Great.
What I would like to do is serve the Django API from a api.myip subdomain, and have myip/ actually point to my static (angular app) files.
Any insight on how to configure NGINX to route this correctly?
NGINX Config currently looks like this:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
try like this
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
root /home/user/myproject;
}
}
server {
listen 80;
server_name api.server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}