Nginx Port and Domain Binding: Easily Achieve Domain Access to the Server

This article explains how to bind ports and domains in Nginx to achieve hosting multiple websites/services on a single server. The core is to distinguish different sites by "port + domain name". Nginx configures virtual hosts through the `server` block, with key directives including `listen` (port), `server_name` (domain name), `root` (file path), and `index` (home page). Prerequisites: The server needs Nginx installed, the domain name should be registered and resolved to a public IP, and the server should be tested to be accessible. Practical cases are divided into two scenarios: 1. The same domain name with different ports (e.g., binding 80 and 443 ports for `www.myblog.com`, with HTTPS certificate required for the latter); 2. Different domain names with different ports (e.g., `www.myblog.com` using port 80, `blog.myblog.com` using port 8080). Configuration files are stored in `/etc/nginx/conf.d/`, and examples should include `listen` and `server_name`. Verification: Execute `nginx -t` to check syntax, use `systemctl restart nginx` to apply changes, and verify access via a browser. Common issues: Configuration errors (check syntax), unapplied domain resolution (wait for DNS or use `nslookup`), port conflicts (change port or ...).

Read More
Nginx Beginner's Guide: Configuring an Accessible Web Server

### A Beginner's Guide to Nginx Nginx is a high-performance, lightweight web server/reverse proxy, ideal for high-concurrency scenarios. It features low resource consumption, flexible configuration, and ease of use. **Installation**: On mainstream Linux systems (Ubuntu/Debian/CentOS/RHEL), install via `apt` or `dnf`. Start and enable Nginx with `systemctl start/ enable nginx`, then verify with `systemctl status nginx` or by accessing the server's IP address. **Core Configuration**: Configuration files are located in `/etc/nginx/`, where `nginx.conf` is the main configuration file and `conf.d/` stores virtual host configurations. Create a website directory (e.g., `/var/www/html`), write an `index.html` file, and add a `server` block in `conf.d/` (specifying port 80 listening and the website directory). **Testing & Management**: After modifying configurations, use `nginx -t` to check syntax and `systemctl reload` to apply changes. Ensure port 80 is open (firewall settings) and file permissions are correct for testing access. Common commands include `start/stop/restart/reload nginx` and status checks. **Summary**

Read More
Nginx Virtual Hosts: Deploying Multiple Websites on a Single Server

This article introduces the Nginx virtual host feature, which allows a single server to host multiple websites, thereby reducing costs. The core is to simulate multiple virtual servers through technology. There are three implementation methods in Nginx: domain name-based (the most common, where different domains correspond to different websites), port-based (distinguished by different ports, suitable for scenarios without additional domains), and IP-based (for servers with multiple IPs, where different IPs correspond to different websites). Before configuration, Nginx needs to be installed, website content prepared (e.g., directories `/var/www/site1` and `/var/www/site2` with homepages), and domain name resolution or test domains (optional) should be ensured. Taking the domain name-based method as an example, the steps are: create the configuration file `/etc/nginx/sites-available/site1.com`, write a `server` block (listening on port 80, matching the domain name, specifying the root directory), configure the second website similarly, create a soft link to `sites-enabled`, test with `nginx -t`, and restart Nginx. For other methods: the port-based method requires specifying a different port (e.g., 8080) in the `server` block; the IP-based method requires the server to bind multiple IPs, with the `listen` directive in the configuration file specifying the IP and port. Common issues include permissions, configuration errors, and domain name resolution, which require checking directory permissions, syntax, and confirming that the domain name points to the server's IP. In summary, Nginx's virtual host feature is a cost-effective solution for hosting multiple websites on a single server, with flexible configuration options based on domain names, ports, or IPs to meet various deployment needs.

Read More
Detailed Explanation of Nginx Configuration Files: Server Block and Location for Beginners

The core of Nginx configuration lies in Server blocks (virtual hosts) and location blocks (path distribution). The main configuration file (nginx.conf) includes the global context (with directives like worker_processes), the events context (with worker_connections), and the http context (which contains multiple Server blocks). A Server block defines a website using directives such as listen (port), server_name (domain name), root (root directory), and index (homepage). Location blocks match requests based on paths, supporting prefix, exact, regular expression, and other types, with priority order: exact match > prefix with ^~ > ordinary prefix > regular expression > default. After configuration, use `nginx -t` to verify syntax and `nginx -s reload` to apply changes. After mastering basic configurations (port, domain name, static path), beginners can progressively learn advanced features like dynamic request forwarding and caching.

Read More