配置 nginx 来托管多个子域名

2025-06-07

配置 nginx 来托管多个子域名

这篇文章是从我的博客转发过来的


在同一台机器上为子域名配置 nginx 对我来说很困惑。所以我写这篇教程主要是为了(自己)参考,但大家也会觉得它很有用。事不宜迟,让我们开始吧。

对于那些不知道 nginx 到底是什么的人,请先阅读这篇freeCodeCamp 文章作为起点。

我假设您使用的是 Linux 机器,并且您知道 nginx 的含义。

首先,安装 nginx -

sudo apt-get install nginx
Enter fullscreen mode Exit fullscreen mode

第二个方法/etc/nginx/sites-available/default是用 vim/nano 打开它。你会看到类似这样的内容:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# 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.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }.
#}
Enter fullscreen mode Exit fullscreen mode

从以上内容中,您可以得出以下几点:

  • server {}- 告诉 nginx“嘿,我认为你应该这样配置服务器”
  • listen 80- 翻译为“监听端口 80,这是 Web 客户端的默认端口”
  • root /var/www/html;- nginx 知道该位置的 HTML 文件可以被提供
  • index index.html index.htm index.nginx-debian.html;- 这表明应按优先级从低到高(从左到右)的顺序提供这些类型的文件。
  • server_name _;- 这表示_服务器名称是 。假设我们的域名是example.com,则服务器名称将是example.com。这是因为当收到 的请求时example.com,nginx 应该决定是否处理它。
  • location / {}- 这意味着所有请求都必须由此服务器配置处理。此外,还有一个选项可以处理特定请求。例如,以 开头的请求/server_1可以加载server_1.html,所有其他请求都可以加载default.html

下一个显而易见的问题是 - 如何配置子域?

其实没那么复杂。只需 7 个步骤 -

  • 复制默认配置并将其重命名为subdomain1.example.com
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/subdomain1.example.com
Enter fullscreen mode Exit fullscreen mode

注意:您不必非要命名文件subdomain1.example.com。如果遵循一个方便所有相关人员的约定,会更容易一些。

  • default_server打开它,删除“besides”一词listen 80及其下面的一行。这很关键。
  • 更改server_nameas并更改正在使用的subdomain1.example.comHTMLroot
  • 然后将该文件链接到 nginx 的 sites-enabled 目录中的文件。这是为了让 nginx 知道配置subdomain1.example.com已在配置中启用。
sudo ln -s /etc/nginx/sites-available/subdomain1.example.com /etc/nginx/sites-enabled/subdomain1.example.com
Enter fullscreen mode Exit fullscreen mode
  • /var/www/html在里面写一个 HTML 文件subdomain1.example.com
  • 重启 nginx 服务器
  • 如果您使用的是 Cloudflare 之类的 DNS 提供商,请将盒子的 IP 地址添加到 A 记录中subdomain1.example.com。如果没有,则将盒子的 IP 地址和 subdomain1.example.com 添加到 /etc/hosts。
  • 在浏览器上加载subdomain1.example.com并检查加载情况。如果一切正常,您将看到为此子域名编写的 HTML。

将相同的方法扩展到您需要的任何其他域或子域。

文章来源:https://dev.to/on_stash/configure-nginx-to-host-multiple-subdomains-2g0b
PREV
只需 5 分钟即可创建您的第一个超棒 Chrome 扩展程序!🔥🔥🔥(内含常见问题解答 🤔 => 😊)常见问题解答
NEXT
Building a URL shortening service with NodeJS and MongoDB. Deploy to Azure. Front end Server Yayy we have come a long way!! NEXT STEP LAST STEP!!!