Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在一台digitalocean vps服务器中为多个节点应用提供服务?_Node.js_Nginx_Digital Ocean_Nginx Location - Fatal编程技术网

Node.js 如何在一台digitalocean vps服务器中为多个节点应用提供服务?

Node.js 如何在一台digitalocean vps服务器中为多个节点应用提供服务?,node.js,nginx,digital-ocean,nginx-location,Node.js,Nginx,Digital Ocean,Nginx Location,下面给出的是我的/etc/nginx/sites available/default文件。我已经添加了两个新的位置块及其各自的localhost链接,只有根位置块和localhost 8000由nginx提供。其他两个链接不起作用 范例 http://111.111.111.111 = Works http://111.111.111.111/app1 = Doesn't work http://111.111.111.111/app2 = Doesn't work http://111.111

下面给出的是我的
/etc/nginx/sites available/default
文件。我已经添加了两个新的位置块及其各自的localhost链接,只有根位置块和localhost 8000由nginx提供。其他两个链接不起作用

范例

http://111.111.111.111 = Works
http://111.111.111.111/app1 = Doesn't work
http://111.111.111.111/app2 = Doesn't work
http://111.111.111.111:3000 = Doesnt't work
http://111.111.111.111:4000 = Doesnt't work
如何修复以下文件,以便访问在三个端口(3000、4000和8000)上运行的三个节点应用程序。提前谢谢你的帮助

 server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

# root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location /app1 {
    proxy_pass http://localhost:4000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

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

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

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
#   include snippets/fastcgi-php.conf;
#
#   # With php-fpm (or other unix sockets):
#   fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#   # With php-cgi (or other tcp sockets):
#   fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
    #   deny all;
    #}
}
下面是mt
etc/nginx/nginx.conf
文件

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

有关完整配置,请查看我的答案。您不需要
root/var/www/html因为您没有提供静态html页面

您需要正确的转发标头:

location /app1 {
    proxy_pass http://localhost:4000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}
您还应该在
localhost
上使用
127.0.0.1
,并为每个上游节点创建一个块。js服务器nginx充当以下对象的反向代理:

upstream root {
  server 127.0.0.1:8000;
  keepalive 256;
}

upstream app1 {
  server 127.0.0.1:4000
  keepalive 256;
}

upstream app2 {
  server 127.0.0.1:3000
  keepalive 256;
}

server {
  listen      80 default_server;

  location / {
    proxy_pass http://root;
    proxy_pass_header       Access-Control-Allow-Origin;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        Host $http_host;
    proxy_set_header        X-NginX-Proxy true;
    proxy_pass_header       Set-Cookie;
    proxy_pass_header       X-UA-Compatible;
    proxy_pass_header       Server;
    proxy_buffers 64        16k;
    proxy_buffer_size       16k;
    proxy_busy_buffers_size 64k;
    proxy_http_version      1.1;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection $http_connection;
    proxy_read_timeout      10;
    proxy_redirect          off;
  }

  location /app1 {
    proxy_pass http://app1;
    proxy_pass_header       Access-Control-Allow-Origin;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        Host $http_host;
    proxy_set_header        X-NginX-Proxy true;
    proxy_pass_header       Set-Cookie;
    proxy_pass_header       X-UA-Compatible;
    proxy_pass_header       Server;
    proxy_buffers 64        16k;
    proxy_buffer_size       16k;
    proxy_busy_buffers_size 64k;
    proxy_http_version      1.1;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection $http_connection;
    proxy_read_timeout      10;
    proxy_redirect          off;
  }

  location /app2 {
    proxy_pass http://app2;
    proxy_pass_header       Access-Control-Allow-Origin;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        Host $http_host;
    proxy_set_header        X-NginX-Proxy true;
    proxy_pass_header       Set-Cookie;
    proxy_pass_header       X-UA-Compatible;
    proxy_pass_header       Server;
    proxy_buffers 64        16k;
    proxy_buffer_size       16k;
    proxy_busy_buffers_size 64k;
    proxy_http_version      1.1;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection $http_connection;
    proxy_read_timeout      10;
    proxy_redirect          off;
  }
}

工作端点是否将您带到为端口8000提供服务的节点应用程序?是的,我可以访问在端口8000上运行的根位置。我无法访问其他两个端口。您好,我已更新了服务器代码和上面的代码,以反映您建议的更改。它似乎仍然不起作用。是否需要在启用站点的文件夹中执行模拟操作?或者任何其他文件夹?啊,你还需要上游应用程序定义。我将用一个完整的工作示例进行编辑。我没有注意到丢失了该文件。@HishamMubarak如果在应用更改后仍然有问题,请包括您的
/etc/nginx/nginx.conf
文件。我已经添加了nginx.conf以及问题。你能看一下吗?非常感谢。