服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx配置二级域名的方法实现

Nginx配置二级域名的方法实现

2023-03-07 17:01Asurplus Nginx

本文主要介绍了Nginx配置二级域名的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

当一个域名需要使用在两个项目上后,我们就需要使用到二级域名,在 Nginx 中配置二级域名如下:

1、原始配置文件如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
worker_processes  1;
 
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    sendfile        on;
    
    keepalive_timeout  65;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 
}

这是解压后的 nginx.conf 文件,可以看出,当前 nginx 监听的是 80 端口,它的服务名为 localhost,假如我们的域名为:baidu.com,那我们输入:localhost.baidu.com 也是可以访问的

2、配置二级域名

对于我们刚才理解的服务名,假如我们的域名为:baidu.com,我们需要配置的二级域名为 asurplus.baidu.com,我们的配置文件如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
worker_processes  1;
 
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    sendfile        on;
    
    keepalive_timeout  65;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
    server {
        listen       80;
        server_name  asurplus.baidu.com;
 
        location / {
            proxy_pass http://127.0.0.1:8081;
        }
    }
 
}

到 sbin 目录,执行命令重启 nginx

?
1
./nginx -s reload

我们新增了一个服务,监听的依然是 80 端口,我们的服务名变成了我们的二级域名:asurplus,并转发到了我们的 8081 端口,从而完成了二级域名的配置

到此这篇关于Nginx配置二级域名的方法实现的文章就介绍到这了,更多相关Nginx配置二级域名内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://lizhou.blog.csdn.net/article/details/114533096

延伸 · 阅读

精彩推荐