taking a django site down for maintenance? - django

I am running django with nginx/gunicorn. I am not deeply familiar with how nginx and gunicorn work, but suppose that I want to take my django site down for maintenance.
I assume I would be wanting to redirect to some simple maintenance page by going into the nginx/gunicorn settings and redirecting something but I could be wrong.
What is the correct (easiest) way to do this?
EDIT adding nginx config proxy statements:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
EDIT 2: adding nginx sites-enabled file
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/mysite/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/mysite/static_dump;
}
location / {
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
#proxy_redirect off;
#proxy_pass http://app_server;
if (-f /usr/share/nginx/html/index.html) {
return 503;
}
}
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /503.html break;
}
}

One of the ways to do is to add the maintenance page somewhere in the server, and then in your nginx file ( In the sites-enabled folder) redirect requests to the site to that maintenance page.
Your nginx page should contain:
server_name myhost.example.com;
root /path/to/html/file/directory;
index index.html;
Only the above 3 lines are enough

Related

Django: serve static section of a web-app

I have:
a Django web app
a separate static HTML site (blog)
The static site is a separate directory tree.
I want the static site to be served as a sub-section of the web app.
For example, the app is at http://app.com/ and the static is site served from http://app.com/blog
Here's my /etc/nginx/sites-available/app:
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
...
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
server {
server_name yourdomain.com;
location /blog{
root /path/to/static/html;
}
location /{
# your django app configuration
proxy_pass http://localhost:8000$request_uri;
# other configurations
}
}

connect() failed (111: Connection refused) while connecting to upstream, client

I am trying to deploy a django instance to ec2 . I am using a combination of nginx and gunicorn to achieve that. I got the nginx isntance and gunicorn to start correctly and I am able to get my instance running. But when i try to upload an image to the database on my application I run into this error in my gunicorn error.log :
connect-failed-111-connection-refused-while-connecting-to-upstream
Also all my api calls from the front end to the database return a 500 internal server in the console.
My nginx.conf looks like
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
include /etc/nginx/sites-available/*;
index index.html index.htm;
server {
listen 127.0.0.1:80;
listen [::]:80 default_server;
server_name 127.0.0.1;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
# redir
And my sites-enabled/default file as
upstream app_server_djangoapp {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
#EC2 instance security group must be configured to accept http connections over Port 80
listen 80;
server_name myec2isntance.com;
access_log /var/log/nginx/guni-access.log;
error_log /var/log/nginx/guni-error.log info;
keepalive_timeout 5;
# path for static files
location /static {
alias xxxxxx;
}
location /media {
alias xxxxxx;
}
location / {
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}
}
I tried most of the things people talked about - adding right permissisons to the folders. Changing localhost to 127.0.0.1 etc. I am relatively new to this topic so any help would be much appreciated!
Thank you
I would suggest to change default to this :
upstream app_server_djangoapp {
server 127.0.0.1:8000 max_fails=3 fail_timeout=50;
keepalive 512;
}
- remove
keepalive_timeout 5;
- why do u have two location / blocks ?
location / {
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}

Nginx loading images very slow

I am trying to load some high resolution images with Jpeg format and specs are 300dpi, 5000 x 5000 resolution in Django production.
Following are my nginx settings at digitalocean:
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/django/django_project;
index index.html index.htm;
client_max_body_size 4G;
server_name localhost;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/media/;
}
# your Django project's static files - amend as required
location /static/django_project/ {
alias /home/django/django_project/static/django_project/;
}
# Django static images
location /static/django_project/images {
alias /home/django/django_project/static-only/django_project/images/;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
After Browsing cache enabled:
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/django/django_project;
index index.html index.htm;
client_max_body_size 4G;
server_name mysite.com;
keepalive_timeout 5;
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/media/;
}
# your Django project's static files - amend as required
location /static/django_project/ {
alias /home/django/django_project/static/django_project/;
}
# Django static images
location /static/django_project/images {
alias /home/django/django_project/static-only/django_project/images/;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
My images on live site are very slow to be loaded. Please advise or ref to some useful resource to fix this.
Thanks
Please use nginx Cache, this will solve your problem..
https://www.nginx.com/blog/nginx-caching-guide/
You can also cache the images in browser too by enabling browser cache...
https://www.howtoforge.com/make-browsers-cache-static-files-on-nginx

Nginx deploying application in multiple port

I'm planning to deploy my django application in 2 ports of nginx server. Port 80 is working but port 99 is not working.
Here are my configs.
Port 99 config
upstream app_server_1 {
# For a TCP configuration:
server 127.0.0.1:8888 fail_timeout=0;
}
# configuration of the server
server {
#add_header HTTP_X_FORWARDED_PROTO https;
# the port your site will be served on
listen 99 default_server;
charset utf-8;
server_name 52.23.184.237;
# SSL configs
#listen 443 default ssl;
#ssl_certificate /etc/ssl/tmatch.crt;
#ssl_certificate_key /etc/ssl/tmatch.key;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /var/www/tmatch/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/tmatch/static; # your Django project's static files - amend as required
}
location / {
# checks for static file, if not found proxy to app
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server_1;
}
port 80 config
upstream app_server {
# For a TCP configuration:
server 127.0.0.1:8000 fail_timeout=0;
}
# configuration of the server
server {
#add_header HTTP_X_FORWARDED_PROTO https;
# the port your site will be served on
listen 80 default_server;
charset utf-8;
server_name 52.23.184.237;
# SSL configs
#listen 443 default ssl;
#ssl_certificate /etc/ssl/tmatch.crt;
#ssl_certificate_key /etc/ssl/tmatch.key;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /var/www/tmatch/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/tmatch/static; # your Django project's static files - amend as required
}
location / {
# checks for static file, if not found proxy to app
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
Any errors?
I've had issues with multiple default_server
Try taking one of them out, worked for me

django+gunicorn+nginx 404 serving static files

I have django+gunicorn+nginx running on 192.168.1.81:3000. The web app will not serve any static files; it returns a 404 error. This suggests that there is a problem with the nginx virtual server config file. I have tried several solutions offered on stack overflow with no success. What is wrong with the nginx virtual server file?
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
location /static/ {
alias /home/pi/door_site/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}
In Nginx virtual server conf file inside server section you need to add to location sections for Nginx can serve files in /static/ and /media/ folders:
location /media {
alias /path/to/your/folder/media;
}
location /static {
alias /path/to/your/folder/static;
}
After that test Nginx configuration:
sudo nginx -t
and reload Nginx:
sudo nginx -s reload
(or restart - sudo /etc/init.d/nginx restart )
try this config using server root and try_files
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
root /path/to/root # place your static directories in root i.e /path/to/root/static
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
This will try to find your static file and then move onto your app server.
Make sure nginx is running as the right user to access your files and that your static files permissions are correctly set perhaps with:
chmod -R u=rwX,g=rwX,o=rX static_dir
I my case it was a permission issue on static directory and it worked after assigning proper permissions.