服务器上的 MEAN 堆栈部署

MEAN stack deployment on server

我为我的项目创建了 AWS 实例并安装了 Nginx 服务器。现在,对于 angular,我创建 ang.conf,对于节点,我在 site-available 中创建 node.conf 文件。分享我的配置文件

ang.conf

    server {
    listen       80;
    server_name  IP;

    location / {
        root   project_path;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;

    # To allow POST on static pages
    error_page  405     =200 $uri;
 }

node.conf

    server {
    listen 3004;
    server_name IP;

    location / {
        proxy_pass http://IP:3000;
        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;
    }
}

我的节点服务器工作正常。我可以将 apipostman 与端口一起使用,例如 http://MY_YP:3000。但是在 angular 站点中,当我转到浏览器并转到 login 页面并单击提交按钮时,无法连接到 node js 服务器。当我在网络中检查我的响应时,它 return 像这样。

Response
HTTP/1.1 200 OK
ETag: W/"5b486d9c-848"
Content-Type: text/html
Date: Fri, 13 Jul 2018 09:56:38 GMT
Last-Modified: Fri, 13 Jul 2018 09:15:08 GMT
Server: nginx/1.10.3 (Ubuntu)
Connection: keep-alive
Content-Encoding: gzip
Transfer-Encoding: Identity

我不知道这段代码有什么问题。请建议我如何处理这个问题。

终于找到答案了。我必须更改我的 nginx.conf 文件。

    events {
  worker_connections  4096;  ## Default: 1024
}

    http {

      # Change this depending on environment

        upstream api {
            server 192.168.0.1:9998;
            #put here node.js ip with port
          }

          server {
            listen       80;
            server_name  localhost;

            root   /usr/share/nginx/html;
            index  index.html index.htm;
            include /etc/nginx/mime.types;

            location / {
              # If you want to enable html5Mode(true) in your angularjs app for pretty URL
              # then all request for your angularJS app will be through index.html
              try_files $uri /index.html;
            }

            # /api will server your proxied API that is running on same machine different port
            # or another machine. So you can protect your API endpoint not get hit by public directly
            location /api {
              proxy_pass http://api;
              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;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }

            #Static File Caching. All static files with the following extension will be cached for 1 day
            location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
              expires 1d;
            }
          }
        }