Configuring virtual hosts and domain names on Nginx involves setting up server blocks that define how Nginx should handle requests for different domains or subdomains. Each server block can specify a unique domain name, root directory for serving files, and other configurations.
Here’s a step-by-step guide:
If Nginx is not already installed, you can install it using your package manager. For example, on Ubuntu:
sudo apt update
sudo apt install nginx
Create a new configuration file for your domain in the /etc/nginx/sites-available/ directory. For example, for example.com:
sudo nano /etc/nginx/sites-available/example.com
In the file, define a server block that specifies the domain name, root directory, and other settings. Here’s an example configuration:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Create a symbolic link from the sites-available directory to the sites-enabled directory to enable the configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
Reload Nginx to apply the changes:
sudo systemctl reload nginx
listen 80;: Specifies that Nginx should listen on port 80 for HTTP requests.server_name example.com www.example.com;: Defines the domain names that this server block will respond to.root /var/www/example.com;: Specifies the root directory from which Nginx will serve files for this domain.index index.html index.htm;: Specifies the default index files to serve if no specific file is requested.location / { try_files $uri $uri/ =404; }: Defines how Nginx should handle requests for URLs that don’t correspond to actual files.For hosting your domain and configuring Nginx on a scalable infrastructure, consider using Tencent Cloud’s Virtual Private Cloud (VPC) and Cloud Load Balancer services. These services provide a flexible and reliable environment for deploying and managing your web applications. Additionally, Tencent Cloud’s Cloud DNS service can help you manage your domain names and DNS records efficiently.