简体   繁体   中英

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.

I´d like to use:

http://localhost/node-app-01 to access app-01 in the port 3001

http://localhost/node-app-02 to access app-02 in the port 3002 and so on.

But It´s not working. The error is " 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.

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:

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

take a look at the example in https tls configuration:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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