在服务器上进行MEAN堆栈部署

时间:2018-07-13 10:04:42

标签: node.js angular express nginx nginx-location

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

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中使用。但是在角度站点中,当我转到浏览器并转到login页并单击“提交”按钮时,未连接到node js服务器。当我在网络中检查我的回复时,它会像这样返回。

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

我没有发现这段代码有什么问题。请建议我如何处理。

1 个答案:

答案 0 :(得分:1)

最后得到了答案。我必须更改我的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;
            }
          }
        }