Skip to content

Commit

Permalink
added few blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
SauravDharwadkar authored May 22, 2024
1 parent 67c725c commit 941f3dc
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 0 deletions.
99 changes: 99 additions & 0 deletions _posts/2024-05-15-update-hosts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Leveraging the Hosts File: A Handy method for testing dns locally

The hosts file is a powerful yet often overlooked tool that can significantly enhance your computing experience. Whether you're a web developer, a system administrator, or just someone looking to block distracting websites, understanding how to use the hosts file can be incredibly beneficial. Here's why you might want to add entries to your hosts file, followed by a step-by-step guide on how to do it for various operating systems.

#### Advantages of Using the Hosts File

1. **Local Development and Testing:**
- When developing a website, you can map your development environment to a domain name. This allows you to access your site via a user-friendly URL like `yoursite.com` instead of typing `localhost` or an IP address.

2. **Bypassing DNS for Speed and Reliability:**
- The hosts file allows you to bypass the Domain Name System (DNS), which can speed up access to frequently visited websites. This can be particularly useful in environments with unreliable DNS servers.

3. **Blocking Unwanted Content:**
- You can use the hosts file to block distracting or malicious websites by mapping them to `127.0.0.1` (your local machine). This effectively prevents your browser from loading these sites.

4. **Network Testing and Troubleshooting:**
- System administrators can test changes in network configurations without affecting the global DNS, providing a controlled environment for troubleshooting.

Now that we understand the benefits, let's dive into how you can edit the hosts file on different operating systems to map `yoursite.com` to `127.0.0.1`.

### Editing the Hosts File: A Step-by-Step Guide

#### Windows

1. **Open Notepad as Administrator:**
- Press the `Start` button.
- Type `Notepad`.
- Right-click `Notepad` and select `Run as administrator`.

2. **Open the Hosts File:**
- In Notepad, go to `File` > `Open`.
- Navigate to `C:\Windows\System32\drivers\etc`.
- Select `All Files` in the file type dropdown (so you can see the hosts file).
- Open the `hosts` file.

3. **Edit the Hosts File:**
- Add the line `127.0.0.1 yoursite.com` at the end of the file.
- Save the file.

#### macOS

1. **Open Terminal:**
- Press `Command + Space` to open Spotlight.
- Type `Terminal` and press `Enter`.

2. **Edit the Hosts File:**
- Type `sudo nano /etc/hosts` and press `Enter`.
- Enter your password when prompted.

3. **Add the Entry:**
- Use the arrow keys to navigate to the end of the file.
- Add the line `127.0.0.1 yoursite.com`.
- Press `Control + O` to save the file, then `Control + X` to exit.

#### Linux

1. **Open Terminal:**

2. **Edit the Hosts File:**
- Type `sudo nano /etc/hosts` and press `Enter`.
- Enter your password if prompted.

3. **Add the Entry:**
- Use the arrow keys to navigate to the end of the file.
- Add the line `127.0.0.1 yoursite.com`.
- Press `Control + O` to save the file, then `Control + X` to exit.

#### NixOS

1. **Open Terminal:**

2. **Edit the Hosts Configuration:**
- NixOS typically manages system configuration declaratively. You can add the entry to the `configuration.nix` file.
- Open the `configuration.nix` file, usually located at `/etc/nixos/configuration.nix`, with your preferred text editor. For example:
```bash
sudo nano /etc/nixos/configuration.nix
```

3. **Add the Hosts Entry:**
- Locate the `networking.hosts` section, or add it if it doesn't exist:
```nix
networking.hosts = {
"127.0.0.1" = [ "yoursite.com" ];
};
```
- Save the file and exit the editor.
4. **Apply the Configuration:**
- Run `sudo nixos-rebuild switch` to apply the changes.
### Verification
For all OS:
- **Verify the Change:**
- Open a command prompt or terminal.
- Run `ping yoursite.com`.
- You should see that `yoursite.com` is resolving to `127.0.0.1`.
By following these steps, you can effectively use your hosts file for improved local development, faster access to websites, and enhanced control over your browsing experience.
114 changes: 114 additions & 0 deletions _posts/2024-05-21-nginx-basics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Understanding Nginx

## What is Nginx?

Nginx (pronounced "engine X") is an open-source, high-performance HTTP server, reverse proxy server, and mail proxy server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

## Why Use Nginx?

Nginx is widely used for several reasons:

1. **Performance**: It is designed to handle high concurrency with low memory usage.
2. **Stability**: Nginx is known for its robustness and stability, even under heavy load.
3. **Flexibility**: It can function as a web server, reverse proxy, load balancer, and HTTP cache.
4. **Scalability**: Easily handles large numbers of connections simultaneously, making it ideal for high-traffic websites.
5. **Security**: Offers security features like SSL/TLS support, rate limiting, and access control.

## Advantages of Nginx

- **High Concurrency**: Efficiently manages thousands of concurrent connections.
- **Low Memory Usage**: Optimized to consume minimal system resources.
- **Fast Static Content Delivery**: Excellent at serving static files quickly.
- **Load Balancing**: Distributes incoming traffic across multiple servers to ensure reliability and uptime.
- **Reverse Proxy**: Acts as an intermediary to distribute traffic to multiple backend servers.
- **HTTP/2 and SSL/TLS**: Supports modern web standards for enhanced performance and security.

## How to Use Nginx

### Installation

To install Nginx on a Ubuntu-based system, use the following commands:

```bash
sudo apt update
sudo apt install nginx
```

### Basic Configuration

The main configuration file for Nginx is located at `/etc/nginx/nginx.conf`. A basic server block setup might look like this:

```nginx
server {
listen 80;
server_name yoursite.com;
location / {
root /var/www/html;
index index.html;
}
}
```

This configuration sets Nginx to listen on port 80 for the domain `yoursite.com`, serving files from the `/var/www/html` directory.

### Starting Nginx

To start and enable Nginx to run on boot, use the following systemctl commands:

```bash
sudo systemctl start nginx
sudo systemctl enable nginx
```

### Running Nginx with Docker

Using Docker to run Nginx allows for easy setup and portability. Here's a step-by-step guide:

1. **Pull the Nginx Docker Image**:

```bash
docker pull nginx
```

2. **Run Nginx in a Docker Container**:

```bash
docker run --name my-nginx -p 80:80 -d nginx
```

This command starts a Docker container named `my-nginx`, mapping the container's port 80 to the host's port 80.

3. **Serving Custom Content**:

Create a directory for your website content:

```bash
mkdir ~/my-website
echo "<h1>Hello, Docker!</h1>" > ~/my-website/index.html
```

Run the Nginx container with a volume mapping to serve the custom content:

```bash
docker run --name my-nginx -p 80:80 -v ~/my-website:/usr/share/nginx/html:ro -d nginx
```

This maps the `~/my-website` directory on the host to the `/usr/share/nginx/html` directory in the container, making your custom content accessible via Nginx.


![Nginx Basics](/assets/images/nginx-basic.svg)



### Diagram Explanation

1. **Client**: The user who sends a request to the web server.
2. **Nginx**: Acts as the first point of contact, handling the client request.
3. **Web Server**: Nginx serves static content directly or forwards requests to a load balancer.
4. **Load Balancer**: Distributes incoming requests to multiple application servers.
5. **App Server1 and App Server2**: Backend servers that process the client requests and return the necessary data.

## Conclusion

Nginx is a versatile and powerful tool in the web server domain. Its ability to handle a large number of simultaneous connections with minimal resource usage makes it an ideal choice for modern web applications. Whether you need a simple web server, a reverse proxy, or a load balancer, Nginx can be configured to meet your needs with ease. Running Nginx with Docker further enhances its portability and ease of deployment, making it a convenient choice for developers and system administrators alike.
115 changes: 115 additions & 0 deletions _posts/2024-05-21-traefik-basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@


## Traefik Overview ( WIP )

Traefik is a reverse proxy and load balancer designed for microservices. It integrates seamlessly with various orchestration tools like Docker, Kubernetes, and others, automatically detecting services and routing traffic to them based on configured rules. Traefik supports various protocols including HTTP, HTTPS, and TCP, and offers features such as SSL termination, load balancing, and more.

### How Traefik Works

1. **Service Discovery**: Traefik can automatically discover services using providers like Docker. It listens for changes in the service registry and updates its routing configuration accordingly.
2

## Traefik Overview

Traefik is a modern reverse proxy and load balancer designed for microservices. It can integrate seamlessly with various orchestrators such as Docker, Kubernetes, and others. It automatically discovers services and routes traffic to them based on configured rules. Traefik supports multiple protocols, including HTTP, HTTPS, and TCP, and offers features like SSL termination, load balancing, and more.

### How Traefik Works

1. **Service Discovery**: Traefik automatically discovers services using various providers like Docker, Kubernetes, etc. It listens for changes in the service registry and updates its routing configuration dynamically.
2. **Routing**: Based on defined rules, Traefik routes incoming requests to the appropriate backend services. These rules can be configured using labels in Docker, annotations in Kubernetes, or through a configuration file.
3. **Middlewares**: Traefik uses middlewares to modify requests before they reach your services. Common middlewares include authentication, rate limiting, and request rewriting.
4. **Load Balancing**: Traefik can distribute requests across multiple instances of a service, balancing the load and providing high availability.

## Using Traefik with Docker Compose

To use Traefik with Docker Compose, follow these steps:

1. **Create a Docker Compose File**: Define your services and add Traefik as a reverse proxy.

2. **Add Traefik Labels**: Use labels to configure Traefik for your services.

### Example Docker Compose File

Here’s a detailed example:

```yaml
version: '3.7'

services:
traefik:
image: traefik:v2.9
command:
- "--api.insecure=true" # Enables the Traefik dashboard on port 8080
- "--providers.docker=true" # Enables Docker as a provider
- "--entrypoints.web.address=:80" # Defines an entrypoint on port 80
ports:
- "80:80" # HTTP
- "8080:8080" # Traefik Dashboard
volumes:
- "/var/run/docker.sock:/var/run/docker.sock" # Allows Traefik to access Docker

whoami:
image: containous/whoami # A simple web server that returns information about the request
labels:
- "traefik.enable=true" # Enable Traefik for this container
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)" # Routing rule
- "traefik.http.services.whoami.loadbalancer.server.port=80" # Service port
```
### Explanation of Labels
#### Basic Labels
- **traefik.enable=true**: This label enables Traefik for the specific container.
- **traefik.http.routers.[router-name].rule=Host(`your.domain.com`)**: Defines a routing rule. Replace `[router-name]` with a unique name and `your.domain.com` with the desired domain.
- **traefik.http.services.[service-name].loadbalancer.server.port=[port]**: Specifies the port Traefik should use to route traffic to the container. Replace `[service-name]` with a unique name and `[port]` with the container's exposed port.

#### Advanced Labels
- **traefik.http.middlewares.[middleware-name].basicauth.users=[user:password]**: Configures basic authentication. Replace `[middleware-name]` with a unique name and `[user:password]` with a valid username and password in htpasswd format.
- **traefik.http.routers.[router-name].middlewares=[middleware-name]**: Attaches a middleware to a router. Replace `[router-name]` and `[middleware-name]` with the appropriate names.

### Complete Example with Middleware

```yaml
version: '3.7'
services:
traefik:
image: traefik:v2.9
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
myservice:
image: myimage
labels:
- "traefik.enable=true"
- "traefik.http.routers.myservice.rule=Host(`myservice.localhost`)"
- "traefik.http.services.myservice.loadbalancer.server.port=80"
- "traefik.http.middlewares.myservice-auth.basicauth.users=user:$$apr1$$DyeYjZnA$$K9gOZH5z0QbJYOeDPOw1g/" # Basic auth user:password
- "traefik.http.routers.myservice.middlewares=myservice-auth"
```
### Explanation
- **traefik** service:
- Runs Traefik with the Docker provider enabled.
- Exposes port 80 for HTTP traffic and port 8080 for the Traefik dashboard.
- Mounts the Docker socket to allow Traefik to detect other containers.
- **myservice**:
- A custom service with Traefik enabled.
- Routes requests with the hostname `myservice.localhost` to this service.
- Configures basic authentication using middleware.

### Summary

- **Traefik Labels**: Used to configure how Traefik handles traffic for each container.
- **Basic Configuration**: Includes enabling Traefik, defining routers, and specifying service ports.
- **Advanced Configuration**: Includes setting up middlewares for additional functionalities like authentication.

Loading

0 comments on commit 941f3dc

Please sign in to comment.