【问题标题】:How to deploy multiple NodeJS applications on different subdomains in digital ocean如何在数字海洋的不同子域上部署多个 NodeJS 应用程序
【发布时间】:2020-07-29 11:57:02
【问题描述】:

大家好,所以早些时候我部署了一个在一个端口上运行但使用 Nginx 的 nodejs 应用程序,我仍然可以接受默认 HTTP/HTTPS 端口上的请求并将它们转发到我的应用程序的端口。现在我想部署同一应用程序的管理部分,该应用程序构建在自己的项目空间中,并在与我的客户端应用程序略有不同的端口上运行;我想把它分配给一个子域。

我在 Digital Ocean 上运行 Ubuntu 发行版。我怎样才能做到这一点?

下面是我的整个 nginx 配置:

# 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 {

    # SSL configuration
    

    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 mydomain.com www.mydomain.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:5000; #whatever port your app runs on
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    # 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;
    #}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}






# 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 {
 index index.html index.htm index.nginx-debian.html;

        server_name admin.mydomain.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://localhost:3000; #whatever port your app runs on
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection 'upgrade';
                        proxy_set_header Host $host;
                        proxy_cache_bypass $http_upgrade;
        }

}
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

server {

    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;   listen [::]:80 default_server;

    server_name mydomain.com www.mydomain.com;
    return 404; # managed by Certbot


}

我的管理节点应用在端口 3000 上运行,客户端应用在 5000 上运行

【问题讨论】:

  • 你在这里尝试了什么。因为如果您可以接受请求(server_name domain.com)并已经将请求转发到默认端口(@98​​7654325@),那么您肯定已经尝试过server_name sub.domain.comproxy_pass http://127.0.0.1:8081
  • 这里的重点不是说“这很容易,你怎么能与它抗争?” (对不起,如果这听起来像)。只是如果你显示你的配置,你会得到简洁而具体的答案,但现在我们不得不猜测。
  • 我明白了。所以事情是在最近之前,我从来没有真正在 Linux 上部署过应用程序。这是我第一次在完全自我管理的服务器上部署任何应用程序。我遵循的指令可以在这里找到:gist.github.com/bradtraversy/cd90d1ed3c462fe3bddd11bf8953a896。不太明显,我可以重复您突出显示的配置部分
  • 那么您的问题解决了吗?通常你重复整个服务器块。例如像这样:serverfault.com/a/538831/233631
  • 不是真的@ippi。 admin.mydomain.com 仍在重定向到在另一个端口上运行的应用程序。我已编辑问题以包含我添加的服务器块。请检查一下。可能是配置错误。

标签: node.js nginx subdomain digital-ocean


【解决方案1】:

您的 nginx 配置看起来不错,为了达到预期的效果,您必须在数字海洋面板中为您的新域添加新的 dns 解析。

添加一个新的 cname 记录,作为您的域的别名,如下所示:

CNAME admin.mydomain.com 
is an alias of mydomain.com
  1. 数字海洋面板
  2. 网络
  3. DNS 记录
  4. 创建新记录cname类型

此配置必须与 server_name 属性的当前 nginx 配置相匹配。

【讨论】:

  • 我已经这样做了,但仍然无法正常工作。我再次编辑了问题以包含我的整个配置文件。也许有什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 2016-05-30
  • 2015-06-29
  • 2019-02-18
  • 2014-09-11
  • 2022-12-17
相关资源
最近更新 更多