From d6b95c9d10d467860af5842d269aba8cbc16797e Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 30 May 2022 18:38:45 +0200 Subject: [PATCH] refactor(nginx.sh): refactor `set_nginx_cfg()` - implements two dedicated nginx configs for mainsail and fluidd Signed-off-by: Dominik Willner th33xitus@gmail.com --- resources/{klipper_webui_nginx.cfg => fluidd} | 10 +- resources/mainsail | 95 +++++++++++++++++++ scripts/nginx.sh | 42 ++++---- 3 files changed, 121 insertions(+), 26 deletions(-) rename resources/{klipper_webui_nginx.cfg => fluidd} (92%) create mode 100644 resources/mainsail diff --git a/resources/klipper_webui_nginx.cfg b/resources/fluidd similarity index 92% rename from resources/klipper_webui_nginx.cfg rename to resources/fluidd index bd4fe85..f25e487 100644 --- a/resources/klipper_webui_nginx.cfg +++ b/resources/fluidd @@ -1,10 +1,10 @@ -# /etc/nginx/sites-available/<> +# /etc/nginx/sites-available/fluidd server { listen 80; - access_log /var/log/nginx/<>-access.log; - error_log /var/log/nginx/<>-error.log; + access_log /var/log/nginx/fluidd-access.log; + error_log /var/log/nginx/fluidd-error.log; # disable this section on smaller hardware like a pi zero gzip on; @@ -16,8 +16,8 @@ server { gzip_http_version 1.1; gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml; - # web_path from <> static files - root /home/pi/<>; + # web_path from fluidd static files + root /home/pi/fluidd; index index.html; server_name _; diff --git a/resources/mainsail b/resources/mainsail new file mode 100644 index 0000000..efabb63 --- /dev/null +++ b/resources/mainsail @@ -0,0 +1,95 @@ +# /etc/nginx/sites-available/mainsail + +server { + listen 80; + + access_log /var/log/nginx/mainsail-access.log; + error_log /var/log/nginx/mainsail-error.log; + + # disable this section on smaller hardware like a pi zero + gzip on; + gzip_vary on; + gzip_proxied any; + gzip_proxied expired no-cache no-store private auth; + gzip_comp_level 4; + gzip_buffers 16 8k; + gzip_http_version 1.1; + gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml; + + # web_path from mainsail static files + root /home/pi/mainsail; + + index index.html; + server_name _; + + # disable max upload size checks + client_max_body_size 0; + + # disable proxy request buffering + proxy_request_buffering off; + + location / { + try_files $uri $uri/ /index.html; + } + + location = /index.html { + add_header Cache-Control "no-store, no-cache, must-revalidate"; + } + + location /websocket { + proxy_pass http://apiserver/websocket; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_read_timeout 86400; + } + + location ~ ^/(printer|api|access|machine|server)/ { + proxy_pass http://apiserver$request_uri; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Scheme $scheme; + } + + location /webcam/ { + postpone_output 0; + proxy_buffering off; + proxy_ignore_headers X-Accel-Buffering; + access_log off; + error_log off; + proxy_pass http://mjpgstreamer1/; + } + + location /webcam2/ { + postpone_output 0; + proxy_buffering off; + proxy_ignore_headers X-Accel-Buffering; + access_log off; + error_log off; + proxy_pass http://mjpgstreamer2/; + } + + location /webcam3/ { + postpone_output 0; + proxy_buffering off; + proxy_ignore_headers X-Accel-Buffering; + access_log off; + error_log off; + proxy_pass http://mjpgstreamer3/; + } + + location /webcam4/ { + postpone_output 0; + proxy_buffering off; + proxy_ignore_headers X-Accel-Buffering; + access_log off; + error_log off; + proxy_pass http://mjpgstreamer4/; + } +} diff --git a/scripts/nginx.sh b/scripts/nginx.sh index b8d8f46..9da518d 100644 --- a/scripts/nginx.sh +++ b/scripts/nginx.sh @@ -269,34 +269,25 @@ function detect_conflicting_packages() { function set_nginx_cfg() { local interface=${1} + if [[ ${SET_NGINX_CFG} == "true" ]]; then - local cfg="${RESOURCES}/${interface}" #check for dependencies local dep=(nginx) dependency_check "${dep[@]}" - status_msg "Creating Nginx configuration for ${interface^} ..." - cat "${RESOURCES}/klipper_webui_nginx.cfg" > "${cfg}" - sed -i "s/<>/${interface}/g" "${cfg}" + local cfg_src="${RESOURCES}/${interface}" + local cfg_dest="/etc/nginx/sites-available/${interface}" + + status_msg "Creating NGINX configuration for ${interface^} ..." + + # copy config to destination and set correct username + [[ -f ${cfg_dest} ]] && sudo rm -f "${cfg_dest}" + sudo cp "${cfg_src}" "${cfg_dest}" + sudo sed -i "/root/s/pi/${USER}/" "${cfg_dest}" if [[ ${SET_LISTEN_PORT} != "${DEFAULT_PORT}" ]]; then - status_msg "Configuring port for ${interface^} ..." - sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg}" - sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${SET_LISTEN_PORT};/" "${cfg}" - fi - - #set correct user - if [[ ${interface} == "mainsail" || ${interface} == "fluidd" ]]; then - sudo sed -i "/root/s/pi/${USER}/" "${cfg}" - fi - - #moving the config file into correct directory - sudo mv "${cfg}" "/etc/nginx/sites-available/${interface}" - ok_msg "Nginx configuration for ${interface^} was set!" - if [[ -n ${SET_LISTEN_PORT} ]]; then - ok_msg "${interface^} configured for port ${SET_LISTEN_PORT}!" - else - ok_msg "${interface^} configured for default port ${DEFAULT_PORT}!" + sudo sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg_dest}" + sudo sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${SET_LISTEN_PORT};/" "${cfg_dest}" fi #remove nginx default config @@ -308,7 +299,16 @@ function set_nginx_cfg() { if [[ ! -e "/etc/nginx/sites-enabled/${interface}" ]]; then sudo ln -s "/etc/nginx/sites-available/${interface}" "/etc/nginx/sites-enabled/" fi + + if [[ -n ${SET_LISTEN_PORT} ]]; then + ok_msg "${interface^} configured for port ${SET_LISTEN_PORT}!" + else + ok_msg "${interface^} configured for default port ${DEFAULT_PORT}!" + fi + sudo systemctl restart nginx.service + + ok_msg "NGINX configuration for ${interface^} was set!" fi }