OSI Model
The OSI (Open Systems Interconnection) model is a conceptual framework used to understand how different networking protocols interact within a system. It’s divided into seven layers, each with specific functionalities.
Layers of OSI Model:
+----------------------+
| Application Layer | - Layer 7
|----------------------|
| Presentation Layer | - Layer 6
|----------------------|
| Session Layer | - Layer 5
|----------------------|
| Transport Layer | - Layer 4
|----------------------|
| Network Layer | - Layer 3
|----------------------|
| Data Link Layer | - Layer 2
|----------------------|
| Physical Layer | - Layer 1
+----------------------+
- Physical Layer: Manages physical connections (cables, switches).
- Data Link Layer: Handles node-to-node data transfer (MAC addressing).
- Network Layer: Manages packet forwarding, including routing through routers (IP addressing).
- Transport Layer: Ensures reliable data transfer (TCP, UDP).
- Session Layer: Manages sessions (connections between applications).
- Presentation Layer: Translates data into a format applications can understand.
- Application Layer: Interfaces directly with applications (HTTP, SMTP).
TCP/IP Model
TCP/IP (Transmission Control Protocol/Internet Protocol) is the foundational suite for internet communication.
Layers of TCP/IP Model:
+----------------------+
| Application Layer |
|----------------------|
| Transport Layer |
|----------------------|
| Internet Layer |
|----------------------|
| Network Access Layer |
+----------------------+
- Network Access: Combines OSI’s Physical and Data Link layers.
- Internet: Similar to OSI’s Network layer (routing, IP addresses).
- Transport: Ensures data delivery with TCP/UDP.
- Application: Includes protocols like HTTP, FTP, DNS.
Example Code (TCP Socket Connection):
import socket
# Create a socket and connect to server
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('example.com', 80))
client_socket.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
# Receive data
response = client_socket.recv(4096)
print(response.decode())
client_socket.close()
HTTP/HTTPS
HTTP (HyperText Transfer Protocol) is an application layer protocol for communication over the web. HTTPS is the secure version, using TLS (Transport Layer Security) for encryption.
Basic HTTP Request Structure:
GET / HTTP/1.1
Host: example.com
DNS (Domain Name System)
DNS is a protocol for translating human-readable domain names (e.g., example.com) into IP addresses (e.g., 192.0.2.1).
DNS Resolution Process:
- The user enters ‘example.com’ in the browser.
- Browser checks cache, then OS resolver.
- Resolver queries the root DNS server, the TLD server, and the authoritative DNS server.
- The IP address is returned.
- The browser connects to the IP address to access the website.
Load Balancing
Load balancing distributes incoming traffic across multiple servers to ensure availability and reliability.
Common Load Balancing Algorithms:
- Round Robin: Requests are distributed sequentially across servers.
- Least Connections: Routes traffic to the server with the fewest active connections.
- IP Hash: Directs traffic based on client IP.
Client Requests
|
+-------------+
| Load Balancer|
+-------------+
/ | \
Server1 Server2 Server3
Example Load Balancer Configuration (Nginx):
http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}
}
Firewalls, Proxies, and Reverse Proxies
- Firewall: Filters traffic based on security rules, positioned at network edges.
- Proxy: Intermediary between client and server, often used for filtering, caching, or access control.
- Reverse Proxy: Sits in front of web servers, managing requests on behalf of them (e.g., load balancing, caching).
Reverse Proxy Diagram:
Client
|
|
+---------+
| Reverse |
| Proxy |
+---------+
/ \
Server1 Server2
Example Reverse Proxy Configuration (Nginx):
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_server;
}
}