Nginx配置二级域名

清疚 2022-11-06 06:50 387阅读 0赞

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

1、原始配置文件如下

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

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

2、配置二级域名

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

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

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

  1. ./nginx -s reload

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

如您在阅读中发现不足,欢迎留言!!!

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx配置二级域名

    当一个域名需要使用在两个项目上后,我们就需要使用到二级域名,在 Nginx 中配置二级域名如下: 1、原始配置文件如下 worker_processes 1;