MayorqueDev
MayorqueDev

Reputation: 31

how to proxy_pass api path to port with nginx

I want to proxy_pass the root location (/) to the port 3000 and the /api location to the port 5000, is completely possible, right?

my nginx config file:

server {
listen       80;
server_name  mywebsite.com;

location /api {
    proxy_pass http://localhost:5000;
}

location / {
    proxy_pass http://localhost:3000;
}
}

if I do the api request locally i can get the expected output:

myuser@myserver [conf.d]# curl localhost:5000
Hello, World!myuser@myserver [conf.d]#

but with an api client don't, and proxy_pass from the root path to the port 3000 works fine in the browser and the api client

Notes:

Upvotes: 2

Views: 15895

Answers (1)

Vishal Ghadage
Vishal Ghadage

Reputation: 185

I think you are using React and nodejs. I use below config.

server {
listen       80;
server_name  mywebsite.com;
location / {
# My react 
        proxy_pass http://localhost:3000;
        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 /api{
# This is my nodejs API 
        proxy_pass http://localhost: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;
    }
}

Upvotes: 8

Related Questions