Home  >  Article  >  Operation and Maintenance  >  How to use Nginx to implement virtual host configuration based on domain name and path

How to use Nginx to implement virtual host configuration based on domain name and path

PHPz
PHPzOriginal
2023-08-02 13:14:181453browse

How to use Nginx to implement virtual host configuration based on domain name and path

Introduction:
In network application development, it is often necessary to configure multiple virtual hosts to provide different services. Nginx is a high-performance HTTP and reverse proxy server. By using Nginx, we can easily implement virtual host configuration based on domain name and path. This article will introduce how to use Nginx to implement such a configuration and give corresponding code examples.

1. Domain name-based virtual host configuration
In the Nginx configuration file, use the server block to configure the virtual host. In domain name-based virtual host configuration, we can specify the domain name through the server_name directive and set up the corresponding services.

Sample code:

server {

listen 80;
server_name example.com;
root /var/www/example;
index index.html;

location / {
    try_files $uri $uri/ =404;
}

}

In the above example code, a virtual host named example.com is configured. Among them, the listen directive is used to specify the port number that Nginx listens on, and the server_name directive is used to specify the domain name. The root directive is used to specify the root directory of the website, and the index directive is used to set the default homepage file.

2. Path-based virtual host configuration
In addition to domain name-based virtual host configuration, Nginx also supports path-based virtual host configuration. By configuring the location block, we can provide different services based on the access path.

Sample code:

server {

listen 80;
server_name example.com;

location / {
    root /var/www/example1;
    index index.html;
}

location /blog {
    root /var/www/example2;
    index index.php;
}

}

In the above example code, a virtual host named example.com is configured, including Two location blocks. The first location block configures the service of the root path /, the root directive is set to /var/www/example1, and the index directive is set to index.html. The second location block configures the service with the path /blog, the root directive is set to /var/www/example2, and the index directive is set to index.php.

3. Virtual host configuration using domain names and paths at the same time
In actual applications, we usually need to use domain names and paths at the same time to configure multiple virtual hosts. Such a configuration can be achieved by combining the server_name and location directives.

Sample code:

server {

listen 80;
server_name example.com;

location / {
    root /var/www/example1;
    index index.html;
}

}

server {

listen 80;
server_name blog.example.com;

location / {
    root /var/www/example2;
    index index.php;
}

}

In the above example In the code, two virtual hosts are configured. The first virtual host uses the domain name example.com and provides services with the path /; the second virtual host uses the domain name blog.example.com and provides services with the path /. The two virtual hosts specify different root directories and homepage files.

Conclusion:
Through Nginx's virtual host configuration, we can easily provide different services for different domain names and paths. In actual applications, domain names and paths can be flexibly combined to configure multiple virtual hosts. I hope this article can help readers better understand how to use Nginx to implement virtual host configuration based on domain names and paths.

Reference code:
https://www.nginx.com/resources/wiki/start/topics/examples/full/
https://www.digitalocean.com/community/tutorials /how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

The above is the detailed content of How to use Nginx to implement virtual host configuration based on domain name and path. 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