簡體   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