简体   繁体   English

将 Nginx 与多个节点应用程序一起使用到同一服务器

[英]Using Nginx with multiple node app into same server

I am trying to setup nginx conf to work with multiple node app in ythe same server.我正在尝试设置 nginx conf 以在同一服务器中使用多个节点应用程序。

I´d like to use:我想使用:

http://localhost/node-app-01 to access app-01 in the port 3001 http://localhost/node-app-01 在3001端口访问app-01

http://localhost/node-app-02 to access app-02 in the port 3002 and so on. http://localhost/node-app-02 访问 app-02 的 3002 端口等等。

But It´s not working.但它不起作用。 The error is " http://localhost/css/chunk-006c7b90.0199750b.css net::ERR_ABORTED 404 (Not Found) ".错误是“ http://localhost/css/chunk-006c7b90.0199750b.css net::ERR_ABORTED 404 (Not Found) ”。 I can see the port is not present here.我可以看到这里没有端口。

If I access the app using http://localhost:3001, http://localhost:3002... all is ok.如果我使用 http://localhost:3001、http://localhost:3002 访问应用程序...一切正常。

If I run the app using如果我使用

My folders app structure:我的文件夹应用程序结构:

\nginx
       \conf
       \html
       \logs
       ....


 \dev-folder    
   \dist
   |  index.html
   |  \css
   |    css files
   |  \js
   |    js files
   |
   |\node-app-01   /*run in localhost:3001*/
   |  \node_modules
   |     node module files
   |  \public
   |     public app files
   |  package.json
   |  app.js
   |  server.js
   
   |\node-app-02 /*run in localhost:3002*/
   |  \node_modules
   |     node module files
   |  \public
   |     public app files
   |  package.json
   |  app.js
   |  server.js
   
   |\node-app-03 /*run in localhost:3003*/
   |  \node_modules
   |     node module files
   |  \public
   |     public app files
   |  package.json
   |  app.js
   |  server.js

Nginx conf: Nginx 配置:

http {
    include       mime.types;
    default_type  application/octet-stream;
    ....

    server {
        listen       80;
        listen   [::]:80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #nginx original server from install
        location / {
            root   html;
            index  index.html index.htm;
        }
        
    
    
        location ^~ /node-app-01/  {
            rewrite ^/node-app-01/(.*)$ /$1 break;
            proxy_pass http://localhost:3001/;
        }

        location ^~ /node-app-02/ {
            rewrite ^/node-app-02/(.*)$ /$1 break;
            proxy_pass http://localhost:3002/;
        }
        
        location ^~ /node-app-03/ {
            rewrite ^/node-app-03/(.*)$ /$1 break;
            proxy_pass http://localhost:3003/;
        }
    }

// //

you can define the upstream in your nginx configuration and if you have websocket try to use ip_hash option您可以在 nginx 配置中定义上游,如果您有 websocket 尝试使用 ip_hash 选项

take a look at the example in https tls configuration:看一下 https tls 配置中的示例:

upstream express_servers {
    ip_hash;
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
    server 127.0.0.1:8003;
}
server {

listen 443 ssl;

server_name  mydomain.com;

error_log on;
ssl_certificate /home/test/ssl/fullchain.pem;
ssl_certificate_key /home/test/ssl/privkey.pem;
client_body_timeout 3m;
client_header_timeout 3m;
client_max_body_size 150m;
send_timeout 3m;
proxy_set_header X-Real-IP $remote_addr; # pass on real client IP


location / {
  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;
  proxy_set_header X-NginX-Proxy true;
  proxy_ssl_session_reuse off;
  proxy_set_header Host $http_host;
  proxy_cache_bypass $http_upgrade;

  proxy_pass http://express_servers;
  proxy_redirect off;

  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nginx 子文件夹中的 Node.js 应用程序,WSS 连接到同一服务器 - Node.js app in a Nginx subfolder with WSS connection to the same server 使用NGinx作为图像服务器,将mongodb作为数据库,将节点作为应用程序服务器…? - Using NGinx as an image server with mongodb as a DB and node as app server…? 使用nginx和node的网络应用程序-哪个是网络服务器? - web-app using nginx and node - which is the web-server? 他们是使用 node 和 nginx 作为代理服务器创建多个应用程序的最佳实践吗? - Is their a best practice to creating multiple applications using node and nginx as a proxy server? 使用ssl的nginx或节点应用程序 - nginx or node app using ssl 在同一域上使用 nginx 为多个节点应用程序提供服务 - Serving multiple node apps with nginx on same domain Nginx和Node.js服务器-多个任务 - Nginx and Node.js server - multiple tasks 使用PM2,如何将node.js应用程序部署到同一服务器上的多个环境和端口? - Using PM2, how can I deploy my node.js app to multiple environments and ports on the same server? 将共享的npm node_modules /用于同一服务器上的多个工作空间 - Using a shared npm node_modules/ for multiple workspaces on the same server 在Node.js + Nginx应用程序的子域中对Jekyll Blog使用相同的样式 - Using the same styles for Jekyll Blog in subdomain of Node.js + nginx app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM