Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

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: e.g.:

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/;
    }
}
question from:https://stackoverflow.com/questions/65914912/node-js-django-configure-apache-nginx-to-run-from-location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...