打开服务器的3000端口危险吗?

时间:2020-07-18 16:01:39

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

我想部署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应用程序的最佳方法是什么?如何实现?

2 个答案:

答案 0 :(得分:0)

您只需将端口分配给process.env.PORT即可部署该应用,然后将整个角度构建放置在public / src文件夹中,并在节点服务器文件中提供公共文件夹路径。

您可以在这里https://github.com/Ris-gupta/chat-application

参考

答案 1 :(得分:0)

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