Compare commits

...

3 Commits

Author SHA1 Message Date
georg.sinn-schirwitz
646194555c feat: add python scripts 2024-11-03 12:45:38 +01:00
georg.sinn-schirwitz
4772c708f1 fix: adjust IP address range 2024-10-27 15:06:57 +01:00
georg.sinn-schirwitz
6572d2bda7 feat: add reverse proxy setup with nginx
docs: add to readme
2024-10-27 15:03:52 +01:00
5 changed files with 368 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ This repository is a collection of ideas for courses.
## Ideas ## Ideas
- [Reverse Proxy Setup with Nginx](ideas/reverse-proxy-setup-with-nginx.md)
## Courses ## Courses
### Beginner ### Beginner

View File

@@ -0,0 +1,197 @@
# Reverse Proxy Setup with NGINX and BIND on Alpine Linux
This setup should give you a basic reverse proxy configuration using NGINX and a DNS server using BIND on Alpine Linux.
## Table of Contents
- [Reverse Proxy Setup with NGINX and BIND on Alpine Linux](#reverse-proxy-setup-with-nginx-and-bind-on-alpine-linux)
- [Table of Contents](#table-of-contents)
- [Introduction](#introduction)
- [Prerequisites](#prerequisites)
- [Steps](#steps)
- [Step 1: Install Required Packages](#step-1-install-required-packages)
- [Step 2: Configure BIND DNS Server](#step-2-configure-bind-dns-server)
- [Edit the BIND Configuration](#edit-the-bind-configuration)
- [Define Your Zone](#define-your-zone)
- [Create Zone File](#create-zone-file)
- [Start BIND](#start-bind)
- [Step 3: Configure NGINX as a Reverse Proxy](#step-3-configure-nginx-as-a-reverse-proxy)
- [Edit NGINX Main Configuration](#edit-nginx-main-configuration)
- [Create Server Block Configuration](#create-server-block-configuration)
- [Set the Webroot Directory](#set-the-webroot-directory)
- [Start NGINX](#start-nginx)
- [Step 4: Adjust Firewall Rules (if necessary)](#step-4-adjust-firewall-rules-if-necessary)
- [Summary of Important Configuration File Locations](#summary-of-important-configuration-file-locations)
- [Final Notes](#final-notes)
## Introduction
Setting up a reverse proxy with NGINX as the HTTP server and BIND as the DNS server on Alpine Linux involves several steps. Below is an outline of the setup, including important configuration file locations.
## Prerequisites
- Alpine Linux installed
- Root access or sudo privileges
## Steps
| # | Step | Description |
| --- | ---------------------------------- | ------------------------------------------------- |
| 1 | Install Required Packages | Install NGINX and BIND |
| 2 | Configure BIND DNS Server | Edit BIND configuration and create zone file |
| 3 | Configure NGINX as a Reverse Proxy | Create a server block configuration for your site |
| 4 | Adjust Firewall Rules | Allow traffic on ports 80 (HTTP) and 53 (DNS) |
### Step 1: Install Required Packages
First, you need to install NGINX and BIND. Open your terminal and run the following command:
```bash
apk add nginx bind
```
### Step 2: Configure BIND DNS Server
BIND will serve as your DNS server. The main configuration file is usually located at `/etc/bind/named.conf`.
#### Edit the BIND Configuration
```bash
vi /etc/bind/named.conf
```
#### Define Your Zone
Add a zone definition for your domain. For example:
```bash
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};
```
#### Create Zone File
Create a zone file for your domain:
```bash
vi /etc/bind/db.example.com
```
Sample content for `db.example.com`:
```dns
$TTL 86400
@ IN SOA ns.example.com. admin.example.com. (
2024102701 ; Serial
3600 ; Refresh
1800 ; Retry
604800 ; Expire
86400 ) ; Negative Cache TTL
@ IN NS ns.example.com.
ns IN A 10.0.0.1 ; Replace with your server's IP
www IN A 10.0.0.1 ; Replace with your server's IP
```
#### Start BIND
Start the BIND service and enable it to run on boot:
```bash
rc-service named start
rc-update add named
```
### Step 3: Configure NGINX as a Reverse Proxy
NGINX will act as a reverse proxy, forwarding requests to the backend server. The main configuration file for NGINX is located at `/etc/nginx/nginx.conf`, but you should create a specific server block configuration file for your site.
#### Edit NGINX Main Configuration
```bash
vi /etc/nginx/nginx.conf
```
Ensure that the following line is included to load additional configuration files:
```nginx
include /etc/nginx/conf.d/*.conf;
```
#### Create Server Block Configuration
Create a new configuration file for your site:
```bash
vi /etc/nginx/conf.d/example.com.conf
```
Sample configuration:
```nginx
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://10.0.0.2; # Replace with your backend server address
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
#### Set the Webroot Directory
If you want to serve static files, you can set the webroot:
```nginx
location / {
root /www; # Your web root directory
index index.html index.htm;
}
```
#### Start NGINX
Start the NGINX service and enable it to run on boot:
```bash
rc-service nginx start
rc-update add nginx
```
### Step 4: Adjust Firewall Rules (if necessary)
If you are using a firewall, make sure to allow traffic on port 80 (HTTP) and 53 (DNS):
```bash
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
```
## Summary of Important Configuration File Locations
- **BIND Configuration:**
- Main config: `/etc/bind/named.conf`
- Zone file: `/etc/bind/db.example.com`
- **NGINX Configuration:**
- Main config: `/etc/nginx/nginx.conf`
- Site-specific config: `/etc/nginx/conf.d/example.com.conf`
- Webroot: `/www`
## Final Notes
- Ensure to replace `example.com` and `backend_server_address` with your actual domain and backend server address.
- After making changes, you may need to reload BIND and NGINX to apply the new configurations:
```bash
rc-service named reload
rc-service nginx reload
```

64
main.py Normal file
View File

@@ -0,0 +1,64 @@
"""Python application main file for the repository
In this file all python code is referenced and executed that is included in this courses repository.
The main file is used to run the code and to test the functionality of the code.
Functionality of the code is loaded from modules found in folder /src.
"""
import os
import sys
from src import markdown_to_html
from src import file_handler
def main():
"""Main function of the courses module.
This function is the entry point for the courses module.
The function loads the files from the /courses folder and sorts them by file type.
Markdown files are converted to html files and saved in the /html folder.
"""
# Load markdown files from /courses folder
course_files = file_handler.load_files("courses")
markdown_files = file_handler.sort_files(course_files, "md")
html_output_files = file_handler.sort_files(course_files, "html")
# check /html folder
if os.path.exists("html"):
# load html files from /html folder
html_files = file_handler.load_files("html")
# extend converted_files with html files
html_output_files.extend(html_files)
else:
# create /html folder
os.makedirs("html")
for file in html_output_files:
print(f"HTML file: {file}")
name = os.path.splitext(os.path.basename(file))[0]
print(f"Name: {name}")
# move to /html folder
dst_file = f"html/{os.path.basename(file)}"
if not os.path.exists(dst_file):
os.rename(file, dst_file)
print(f"Moved file to /html folder: {dst_file}")
else:
print(f"File already exists in /html folder: {dst_file}")
# get course markdown file name
course_file = f"courses\\{name}.md"
# remove markdown file from list markdown_files
if course_file in markdown_files:
markdown_files.remove(course_file)
print(f"Removed markdown file from list: {course_file}")
# Convert remaining markdown files to html files
markdown_to_html.convert_markdown_files(markdown_files)
if __name__ == "__main__":
main()

57
src/file_handler.py Normal file
View File

@@ -0,0 +1,57 @@
"""File handler module."""
import os
def load_files(folder):
"""Load files from a folder.
This function loads files from a folder.
The function returns a list of files found in the folder.
Args:
folder (str): Folder to load files from.
Returns:
list: List of files found in the folder.
"""
files = []
for file in os.listdir(folder):
files.append(os.path.join(folder, file))
return files
def sort_files(files, file_type):
"""Sort files by file type.
This function sorts files by file type.
The function returns a list of files that match the file type.
Args:
files (list): List of files to sort.
file_type (str): File type to sort by.
Returns:
list: List of files that match the file type.
"""
sorted_files = []
for file in files:
if file.endswith(file_type):
sorted_files.append(file)
return sorted_files
def main():
"""Main function of the file_handler module.
This function is the entry point for the file_handler module.
The function loads files from the /courses folder and sorts them by file type.
The files are then printed to the console.
"""
# Load files from /courses folder
files = load_files("courses")
# Sort files by file type
markdown_files = sort_files(files, "md")
# Print files
print("Markdown files:")
for file in markdown_files:
print(file)
if __name__ == "__main__":
main()

48
src/markdown_to_html.py Normal file
View File

@@ -0,0 +1,48 @@
"""Markdown to HTML conversion module."""
import os
import markdown
from src import file_handler
def convert_markdown_files(markdown_files):
"""Convert markdown files to html files.
This function converts markdown files to html files.
The function uses the markdown module to convert the markdown files.
The html files are saved in the /html folder.
Args:
markdown_files (list): List of markdown files to convert.
"""
# Create /html folder if it does not exist
if not os.path.exists("html"):
os.makedirs("html")
# Convert markdown files to html files
for markdown_file in markdown_files:
# Read markdown file
with open(markdown_file, "r") as file:
markdown_content = file.read()
# Convert markdown to html
html_content = markdown.markdown(markdown_content)
# Save html file
html_file = "html/" + \
os.path.splitext(os.path.basename(markdown_file))[0] + ".html"
with open(html_file, "w") as file:
file.write(html_content)
print(f"Converted {markdown_file} to {html_file}")
def main():
"""Main function of the markdown_to_html module.
This function is the entry point for the markdown_to_html module.
The function loads the markdown files from the /courses folder and converts them to html files.
"""
# Load markdown files from /courses folder
course_files = file_handler.load_files("courses")
markdown_files = file_handler.sort_files(course_files, "md")
print(markdown_files)
if __name__ == "__main__":
main()