简体   繁体   中英

node.JS / Django - configure Apache / NGINX to run from location

I am using Apache as my frontend VPS / NGINX as backend VPS for node.js / django apps with react.js frontend.

I am trying to run my apps from same VPS, but from different locations: eg:

https://myapps/app1
https://myappps/app2

Previously I have tackled this issue by adding the location in the router of the apps - however I am not sure if this is the best way to do this. Can I actually add the location in the VPS config of NGINX or Apache?

# nginx.default

server {
    listen 8020;
    server_name example.org;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        root /opt/app/client/build/;
    }
}

If you are running two node apps it could be done using this NGINX config. Just make sure app1 and app2 are on different ports.

server {
        listen 80;

        server_name localhost;

        location /app1 {
            proxy_pass http://127.0.0.1:5000;
            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 /app2{
            proxy_pass http://127.0.0.1:5001;
            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;
        }
    }

EDIT: This is how I have multiple sites running *note: server name should be set to the domain, but if you dont have access to domains set them as the IP:PORT shown in example below. Reason why server name cannot be local is it needs to be public for access. In my use case for website 1 server_name is website1.com then website 2 server_name is website2.com (My domains)

server {
        listen 5000;
        //APP 1
        server_name 10.0.0.1:5000;

        location /{
            proxy_pass http://127.0.0.1:5000;
            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;
        }
    }
    
    server {
        listen 5001;
        //APP 2
        server_name 10.0.0.1:5001;

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

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