Answer the question
In order to leave comments, you need to log in
How to redirect from www to just on Ubuntu with gunicorn (socket) + nginx?
The situation is the following. Using a YouTube video that relies on this old tutorial here , I set up a server for Django.
In short:
Ununtu 18.04
pip, git, virtualenv, python3, git pull ..
gunicorn, supervisor
nginx
certbot (certificate for https)
everything works, but I can't configure it to switch from www to "without www". Just haven't tried it. And it seems to me that the ambush is that I configured gunicorn via socket.
#!/bin/bash
NAME="fasad.biz" # name application (domain)
DJANGODIR=/webapp/fsdbz/_fsdbz # path to Django application
SOCKFILE=/webapp/fsdbz/run/gunicorn.sock # we will communicte using this unix socket
USER=fsdbz # the user to run as
GROUP=webapp # the group to run as
NUM_WORKERS=3 # Gunicorn spawn. "qty core server" * 2 + 1
DJANGO_SETTINGS_MODULE=_fsdbz.settings # which settings file should Django use
DJANGO_WSGI_MODULE=_fsdbz.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ../v/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../v/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
default off;
}
upstream fsdbz {
# fail_timeout=0 means we always retry an upstream even if it failed to return a good HTTP response (in case the Unicorn master nukes a single worker for timing out).
server unix:/webapp/fsdbz/run/gunicorn.sock fail_timeout=0;
}
server {
server_name www.fasad.biz fasad.biz;
client_max_body_size 4G;
access_log /webapp/fsdbz/logs/nginx-access.log;
error_log /webapp/fsdbz/logs/nginx-error.log;
location /.well-known {
root /var/www/html; # Need for letsencrypt (certbot)
}
location /_static/ {
alias /webapp/fsdbz/_fsdbz/_static/;
}
location /_media/ {
alias /webapp/fsdbz/_fsdbz/_media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $server_name;
proxy_pass http://fsdbz;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapp/fsdbz/_fsdbz/_static/;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/fasad.biz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/fasad.biz/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = fasad.biz) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name fasad.biz;
return 404; # managed by Certbot
}
Answer the question
In order to leave comments, you need to log in
server {
listen 80;
server_name www.fasad.biz, fazad.bit;
return 301 https://fazad.bit$request_uri;
}
Old question, but oh well. If I understand the question correctly, then this can be done using DNS. A CNAME record is created, which specifies the address without WWW, and redirection is performed without reaching the server, and the NGINX config does not need to be touched. From an SEO point of view, this is more correct.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question