|
| 1 | +--- |
| 2 | +title: "Nginx Configuration Basics" |
| 3 | +sidebar_label: "3. Config Basics" |
| 4 | +sidebar_position: 3 |
| 5 | +description: "Master the anatomy of Nginx configuration files, server blocks, and location directives." |
| 6 | +--- |
| 7 | + |
| 8 | +Nginx doesn't have a graphical interface. Everything you want it to do—from hosting a **Docusaurus** site to securing a **MERN** API—is defined in simple text files. |
| 9 | + |
| 10 | +At **CodeHarborHub**, we follow the "Industrial Standard" of modular configuration. Instead of one giant file, we break our settings into smaller, manageable pieces. |
| 11 | + |
| 12 | +:::info Why Modular Configuration? |
| 13 | +This approach allows us to: |
| 14 | + |
| 15 | +1. **Organize by Purpose:** Keep frontend and backend settings separate. |
| 16 | +2. **Enable/Disable Easily:** Just move a file from `sites-available` to `sites-enabled` to activate it. |
| 17 | +3. **Collaborate:** Multiple developers can work on different configs without conflicts. |
| 18 | +::: |
| 19 | + |
| 20 | +## The File Hierarchy |
| 21 | + |
| 22 | +On a Linux server (Ubuntu), Nginx stores its files in `/etc/nginx/`. Here is the structure you need to know: |
| 23 | + |
| 24 | +| File/Folder | Purpose | |
| 25 | +| :--- | :--- | |
| 26 | +| **`nginx.conf`** | The main "Global" configuration file. | |
| 27 | +| **`sites-available/`** | Where you store individual website configs (The "Drafts"). | |
| 28 | +| **`sites-enabled/`** | Where "Active" website configs live (The "Live" versions). | |
| 29 | +| **`conf.d/`** | General configuration snippets included by the main file. | |
| 30 | + |
| 31 | +## The Anatomy of a Config File |
| 32 | + |
| 33 | +Nginx uses a **Block-based** syntax. It looks very similar to CSS or JSON. A block is defined by curly braces `{ }`. |
| 34 | + |
| 35 | +### 1. The Directives |
| 36 | +A **Directive** is a single instruction. It ends with a semicolon `;`. |
| 37 | +* *Example:* `listen 80;` |
| 38 | + |
| 39 | +### 2. The Context (Blocks) |
| 40 | +A **Context** is a group of directives. |
| 41 | +* **Main:** The global settings (outside any braces). |
| 42 | +* **Events:** Connection handling settings. |
| 43 | +* **Http:** Settings for all web traffic. |
| 44 | +* **Server:** Settings for a specific website (Virtual Host). |
| 45 | +* **Location:** Settings for a specific URL path. |
| 46 | + |
| 47 | +## Writing a Basic Server Block |
| 48 | + |
| 49 | +This is the most common task for a **CodeHarborHub** developer. Below is a professional template for hosting a static site (like your Docusaurus build). |
| 50 | + |
| 51 | +```nginx |
| 52 | +server { |
| 53 | + listen 80; # Listen on Port 80 (HTTP) |
| 54 | + server_name codeharborhub.com; # Your Domain Name |
| 55 | +
|
| 56 | + # Where the website files are stored |
| 57 | + root /var/www/codeharborhub/build; |
| 58 | +
|
| 59 | + # The default file to load |
| 60 | + index index.html; |
| 61 | +
|
| 62 | + # URL Handling |
| 63 | + location / { |
| 64 | + # Check if the file exists, if not, load index.html |
| 65 | + # (Crucial for React/Docusaurus routing!) |
| 66 | + try_files $uri $uri/ /index.html; |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## How Nginx Processes a Request |
| 72 | + |
| 73 | +When a user visits `codeharborhub.com/docs`, Nginx follows a logical "Decision Tree" to find the right file. |
| 74 | + |
| 75 | +```mermaid |
| 76 | +graph TD |
| 77 | + A[Request: /docs] --> B{Matches server_name?} |
| 78 | + B -- Yes --> C{Find matching 'location'} |
| 79 | + C -- "location /docs" --> D[Check root folder] |
| 80 | + D --> E[Serve index.html] |
| 81 | + B -- No --> F[Serve Default Server Block] |
| 82 | +``` |
| 83 | + |
| 84 | +## Essential Directives to Memorize |
| 85 | + |
| 86 | +<Tabs> |
| 87 | +<TabItem value="server" label="Server Directives" default> |
| 88 | + |
| 89 | + * **`listen`**: Defines the port (80 for HTTP, 443 for HTTPS). |
| 90 | + |
| 91 | + * **`server_name`**: Defines the domain (e.g., `api.example.com`). |
| 92 | + |
| 93 | + * **`error_page`**: Defines custom pages for 404 or 500 errors. |
| 94 | + |
| 95 | +</TabItem> |
| 96 | +<TabItem value="location" label="Location Directives"> |
| 97 | + |
| 98 | + * **`proxy_pass`**: Forwards the request to another server (Node.js). |
| 99 | + |
| 100 | + * **`root`**: The base folder for searching files. |
| 101 | + |
| 102 | + * **`alias`**: Defines a replacement for the path. |
| 103 | + |
| 104 | + * **`return`**: Used for redirects (e.g., redirecting HTTP to HTTPS). |
| 105 | + |
| 106 | +</TabItem> |
| 107 | + |
| 108 | +</Tabs> |
| 109 | + |
| 110 | +## Best Practices for CodeHarborHub Learners |
| 111 | + |
| 112 | +1. **Always Test Your Config:** Before restarting Nginx, run this command to check for syntax errors: |
| 113 | + |
| 114 | + ```bash |
| 115 | + sudo nginx -t |
| 116 | + ``` |
| 117 | + |
| 118 | + *If it says "syntax is ok," you are safe to reload\!* |
| 119 | + |
| 120 | +2. **Use Reload, Not Restart:** |
| 121 | + |
| 122 | + * **Restart:** Stops the server and starts it again (brief downtime). |
| 123 | + * **Reload:** Applies changes without stopping the server (zero downtime). |
| 124 | + |
| 125 | + ```bash |
| 126 | + sudo systemctl reload nginx |
| 127 | + ``` |
| 128 | + |
| 129 | +3. **Permissions:** Ensure the `nginx` user has permission to read your `/var/www/` folders. |
| 130 | + |
| 131 | + |
| 132 | +:::tip Tip for Local Development |
| 133 | +When testing Nginx locally, you can use `localhost` or `127.0.0.1`. If you want to test with a custom domain (like `codeharborhub.local`), add this line to your `/etc/hosts` file: |
| 134 | + |
| 135 | +``` |
| 136 | +127.0.0.1 codeharborhub.local |
| 137 | +``` |
| 138 | + |
| 139 | +This way, you can mimic a real domain environment on your local machine. |
| 140 | +::: |
0 commit comments