Home  >  Article  >  Operation and Maintenance  >  How Nginx implements domain name-based virtual host configuration

How Nginx implements domain name-based virtual host configuration

PHPz
PHPzOriginal
2023-11-08 20:05:19994browse

How Nginx implements domain name-based virtual host configuration

How Nginx implements domain name-based virtual host configuration requires specific code examples

Nginx is a high-performance HTTP and reverse proxy server that provides flexible Configuration options to implement domain name-based virtual host configuration. In this article, we will introduce in detail how to configure Nginx to implement domain name-based virtual hosting and provide specific code examples.

First, we need to add the virtual host configuration block to the Nginx configuration file. On Ubuntu systems, the default configuration file path is /etc/nginx/nginx.conf. Please open the file with a text editor and add the following code to it:

http {
    server {
        listen 80;
        server_name example.com;
        root /var/www/example.com/html;
        index index.html;
    }
    
    server {
        listen 80;
        server_name example.org;
        root /var/www/example.org/html;
        index index.html;
    }
    
    # 添加更多的虚拟主机配置块...
}

In the above code, we have defined two virtual hosts: example.com and example.org. Each virtual host configuration block contains the following key configuration items:

  • listen: Specifies the port number for the virtual host to listen to. Here we use port 80.
  • server_name: Specify the domain name or IP address corresponding to the virtual host. In the above example, we used example.com and example.org respectively.
  • root: Specify the root directory of the virtual host. Here we assume that the web page files of each virtual host are stored in different directories.
  • index: Specify the default home page file of the virtual host.

Next, we need to ensure that the web file directory of the virtual host exists and has the correct file permissions. In the above example, we assume that the web page file directory of example.com is /var/www/example.com/html, and the web page file directory of example.org is /var/www/example.org/html. Please create these directories according to actual conditions and ensure that Nginx has access permissions.

After completing the above configuration, save and exit the Nginx configuration file. Then, reload the Nginx configuration file using the following command:

sudo nginx -s reload

Now, we have completed the domain name-based virtual host configuration. When a user accesses example.com or example.org through a browser, Nginx will select the corresponding virtual host for processing based on the requested domain name.

If you have more domain names that need to be configured with virtual hosts, please follow the above example to add more virtual host configuration blocks, and modify the server_name and root configuration items accordingly.

Finally, we provide the following common Nginx configuration instructions to further optimize domain name-based virtual host configuration:

  • access_log: used to determine whether access is generated Logs and the location where the logs are stored.
  • error_log: Used to determine whether to generate an error log and where the log is stored.
  • location: Used to further customize the behavior of the virtual host, such as configuring URL rewriting rules or reverse proxy.

By flexibly using the above instructions, you can configure and optimize the virtual host more precisely.

Summary: This article introduces in detail how to use Nginx to implement domain name-based virtual host configuration, and provides specific code examples. By configuring Nginx's virtual host according to the example, we can easily host different website content for multiple domain names and provide a better user experience. I hope this article will be helpful to students learning Nginx.

The above is the detailed content of How Nginx implements domain name-based virtual host configuration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn