Use nginx to configure domain name-based virtual hosts

Use nginx to configure domain name-based virtual hosts

1. What is a virtual host?

Virtual hosts use special technologies to logically divide a running server into multiple hosts. The main purpose of doing this is to allow multiple website programs to run on one physical server, so that the remaining space on the server can be utilized. Give full play to the role of the server. The virtual hosts are completely independent.

In this way, when using nginx to build a website platform, you only need to use one nginx software to run multiple IP-based or domain-based websites.

2. Domain name-based virtual hosting

This type of domain name-based virtual hosting is the most commonly used. IP-based ones are generally used in intranets.

(1) Configuration in nginx.conf

Just add the above and below fields in nginx.conf.

include vhosts/*.conf;

The above fields can be added in the http module.

Then create a vhosts directory in the conf directory of nginx, if it already exists, do not create it.

(2) Add a virtual host configuration file

Add a clear .conf configuration file in the vhosts directory. You can give it any name, but it is best to give it a name related to the deployed application to facilitate later maintenance.

Take the previous configuration as an example

server {

    listen 80;     

#Configure the listening port. Just configure port 80. No matter how many virtual hosts you write, port 80 will be used. server_name ebook.yunweigonghui.com;

    #This is the most important thing, configure the required domain name.

    root /usr/local/ywgh/nginx/html/wp/;

       #Write the project path clearly. This is also very important. Don't configure it wrong.

    access_log /usr/local/ywgh/nginx/logs/wp/access.log main;

    #Access logs can be written clearly or not. In production environments, they must be configured clearly to separate the logs.

location ~ \.php$ {

        try_files $uri =404;

        fastcgi_pass 127.0.0.1:9000;

        error_log /usr/local/ywgh/nginx/logs/wp/php-error.log;

        include fastcgi.conf;

        fastcgi_index index.php;

    }

       #The above is the configuration content related to PHP.

}

After the above configuration is completed, you can restart nginx or reload it.

(3) Summary

Many virtual hosts can be written in the same configuration file, but this will be very inconvenient for later maintenance (the author has a deep understanding of this, especially when connecting to a platform maintained by others). Try to write as many configuration files as possible so that they appear short and easy to read.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Three ways to configure Nginx virtual hosts (based on domain names)

<<:  Detailed explanation of JS memory space

>>:  MySQL master-slave synchronization mechanism and synchronization delay problem tracking process

Recommend

Summary of various postures of MySQL privilege escalation

Table of contents 1. Write Webshell into outfile ...

How to use IDEA to configure tomcat and create JSP files

Before using idea to write JSP files, you need to...

Detailed explanation of primary keys and transactions in MySQL

Table of contents 1. Comments on MySQL primary ke...

MySQL full-text search Chinese solution and example code

MySQL full text search Chinese solution Recently,...

Drop-down menu and sliding menu design examples

I found a lot of websites that use drop-down or sl...

How to get the contents of .txt file through FileReader in JS

Table of contents JS obtains the .txt file conten...

Lambda expression principles and examples

Lambda Expressions Lambda expressions, also known...

How to use Linux tr command

01. Command Overview The tr command can replace, ...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

CSS layout tutorial: How to achieve vertical centering

Preface I have been summarizing my front-end know...

Nginx Layer 4 Load Balancing Configuration Guide

1. Introduction to Layer 4 Load Balancing What is...

CSS optimization skills self-practice experience

1. Use css sprites. The advantage is that the smal...

Detailed example of locating and optimizing slow query sql in MySQL

Table of contents 1. How to locate and optimize s...