This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

We can use port Numbers to distinguish between different services, but when we visit the web site is almost never seen use of domain name and port number, use the main reason for the domain name is easy to remember domain name than IP address, and port number change affect the function of domain name, then is there any way we could not add port number, also can distinguish between these services? You can use a secondary domain name, for example, the domain name of Baidu search is www.baidu.com, and the domain name of Baidu Baike is baike.baidu.com. Use nginx to configure secondary domain names.

Do not use a secondary domain name before

 server {
        listen       80;
        server_name  _/;
        root         /usr/share/nginx/html/wangscaler;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        rewrite ^(.*)$ https://$hostThe $1;
        location / {
            try_files $uri $uri/ /index.html;
            #proxy_pass http://WebApi;
        }
        location /wangscaler{
           proxy_pass http://WebApi;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

In addition to inconvenient access, it is resource consumption. Each service requires an additional port. And open the service more words, port number also have to manage, otherwise a long time do not know whether the port number has been used, was used in what place.

Using a Secondary domain name

server {
        listen       80;
        server_name  test.scalerwang.com;
        include /etc/nginx/default.d/*.conf;
        root         /usr/share/nginx/html/test;
        gzip_static on;
        rewrite ^(.*)$ https://$hostThe $1;
        location /{
         try_files $uri $uri/ /index.html;
        }
       }
    server {
        listen       80;
        server_name  test1.scalerwang.com;
        root         /usr/share/nginx/html/test1;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        rewrite ^(.*)$ https://$hostThe $1;
        location / {
            try_files $uri $uri/ /index.html;
        }
        location /love{
           proxy_pass http://WebApi;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

If port 80 is used, you only need to configure server_name respectively. It’s as simple as assigning different secondary domain names to different services. Note: I’m using SSL certificates here, so if you don’t have one you can comment out rewrite ^(.*)$https://$host$1; The purpose is to redirect HTTP users to HTTPS, of course, if you configure SSL certificates, you need to configure port number 443 instead of 80.

redirect

Because the address of my previous blog was www.scalerwang.com/blog and now it has changed to blog.scalerwang.com after using the secondary domain name, the original address may not be directly accessible when applied in other places, for example, my address is applied in the case of recommending excellent blogs on the theme of this blog. You can redirect to the current address without causing any impact. Change it under the previous www.scalerwang.com server.

location /blog {
       rewrite ^(.*)$  https://blog.scalerwang.com;
   }
Copy the code

If this doesn’t happen to you, ignore it.

Enabling Domain name Resolution

I bought the domain name at ucloud.cn, so take UCloud as an example.

If I click resolve, the secondary domain I need to access is test.scalerwang.com

Record the value and enter your IP address. You can also enter the wildcard * in the host record to enable resolution of all your secondary domain names.

Pay attention to the point

  • The default nginx configuration file contains the listen configuration after 80default_server;namelylisten 80 default_server;, remember todefault_serverDelete it. Otherwise, the configuration fails
  • 2. After the configuration, reload the configuration file or restart the service.
  • 3, the use of the domain name must be in the purchase of the domain name manufacturer open the corresponding domain name resolution service.

The last

After using the secondary domain name, access is also convenient and fast, the most important is to save a large number of port numbers.

Links to articles related to this article.

  • 1, hand in hand to teach you to build your own blog: vuepress static website.
  • 2. Make your site HTTPS: Convert your site from HTTP to secure HTTPS.
  • 3. Develop tips for Nginx GZIP: Optimizing services.
  • 4. Develop tips for compiling and compressing plug-ins: supplement the previous article.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!