Django/Nginx : serving static files in production - django

I am trying to serve CSS and other static files on my django app using NGINX server. So I tried to configure it. Here is my /etc/nginx/sites-enabled/mydomain:
server {
listen 80;
server_name redpillers.net;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/limited/REDPILLERS;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/limited/REDPILLERS/redpillers.sock;
}
location /static/ {
alias /home/limited/REDPILLERS/static/;
}
}
But when I restart the service I got an error in the error logs file:
2019/05/22 07:26:44 [emerg] 11589#11589: duplicate location "/static/" in /etc/nginx/sites-enabled/mydomain:15

server {
listen 80;
server_name redpillers.net;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/home/limited/REDPILLERS/redpillers.sock;
}
location /static/ {
alias /home/limited/REDPILLERS/static/;
}
}
You have location /static/ two times in configuration. Just remove one.

please remove the second /static/ location and keep the first one
So the ultimate config:
server {
listen 80;
server_name redpillers.net;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/limited/REDPILLERS;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/limited/REDPILLERS/redpillers.sock;
}
}

Related

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.

When accessing a resource on website a forward slash gets removed from url when redirecting to https

I have an Nginx serving my django application. When I try to access example.com/resource it should redirect to https://example.com/resource but in my case it redirects to https://example.comresource (removing the / before resource)
I have a very minimal Nginx configuration (as you'll see below). I don't know where to start to figure out what's wrong.
server{
listen 443;
listen [::]:443 ssl;
ssl on;
ssl_certificate /etc/ssl/example_com.crt;
ssl_certificate_key /etc/ssl/example_com.key;
server_name example.com;
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /static/ {
autoindex on;
alias /home/administrator/example/collectedstatic/;
}
location /media {
alias /home/administrator/example/store;
}
}
server {
listen 8000;
server_name localhost;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
autoindex on;
alias /home/administrator/example/collectedstatic/;
}
location /media {
alias /home/administrator/example/store;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
There are no error messages, just a webpage not found.

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