【问题标题】:Is it dangerous opening port 3000 of the server?打开服务器的3000端口有危险吗?
【发布时间】:2020-11-08 05:55:32
【问题描述】:

我想部署我的 Angular + NodeJS 应用程序。我的 NodeJS 应用程序在服务器上的 http://localhost:3000 上运行。我的 Angular 应用程序尝试使用以下前缀地址将其请求发送到服务器:http://server.ip.address:3000。我使用以下命令打开了服务器的 3000 端口,以帮助我的程序运行,现在它运行良好。

irewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --reload

但我不确定自己做得好不好?

我的 Angular 应用在​​ nginx 上运行,而我的 NodeJS 应用在 PM2 上运行。我还尝试设置反向代理,如下面的etc/nginx/nginx.conf 所示,但它不起作用,只打开端口 3000 对我有用!

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /demo/stock-front9/dist/strategy;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            #proxy_pass http://localhost:3000;
            #proxy_http_version 1.1;
            # First attempt to serve request as file, then

            # as directory, then redirect to index(angular) if no file found.
            try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

部署 Angular + NodeJS 应用程序的最佳方式是什么?我该怎么做?

【问题讨论】:

  • 更改您的ajax'y代码不使用端口,然后在本地开发时使用3000,在生产中将使用80,然后nginx传递到3000,打开它并不危险,但它绕过 nginx 暴露上游主机和端口,即如果想使用 cloudflare 和/或可能的 cors,你会遇到问题
  • @LawrenceCherone:我认为 Express 服务器默认运行 localhost:3000。你的意思是我可以从 Angular 代码中的请求中删除端口号(3000)?
  • nginx 将处理从 80 到 3000 的转发。是的,更改您的 Angular 代码,因此不包括端口。即不做url: ':3000/api/endpoint,只做url: '/api/endpoint 那么如果url 是localhost:3000 或example.com 它将继承域和端口
  • 你还需要使用 proxy_pass 转发到 express,而不是 try_files
  • @LawrenceCherone:我试过你说的但没有用!

标签: node.js express nginx reverse-proxy pm2


【解决方案1】:

没有最佳方法,但有一些最佳做法。直接在服务器上打开端口不是好的解决方案。我建议您使用 docker 并使用 NGINX 在容器内发布您的应用程序。你也可以用同样的方式部署你的后端服务器。

【讨论】:

    【解决方案2】:

    您可以通过将端口分配给 process.env.PORT 来部署应用程序,并将整个 Angular 构建放在 public/src 文件夹中,并在节点服务器文件中提供公共文件夹路径。

    你可以参考这里https://github.com/Ris-gupta/chat-application

    【讨论】:

      猜你喜欢
      • 2013-10-20
      • 2020-11-08
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多