Nginx · December 18, 2023

Nginx Command: server_name

Nginx Command: server_name

Nginx is a popular web server that is known for its high performance, scalability, and flexibility. It is widely used to serve static content, reverse proxy, and load balance web applications. One of the key features of Nginx is its ability to handle multiple websites or domains on a single server using the server_name directive.

What is server_name?

The server_name directive in Nginx is used to define the domain names or hostnames that the server should respond to. It allows you to configure multiple virtual hosts or server blocks on a single Nginx server, each serving a different website or application.

The server_name directive can accept multiple values, separated by spaces. It supports various matching patterns, including exact matches, wildcard matches, and regular expressions. This flexibility allows you to define complex server configurations based on your specific requirements.

Usage Examples

Let's take a look at some examples to understand how the server_name directive works:

Exact Match

If you want Nginx to respond to a specific domain name, you can use an exact match. For example:

server {
    listen 80;
    server_name example.com;
    ...
}

In this example, Nginx will only respond to requests for example.com and ignore requests for any other domain.

Wildcard Match

If you want Nginx to respond to multiple subdomains of a domain, you can use a wildcard match. For example:

server {
    listen 80;
    server_name *.example.com;
    ...
}

In this example, Nginx will respond to requests for any subdomain of example.com, such as www.example.com, blog.example.com, or api.example.com.

Regular Expression Match

If you need more advanced matching capabilities, you can use regular expressions with the server_name directive. For example:

server {
    listen 80;
    server_name ~^(www.)?example.com$;
    ...
}

In this example, Nginx will respond to requests for both example.com and www.example.com using a regular expression pattern.

Conclusion

The server_name directive in Nginx is a powerful tool for configuring virtual hosts and handling multiple websites or domains on a single server. It allows you to define complex server configurations based on domain names, subdomains, or even regular expressions. By leveraging the flexibility of the server_name directive, you can efficiently manage and serve multiple websites or applications with ease.

Summary

In summary, the server_name directive in Nginx is used to define the domain names or hostnames that the server should respond to. It allows you to configure multiple virtual hosts or server blocks on a single Nginx server, each serving a different website or application. The server_name directive supports various matching patterns, including exact matches, wildcard matches, and regular expressions. By utilizing the flexibility of the server_name directive, you can efficiently manage and serve multiple websites or applications on your Nginx server.

For more information about VPS hosting and to experience the power of Nginx, check out Server.HK.