From 8ffdf12a4af1e3d63095864f10ef5065380a8388 Mon Sep 17 00:00:00 2001 From: th33xitus <> Date: Sun, 19 Jul 2020 22:38:21 +0200 Subject: [PATCH] add octoprint reverse proxy --- resources/octoprint_nginx.cfg | 40 +++++++++++++++++++++++++++++++++++ scripts/functions.sh | 15 ++++++++----- scripts/install_octoprint.sh | 36 +++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 resources/octoprint_nginx.cfg diff --git a/resources/octoprint_nginx.cfg b/resources/octoprint_nginx.cfg new file mode 100644 index 0000000..b35d3cc --- /dev/null +++ b/resources/octoprint_nginx.cfg @@ -0,0 +1,40 @@ +map $http_upgrade $connection_upgrade { + default upgrade; + '' close; +} + +upstream octoprint { + server 127.0.0.1:5000; +} + +upstream mjpg-streamer { + server 127.0.0.1:8080; +} + +server { + listen 80; + listen [::]:80; + + location / { + proxy_pass http://octoprint/; + proxy_set_header Host $http_host; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + 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; + #proxy_set_header X-Script-Name /; + proxy_http_version 1.1; + client_max_body_size 0; + } + + location /webcam { + proxy_pass http://mjpg-streamer/; + } + + # redirect server error pages to the static page /50x.html + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root html; + } +} \ No newline at end of file diff --git a/scripts/functions.sh b/scripts/functions.sh index ac8ad21..2628943 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -78,6 +78,13 @@ restart_octoprint(){ fi } +restart_nginx(){ + if [ -e /etc/init.d/nginx ]; then + status_msg "Restarting Nginx Service ..." + sudo /etc/init.d/nginx restart && sleep 2 && ok_msg "Nginx Service restarted!" + fi +} + dep_check(){ for package in "${dep[@]}" do @@ -261,12 +268,10 @@ create_custom_hostname(){ top_border echo -e "| You can change the hostname of this machine to use |" echo -e "| that name to open the Interface in your browser. |" - echo -e "| This option is completely optional and can also be |" - echo -e "| done at a later point in time if you are unsure. |" echo -e "| |" echo -e "| Example: If you set the hostname to 'my-printer' |" - echo -e "| you can open Mainsail by browsing to: |" - echo -e "| http://my-printer.local |" + echo -e "| you can open Mainsail/Octoprint by |" + echo -e "| browsing to: http://my-printer.local |" bottom_border while true; do echo -e "${cyan}" @@ -323,7 +328,7 @@ set_hostname(){ echo "127.0.0.1 $NEW_HOSTNAME ###set by kiauh" | sudo tee -a /etc/hosts &>/dev/null fi ok_msg "New hostname successfully configured!" - ok_msg "You need to reboot your machine for changes to take effect!" + ok_msg "Remember to reboot your machine for the changes to take effect!" break else warn_msg "'$NEW_HOSTNAME' is not a valid hostname!" diff --git a/scripts/install_octoprint.sh b/scripts/install_octoprint.sh index e921b59..0766113 100755 --- a/scripts/install_octoprint.sh +++ b/scripts/install_octoprint.sh @@ -6,6 +6,7 @@ octoprint_install_routine(){ add_groups configure_autostart add_reboot_permission + octoprint_reverse_proxy_dialog load_server } @@ -108,6 +109,41 @@ add_reboot_permission(){ sleep 2 } +octoprint_reverse_proxy_dialog(){ + top_border + echo -e "| If you want to have nicer URLs or simply need | " + echo -e "| OctoPrint to run on port 80 (http's default port) | " + echo -e "| due to some network restrictions, you can set up a | " + echo -e "| reverse proxy instead of configuring OctoPrint to | " + echo -e "| run on port 80. | " + bottom_border + while true; do + echo -e "${cyan}" + read -p "###### Do you want to set up a reverse proxy now? (Y/n): " yn + echo -e "${default}" + case "$yn" in + Y|y|Yes|yes|"") octoprint_reverse_proxy; break;; + N|n|No|no) break;; + esac + done +} + +octoprint_reverse_proxy(){ + if ! [[ $(dpkg-query -f'${Status}' --show nginx 2>/dev/null) = *\ installed ]]; then + sudo apt-get install nginx -y + fi + cat ${HOME}/kiauh/resources/octoprint_nginx.cfg > ${HOME}/kiauh/resources/octoprint + sudo mv ${HOME}/kiauh/resources/octoprint /etc/nginx/sites-available/octoprint + if [ -e /etc/nginx/sites-enabled/default ]; then + sudo rm /etc/nginx/sites-enabled/default + fi + if [ ! -e /etc/nginx/sites-enabled/octoprint ]; then + sudo ln -s /etc/nginx/sites-available/octoprint /etc/nginx/sites-enabled/ + fi + restart_nginx + create_custom_hostname +} + load_server(){ start_octoprint #create an octoprint.log symlink in home-dir just for convenience