Nginx Dynamic and Static Content Separation: Speed Up and Stabilize Your Website Loading

Nginx static-dynamic separation separates static resources (images, CSS, JS, etc.) from dynamic resources (PHP, APIs, etc.). Nginx focuses on quickly returning static resources, while backend servers handle dynamic requests. This approach can improve page loading speed, reduce backend pressure, and enhance scalability (static resources can be deployed on CDNs, and dynamic requests can use load balancing). The core of implementation is distinguishing requests using Nginx's `location` directive: static resources (e.g., `.jpg`, `.js`) are directly returned using the `root` directive with specified paths; dynamic requests (e.g., `.php`) are forwarded to the backend (e.g., PHP-FPM) via `fastcgi_pass` or similar. In practice, within the `server` block of the Nginx configuration file, use `~*` to match static suffixes and set paths, and `~` to match dynamic requests and forward them to the backend. After verification, restart Nginx to apply the changes and optimize website performance.

Read More
Nginx Static Resource Service: Rapid Setup for Image/File Access

Nginx is suitable for hosting static resources such as images and CSS due to its high performance, lightness, stability, and strong concurrency capabilities, which enhances access speed and saves server resources. For installation, run `sudo apt install nginx` on Ubuntu/Debian and `sudo yum install nginx` on CentOS/RHEL. After startup, access `localhost` to verify. For core configuration, create `static.conf` in `/etc/nginx/conf.d/`. Example: Listen on port 80, use `location` to match paths (e.g., `/images/` and `/files/`), specify the resource root directory with `root`, and enable directory browsing with `autoindex on` (with options to set size and time display). During testing, create `images` and `files` directories under `/var/www/static`, place files in them, run `nginx -t` to check configuration, and reload Nginx with `systemctl reload nginx` to apply changes. Then test access via `localhost/images/xxx.jpg` or `localhost/files/xxx.pdf`. Key considerations include Nginx user permissions and configuration reload effectiveness. Setting up Nginx for static resource service is simple, with core configuration paths and directory browsing functionality, ideal for rapid static resource hosting. It can be extended with features like image compression and anti-leeching.

Read More
Beginner's Guide to Nginx: From Installation to Reverse Proxy Configuration

Nginx is a high-performance HTTP and reverse proxy server, lightweight and stable, suitable for scenarios such as website building and load balancing. Installation is divided into Ubuntu/Debian (`sudo apt install nginx`) and CentOS/RHEL (`sudo yum install nginx`). Verification is done with `nginx -v`. Start the service (`sudo systemctl start nginx`) and set it to start automatically (`sudo systemctl enable nginx`). Management commands include start/stop, restart, and reload configuration (`reload`). For core reverse proxy configuration: Create a site configuration file (e.g., `myapp.conf`) in `/etc/nginx/conf.d/`. Example configuration: The `server` listens on port 80, `server_name` is set to the domain name/IP, `location /` forwards to the backend port (e.g., `127.0.0.1:3000`) via `proxy_pass`, and passes Host and real IP through `proxy_set_header`. After configuration, check syntax with `nginx -t`, and `reload` to apply changes, then test access to the backend content. Notes: Open ports 80/443 in the firewall, ensure the backend service is running, and `proxy_pass` must start with `http://`/`https://`.

Read More