在 Digitalocean 上使用 express 和 mongo db 部署 Angular 2 应用程序

在 Digitalocean 上使用 express 和 mongo db 部署 Angular 2 应用程序

我整个上午都在努力部署 Angular 7 应用以及 node、express 和 mongo,我感到很困惑。不知为何整个互联网上都没有关于如何执行此操作的很好的解释。请帮忙!

我在 DigitalOcean 上。我已经设置了 Nginx,并且能够在我的 IP 地址上看到 Nginx 索引页。我还设置了一个服务器块,其中包含两个位置,一个用于我的实际 Angular 应用程序,另一个用于我的 express api。以下是该代码:

server {

        root /var/www/devgrub.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name devgrub.com www.devgrub.com;

        location / {
                try_files $uri $uri/ =404;
        }

        location /api {
                proxy_pass http://localhost:8080;
                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;
                #try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/devgrub.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/devgrub.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


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


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


        listen 80;
        listen [::]:80;

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

}

我已经/var/www/devgrub.com/html上传了运行后生成的 Angular 应用程序的代码ng build --prod

里面/home/devgrub有我的 express / node 应用程序。

我可以在 devgrub.com 上成功访问我的 Angular 应用程序,但是当我访问 devgrub.com/api 时,它给出的是hello world我按照 DigitalOcean 设置教程制作的原始节点应用程序。我重新启动了正在运行的节点应用程序,现在它在访问 devgrub.com/api 时PM2显示。502 Bad Gateway

我更改了 Angular 应用程序中所有的localhostapi 调用,希望这样做能有所成效,但结果却并非如此。我已经创建了数据库,use devgrub但 Angular 应用程序似乎无法连接到它,而且由于我对此还很陌生,我不知道从哪里开始进行故障排除。请帮忙!

更新 以下是 Nginx 错误日志中的一些错误

2019/03/16 17:55:10 [warn] 14358#14358: server name "devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
2019/03/16 17:55:10 [warn] 14358#14358: server name "www.devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
2019/03/16 17:55:10 [notice] 14358#14358: signal process started
2019/03/16 17:56:14 [crit] 14359#14359: *98 SSL_do_handshake() failed (SSL: error:1417D18C:SSL routines:tls_process_client_hello:version too low) while SSL handshaking, client: 184.105.139.70, server: 0.0.0.0:443
2019/03/16 17:57:11 [warn] 14436#14436: server name "devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
2019/03/16 17:57:11 [warn] 14436#14436: server name "www.devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
2019/03/16 17:57:11 [notice] 14436#14436: signal process started
2019/03/16 17:57:22 [error] 14437#14437: *100 connect() failed (111: Connection refused) while connecting to upstream, client: 71.175.29.181, server: devgrub.com, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3000/api", host: "devgrub.com"
2019/03/16 17:57:51 [warn] 14501#14501: server name "devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
2019/03/16 17:57:51 [warn] 14501#14501: server name "www.devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
2019/03/16 17:57:51 [notice] 14501#14501: signal process started
2019/03/16 17:57:52 [error] 14502#14502: *104 connect() failed (111: Connection refused) while connecting to upstream, client: 71.175.29.181, server: devgrub.com, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3000/api", host: "devgrub.com"
2019/03/16 17:57:53 [error] 14502#14502: *104 connect() failed (111: Connection refused) while connecting to upstream, client: 71.175.29.181, server: devgrub.com, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3000/api", host: "devgrub.com"
2019/03/16 17:57:54 [error] 14502#14502: *104 connect() failed (111: Connection refused) while connecting to upstream, client: 71.175.29.181, server: devgrub.com, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3000/api", host: "devgrub.com"
2019/03/16 18:05:21 [error] 14502#14502: *110 connect() failed (111: Connection refused) while connecting to upstream, client: 71.175.29.181, server: devgrub.com, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3000/api", host: "devgrub.com"

答案1

如果有,502 Bad Gateway则意味着您应该调查后端出了什么问题。它真的正常运行了吗?您在后端日志中看到了什么?

簡單curl http://localhost:8080就明了。

调查您的错误日志,我发现您的 server_name 指令存在问题:

server name "devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8
server name "www.devgrub.com/api" has suspicious symbols in /etc/nginx/sites-enabled/devgrub-api:8

我不知道您发布了哪个配置,但请检查您的配置/etc/nginx/sites-enabled/devgrub-api- 我猜您会看到不正确的 server_name。

因此,要使其工作,解决方案是确保您的后端正常工作。如果您的节点在 docker 中 - 请确保它正确公开端口,或者两个容器(nginx 和节点)都在一个网络内。检查后端服务器的日志也是一个很好的起点。

PS 与 DigitalOcean、mongo 或 Angular2 应用无关。我的意思是没有必要将它们添加到标题中,因为您的根本问题超出了它们的范围。

相关内容