二级域名配置以及nginx解析二级域名到html页面

刺骨的言语ヽ痛彻心扉 2023-10-04 20:06 95阅读 0赞

此文章适合发布前端项目使用,如果想要配置二级域名到后端服务,可以查看这篇文章:nginx配置二级域名 - 简书

在阿里云上配置二级域名,就是添加一条记录就可以了,超级简单,不懂的可以看后面的解释说明,比如我这里添加了一个second.1024shen.com为二级域名,主域名是1024shen.com

438ce00bbd0f4a07a0c3d0d0b51f3191.png

我们的主域名页面是:

dba8c7f8d59b459599807bc3718ef750.png我的nginx配置目录内容是:

c62998adec1f45f9ac4ce38206002e03.png

我们的nginx.conf 默认配置是:

  1. user www-data;
  2. worker_processes auto;
  3. pid /run/nginx.pid;
  4. events {
  5. worker_connections 768;
  6. # multi_accept on;
  7. }
  8. http {
  9. ##
  10. # Basic Settings
  11. ##
  12. sendfile on;
  13. tcp_nopush on;
  14. tcp_nodelay on;
  15. keepalive_timeout 65;
  16. types_hash_max_size 2048;
  17. # server_tokens off;
  18. # server_names_hash_bucket_size 64;
  19. # server_name_in_redirect off;
  20. include /etc/nginx/mime.types;
  21. default_type application/octet-stream;
  22. ##
  23. # SSL Settings
  24. ##
  25. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  26. ssl_prefer_server_ciphers on;
  27. ##
  28. # Logging Settings
  29. ##
  30. access_log /var/log/nginx/access.log;
  31. error_log /var/log/nginx/error.log;
  32. ##
  33. # Gzip Settings
  34. ##
  35. gzip on;
  36. gzip_disable "msie6";
  37. # gzip_vary on;
  38. # gzip_proxied any;
  39. # gzip_comp_level 6;
  40. # gzip_buffers 16 8k;
  41. # gzip_http_version 1.1;
  42. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  43. ##
  44. # Virtual Host Configs
  45. ##
  46. include /etc/nginx/conf.d/*.conf;
  47. include /etc/nginx/sites-enabled/*;
  48. }
  49. #mail {
  50. # # See sample authentication script at:
  51. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  52. #
  53. # # auth_http localhost/auth.php;
  54. # # pop3_capabilities "TOP" "USER";
  55. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  56. #
  57. # server {
  58. # listen localhost:110;
  59. # protocol pop3;
  60. # proxy on;
  61. # }
  62. #
  63. # server {
  64. # listen localhost:143;
  65. # protocol imap;
  66. # proxy on;
  67. # }
  68. #}

可以看到里面引入了:

nginx 会从 /etc/nginx/conf.d 中加载以 .conf 结尾的配置文件

nginx 会从 /etc/nginx/sites-enabled 中加载任何名称的配置文件

1b307194559d467a84ce359fa9060560.png

sites-available 中拥有名为 default 的配置文件,打开即可在该文件开头看到 nginx packaging team 的说明:

In most cases, administrators will remove this file from sites-enabled/ and leave it as reference inside of sites-available where it will continue to be updated by the nginx packaging team.

通常情况下,网站管理员会将此文件的链接从 sites-enabled 中删除,并将其作为 sites-available 中其他文件的参考,nginx packaging team 将持续对此文件进行更新。

也就是说,文件夹下的 default 为网站配置文件的参考,由于在 nginx 更新时,default 会一同被更新以展示配置文件的变化,所以在配置网站时,不应该直接修改此文件,需要复制为新文件,再进行修改。

sites-available 则是用于存放网站的配置文件,意为可用的网站列表,用于在需要时链接到 sites-enabled 中作为需要启用的网站。

sites-enabled 中则只拥有 sites-available 文件夹下 default 的软链接,结合前面得出:

sites-enabled 下的文件,会作为 nginx.conf 的一部分加载
sites-enabled 下的用于存放 sites-available 中文件的软连接
sites-enabled 意为已开启的网站,将 sites-available 中的配置文件链接到此处,以使配置文件被 nginx 加载。

sites-available 与 sites-enabled 使我们能够进行模块化配置,当我们希望增加新网站时,我们可以在 sites-available 中创建新配置文件;当我们需要关闭某个站点时,我们可以在 sites-enabled 中将链接移除,这在某种程度是提高了 nginx 的管理效率。

所以我们只需要在sites-available创建一个新的配置文件,直接复制default,然后修改里面的内容:

193a21ee83ea4197a75a47f9ecf5d7b7.png

  1. ##
  2. # You should look at the following URL's in order to grasp a solid understanding
  3. # of Nginx configuration files in order to fully unleash the power of Nginx.
  4. # http://wiki.nginx.org/Pitfalls
  5. # http://wiki.nginx.org/QuickStart
  6. # http://wiki.nginx.org/Configuration
  7. #
  8. # Generally, you will want to move this file somewhere, and start with a clean
  9. # file but keep this around for reference. Or just disable in sites-enabled.
  10. #
  11. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
  12. ##
  13. # Default server configuration
  14. #
  15. server {
  16. listen 80;
  17. listen [::]:80;
  18. # SSL configuration
  19. #
  20. # listen 443 ssl default_server;
  21. # listen [::]:443 ssl default_server;
  22. #
  23. # Note: You should disable gzip for SSL traffic.
  24. # See: https://bugs.debian.org/773332
  25. #
  26. # Read up on ssl_ciphers to ensure a secure configuration.
  27. # See: https://bugs.debian.org/765782
  28. #
  29. # Self signed certs generated by the ssl-cert package
  30. # Don't use them in a production server!
  31. #
  32. # include snippets/snakeoil.conf;
  33. root /var/www/second;
  34. # Add index.php to the list if you are using PHP
  35. index index.html index.htm index.nginx-debian.html;
  36. server_name second.1024shen.com;
  37. location / {
  38. # First attempt to serve request as file, then
  39. # as directory, then fall back to displaying a 404.
  40. try_files $uri $uri/ =404;
  41. }
  42. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  43. #
  44. #location ~ \.php$ {
  45. # include snippets/fastcgi-php.conf;
  46. #
  47. # # With php7.0-cgi alone:
  48. # fastcgi_pass 127.0.0.1:9000;
  49. # # With php7.0-fpm:
  50. # fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # Virtual Host configuration for example.com
  60. #
  61. # You can move that to a different file under sites-available/ and symlink that
  62. # to sites-enabled/ to enable it.
  63. #
  64. #server {
  65. # listen 80;
  66. # listen [::]:80;
  67. #
  68. # server_name example.com;
  69. #
  70. # root /var/www/example.com;
  71. # index index.html;
  72. #
  73. # location / {
  74. # try_files $uri $uri/ =404;
  75. # }
  76. #}

注意修改这两个地方:root后面跟上静态文件目录,server_name 后面跟上二级域名

d5427a6299334670819b83978f7ca825.png

然后在/var/www路径下创建一个second文件夹,并创建一个index.html页面:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <h1>这是second.1024shen.com页面!!!!!</h3>
  11. </body>
  12. </html>

然后重新加载nginx配置:

  1. service nginx reload

然后访问子域名:http://second.1024shen.com/

ff84f2f53394446fb1598c9b9d496ae2.png

发表评论

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

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

相关阅读

    相关 Nginx配置二级域名

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

    相关 二级目录和二级域名

    想把一个站发展起来,内容充实一些,是添加二级域名还是二级目录好呢?最后想了想从以下几点来分析: 什么是二级域名,什么是二级目录? 二级目录,就是子目录,继承在主站目录下的,