繁体   English   中英

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

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

我正在尝试设置 nginx conf 以在同一服务器中使用多个节点应用程序。

我想使用:

http://localhost/node-app-01 在3001端口访问app-01

http://localhost/node-app-02 访问 app-02 的 3002 端口等等。

但它不起作用。 错误是“ http://localhost/css/chunk-006c7b90.0199750b.css net::ERR_ABORTED 404 (Not Found) ”。 我可以看到这里没有端口。

如果我使用 http://localhost:3001、http://localhost:3002 访问应用程序...一切正常。

如果我使用

我的文件夹应用程序结构:

\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 配置:

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/;
        }
    }

//

您可以在 nginx 配置中定义上游,如果您有 websocket 尝试使用 ip_hash 选项

看一下 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM