ExpressJS / NodeJS - 反向代理部署

时间:2017-05-31 09:44:36

标签: angularjs node.js amazon-web-services express amazon-ec2

我有3个AngularJS应用程序,每个应用程序都有自己的ExpressJS后端。如何以这种方式部署所有3个应用程序:

http://myapp.com/<app1>
http://myapp.com/<app2>
http://myapp.com/<app3>

其他信息:
- 我在AWS EC2实例中部署应用程序 - 我尝试在单个ExpressJS应用程序中合并应用程序。虽然这有效,但我仍然想知道上面的情况是否可能

3 个答案:

答案 0 :(得分:2)

当然有可能。您只需要NGINX或Apche作为反向代理运行。

假设您的节点应用程序在本地端口3000,3001和3002上运行,您将设置一个.conf文件,将其作为位置标记的上游服务器,如下所示:

。 。 。     location / app1 {         proxy_pass http://localhost:3000;         proxy_http_version 1.1;         proxy_set_header升级$ http_upgrade;         proxy_set_header连接'升级';         proxy_set_header主机$ host;         proxy_cache_bypass $ http_upgrade;     }

. . .
location /app2 {
    proxy_pass http://localhost:3001;
    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;
}

}

在此处阅读更多详情:https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

答案 1 :(得分:1)

server {
    listen 80;
    server_name es.domain.com;
    location /app1 {
        rewrite ^/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:3001;
        proxy_redirect http://localhost:3001 https://es.domain.com/appw;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $http_host;
        auth_basic "Elasticsearch Authentication";
        auth_basic_user_file /etc/elasticsearch/user.pwd;
}

    location /app2 {
        rewrite ^/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:3002;
        proxy_redirect http://localhost:3002 https://es.domain.com/app2;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $http_host;
        auth_basic "Elasticsearch Authentication";
        auth_basic_user_file /etc/elasticsearch/user.pwd;
}
}

请参阅此链接 http://www.minvolai.com/blog/2014/08/Setting-up-a-Secure-Single-Node-Elasticsearch-server-behind-Nginx/Setting-up-a-Secure-Single-Node-Elasticsearch-server-behind-Nginx/

答案 2 :(得分:0)

您可以设置AWS CloudFront并将每个应用程序设置为起源。它提供了从单个域(Setup for CloudFront)路由到不同Express Apps的灵活性,还允许在Edge位置缓存静态内容路径。