imporve nginx configs

This commit is contained in:
prance 2022-01-28 01:28:46 +01:00
parent 840fb66840
commit b02e5bbb38
6 changed files with 50 additions and 32 deletions

View File

@ -2,7 +2,7 @@ import subprocess
class NGINXConfigurator: class NGINXConfigurator:
_config_path = "/etc/nginx/nginx.conf" _config_path = "/etc/nginx/conf.d/muor.conf"
_location_preamble_path = "MUOR/configs/muor_before_location_block.conf" _location_preamble_path = "MUOR/configs/muor_before_location_block.conf"
_location_epilouge_path = "MUOR/configs/muor_after_location_block.conf" _location_epilouge_path = "MUOR/configs/muor_after_location_block.conf"
@ -14,7 +14,8 @@ class NGINXConfigurator:
config = cls._get_config(sessions) config = cls._get_config(sessions)
with open(cls._config_path, 'w') as F: with open(cls._config_path, 'w') as F:
F.write(config) F.write(config)
completed_process = subprocess.run(["sudo", "nginx", "-s", "reload"]) completed_process = subprocess.run(
["sudo", "systemctl", "reload", "nginx"])
return completed_process.returncode return completed_process.returncode
@classmethod @classmethod
@ -43,7 +44,7 @@ class NGINXConfigurator:
@classmethod @classmethod
def _get_upstream_entry(cls, uid, port): def _get_upstream_entry(cls, uid, port):
return f"upstream {uid}" + " {\n" \ return f"upstream {uid}" + " {\n" \
+ f" server localhost:{port}" + "\n}" + f" server localhost:{port};" + "\n}"
@classmethod @classmethod
def _get_location_entry(cls, uid, sessionid): def _get_location_entry(cls, uid, sessionid):

View File

@ -1,15 +1,15 @@
if ($cookie_sessionid = ""){ if ($cookie_sessionid = ""){
proxy_pass django; # TODO prolly change that to uwsgi_pass proxy_pass http://django; # TODO prolly change that to uwsgi_pass
} }
} }
ssl_certificate_key /ssl/private.pem; # ssl_certificate_key /ssl/private.pem;
ssl_certificate /ssl/certs.pem; # ssl_certificate /ssl/certs.pem;
ssl_protocols TLSv1.2; # ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256'; # ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on; # ssl_prefer_server_ciphers on;
# add_header Strict-Transport-Security max-age=31536000; # add_header Strict-Transport-Security max-age=31536000;
} }

View File

@ -6,19 +6,20 @@ map $http_upgrade $connection_upgrade {
} }
upstream django { upstream django {
server localhost:8080 server localhost:8080;
} }
server { # server {
listen 80 reuseport; # listen 80 reuseport;
server_name randomsec.projektstudencki.pl; # server_name randomsec.projektstudencki.pl;
return 301 https://randomsec.projektstudencki.pl; # return 301 https://randomsec.projektstudencki.pl;
} # }
# configuration of the server # configuration of the server
server { server {
# the port your site will be served on # the port your site will be served on
listen 443 ssl http2 reuseport; # listen 443 ssl http2 reuseport; #TODO change to 443
listen 80 reuseport;
# the domain name it will serve for # the domain name it will serve for
server_name randomsec.projektstudencki.pl server_name randomsec.projektstudencki.pl
charset utf-8; charset utf-8;
@ -26,14 +27,24 @@ server {
# max upload size # max upload size
client_max_body_size 75M; # adjust to taste client_max_body_size 75M; # adjust to taste
location /static { # location /static {
alias /path/to/static; #TODO add path # alias /path/to/static; #TODO add path
expires 30d; # expires 30d;
access_log off; # access_log off;
add_header Pragma public; # add_header Pragma public;
add_header Cache-Control "public"; # add_header Cache-Control "public";
# }
error_page 500 502 503 504 404 /error.html;
location = /error.html {
add_header Content-Type text/html;
return 200 '<meta http-equiv="refresh" content="1; URL=http://localhost/" />';
} }
location /logout/ {
proxy_pass http://django;
}
# Finally, send all non-media requests to the Django server. # Finally, send all non-media requests to the Django server.
location / { location / {

View File

@ -25,7 +25,7 @@ SECRET_KEY = 'django-insecure-t52#vo-k9ty*$@u9bf75hrkd#^o_)gadrz9$7w%xnkb-0#y!bi
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'django']
# Application definition # Application definition
@ -50,6 +50,9 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ]
CSRF_TRUSTED_ORIGINS = ["http://localhost", "http://192.168.88.206",
"http://randomsec.projektstudencki.pl"]
ROOT_URLCONF = 'MUOR.urls' ROOT_URLCONF = 'MUOR.urls'
TEMPLATES = [ TEMPLATES = [

View File

@ -60,13 +60,16 @@ def profile_start_up(sender, user, request, **kwargs):
@receiver(user_logged_out) @receiver(user_logged_out)
def profile_logged_out(sender, user, request, **kwargs): def profile_logged_out(sender, user, request, **kwargs):
# Obtaining container_id # Obtaining container_id
session = Session.objects.get(user=user) try:
container_id = session.container_id session = Session.objects.get(user=user)
container_id = session.container_id
# Turn off and delete container # Turn off and delete container
docker_manager = DockerManager() docker_manager = DockerManager()
docker_manager.turn_off_container(container_id) docker_manager.turn_off_container(container_id)
# Delete session # Delete session
session.delete() session.delete()
except Session.DoesNotExist:
pass
NGINXConfigurator.refresh_config(get_sessions_data()) NGINXConfigurator.refresh_config(get_sessions_data())

View File

@ -24,7 +24,7 @@ class SignUpView(generic.CreateView):
def home(request): def home(request):
if request.user.is_authenticated: # if request.user.is_authenticated:
logout(request) # logout(request)
return redirect("/welcome") return redirect("/welcome")