“This is the fourth day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Hi, I’m Smooth, a sophomore front-end enthusiast who wants to become a front-end development engineer

Configuring subdomains allows you to deploy multiple projects simultaneously on a single server

This article will explain how to configure the subdomain so that you can deploy multiple projects on a server at the same time, the new hand can understand the tutorial ha ha

demand

The requirements are as follows:

I have a domain name zhangjiancong. Top, I want to build several Web applications on it, respectively

  • Personal blog: Myblog.zhangjiancong. Top
  • Entertainment: XXX. Zhangjiancong. Top

One way to do this is in the nginx document myblog, XXX directory, access is zhangjiancong. Top /myblog

zhangjiancong.top/xxx

But this has several drawbacks

  1. Some Web applications may not support non-root access, making them unavailable
  2. Because individual applications may require special configuration of location, the location configuration may be different and more complex when these applications are not in the root path
  3. It’s not cool. It’s ugly

To make myblog and XXX have their own subdomains (myblog.zhangjiancong. Top, xxx.Zhangjiancong. Top), you can do as follows:

  1. In the DNS server, add a CNAME record and point the subdomain name to the host name corresponding to the root domain.
  2. Then modify the Nginx configuration on the pagoda screen
  3. Adding a New site

Details are as follows:

configuration

1. Add CNAME records

I am Ali cloud, so use Ali cloud as an example, no matter what service provider, open the domain name resolution interface

The domain name resolution page is displayed



Click Add Record

Configuration instructions

CNAME record

When are CNAME records used?

If a domain name needs to point to another domain name, and another domain name provides the IP address, you need to add a CNAME record

Description of adding CNAME records

Remember to change the line parsing column to default, China Unicom is only the following example to illustrate the function of this configuration item

  • Record type: Select CNAME

  • Host record: fill in the subdomain name (for example, to add myblog. Zhangjiancong. Top resolution, just need to fill in myblog in the host record; If zhangjiancong. Top parse is added, the host record is left blank, the system will automatically fill a “@” into the input box).

  • Line resolution: default (if you do not select the default, some specific users cannot resolve; In the figure above, all users except Unicom users can be resolved normally.

  • Record value: Specifies the domain name specified by CNAME. You can enter only the domain name. After a record is generated, a “(“) is automatically added to the domain name. This is a normal phenomenon

  • TTL: the system generates the value automatically when you add the value. The default value is 600 seconds. (TTL is the cache time.

Click OK

2. Modify the Nginx configuration file

Find the pagoda interface item





Find the configuration entry



Modify the Server field in the configuration file as shown in the figure above

  • Because my main domain is my other resource loaf-community, so I$docThe initial value for the"loaf-community", or for""Can be
  • If is the matching rule, root is the path resources corresponding to the server, my resources are stored in/www/wwwroot/You can configure it according to the real path of your server
Set $doc "loaf-community"; if ($host ~ ^(\w+)\.zhangjiancong\.top) { set $doc $1; } # root root/ WWW /wwwroot/$doc;Copy the code

As long as I create a new subdirectory in the/WWW /wwwroot directory to record the new project, take the subdomain name as the directory name, and add a CNAME of the domain name in the domain name service provider.





3. Add a new site

Find the entrance



Configure subdomain names and project resource paths

After the configuration, restart the server

After configuring the second step, you only need to perform the first and third steps each time you add a new subdomain name or project

That’s all for this article

The last

Finally, my name is Smooth, and I will write as many front-end tutorials as I can in 2022. I look forward to your attention

The latest publication plan isWebpackAnd performance optimization knowledge body

At the same time, you can also scan the code to pay attention to my public number: Smooth front-end growth record, public number synchronous update