openHAB: redirect port 8080 to port 80
The openHAB Raspberry image, by default, listens on port 8080. After using the image for a while, it became quite boring to always add the port to the URL. Quickly I decided to redirect port 80 to port 8080, and make my life easier.
Not sure how much work that is in the openHAB settings, I settled with redirecting or forwarding the port 80 to port 8080.
There are a few options: use a proxy, install a full flavored webserver and use that as a proxy, use a tool which generates a "301" or "302" redirect on port 80, or use a tool which redirects the traffic itself.
The small utility "redir" seemed like a good start, and the operating system already has a package for that. All I have to do is install the package, and create a service file for systemd *sigh*
Let's start with the package installation: that's an easy task using Ansible. I decided to move everything into a separate role named "redir", this way I can just pull in the role. The directory structure looks like this:
Playbook Directory
-> roles/
-> roles/redir/
-> roles/redir/tasks/
-> roles/redir/templates/
The "tasks" directory holds a main.yml file which has all required installation instructions. The "templates" directory has the template for the service file: redir.service. The main.yml file:
---
- name: redir packages
apt:
name: "{{ item }}"
state: present
with_items:
- redir
- name: Install systemd service file
copy:
src: "{{ role_path }}/templates/redir.service"
dest: /etc/systemd/system/redir.service
register: systemd_redir_service_file
- name: Enable redir service
systemd:
name: redir
enabled: yes
state: started
- name: Restart redir service
service:
name: redir
state: restarted
when: systemd_redir_service_file.changed
Three steps:
- Install the "redir" package
- Copy the template file to the remote host, into the systemd service directory
- Restart the service, in case something changed
And finally, the service file:
[Unit]
Description=redir service for redirecting port 80 to 8080
After=syslog.target network.target openhab2.service
[Service]
ExecStart=/usr/bin/redir :80 :8080
ExecStop=/usr/bin/killall -15 redir
Restart=always
Type=forking
User=root
Group=root
RestartSec=2
WorkingDirectory=/tmp
[Install]
WantedBy=multi-user.target
The service itself is started using this line:
ExecStart=/usr/bin/redir :80 :8080
This tells "redir" to redirect all traffic from port 80 to port 8080. Because the port 80 is a privileged port, the service must be started as root.
Now all I have to do is adding the role in my main Playbook:
---
- hosts: all
become: yes
roles:
- redir
tasks:
Comments
Display comments as Linear | Threaded