refactor(nginx.sh): refactor set_nginx_cfg()

- implements two dedicated nginx configs for mainsail and fluidd

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-05-30 18:38:45 +02:00
parent f3a769e03e
commit d6b95c9d10
3 changed files with 121 additions and 26 deletions

View File

@@ -1,10 +1,10 @@
# /etc/nginx/sites-available/<<UI>> # /etc/nginx/sites-available/fluidd
server { server {
listen 80; listen 80;
access_log /var/log/nginx/<<UI>>-access.log; access_log /var/log/nginx/fluidd-access.log;
error_log /var/log/nginx/<<UI>>-error.log; error_log /var/log/nginx/fluidd-error.log;
# disable this section on smaller hardware like a pi zero # disable this section on smaller hardware like a pi zero
gzip on; gzip on;
@@ -16,8 +16,8 @@ server {
gzip_http_version 1.1; gzip_http_version 1.1;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml; gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml;
# web_path from <<UI>> static files # web_path from fluidd static files
root /home/pi/<<UI>>; root /home/pi/fluidd;
index index.html; index index.html;
server_name _; server_name _;

95
resources/mainsail Normal file
View File

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

View File

@@ -269,34 +269,25 @@ function detect_conflicting_packages() {
function set_nginx_cfg() { function set_nginx_cfg() {
local interface=${1} local interface=${1}
if [[ ${SET_NGINX_CFG} == "true" ]]; then if [[ ${SET_NGINX_CFG} == "true" ]]; then
local cfg="${RESOURCES}/${interface}"
#check for dependencies #check for dependencies
local dep=(nginx) local dep=(nginx)
dependency_check "${dep[@]}" dependency_check "${dep[@]}"
status_msg "Creating Nginx configuration for ${interface^} ..." local cfg_src="${RESOURCES}/${interface}"
cat "${RESOURCES}/klipper_webui_nginx.cfg" > "${cfg}" local cfg_dest="/etc/nginx/sites-available/${interface}"
sed -i "s/<<UI>>/${interface}/g" "${cfg}"
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 if [[ ${SET_LISTEN_PORT} != "${DEFAULT_PORT}" ]]; then
status_msg "Configuring port for ${interface^} ..." sudo sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg_dest}"
sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg}" sudo sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${SET_LISTEN_PORT};/" "${cfg_dest}"
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}!"
fi fi
#remove nginx default config #remove nginx default config
@@ -308,7 +299,16 @@ function set_nginx_cfg() {
if [[ ! -e "/etc/nginx/sites-enabled/${interface}" ]]; then if [[ ! -e "/etc/nginx/sites-enabled/${interface}" ]]; then
sudo ln -s "/etc/nginx/sites-available/${interface}" "/etc/nginx/sites-enabled/" sudo ln -s "/etc/nginx/sites-available/${interface}" "/etc/nginx/sites-enabled/"
fi 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 sudo systemctl restart nginx.service
ok_msg "NGINX configuration for ${interface^} was set!"
fi fi
} }