How to proxy locally using NGINX

63 views
Skip to first unread message

Bairon Paz

unread,
Nov 27, 2024, 10:00:42 PM11/27/24
to grpc.io

Hi yall, I am trying to set up a proxy for my gRPC server.

I am using NGINX as a reverse proxy locally ran using docker-compose. My idea is to run the following:

api.domain.com/api to my regular Express server and api.domain.com/grpc my regular grpc server.

I have the following on my nginx.conf

events {
  worker_connections 1024;
}

http {

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    # All other servers, eg: admin dashboard, client website etc


    server {
        listen 80;
        http2 on;
        server_name http://api.domain.com;

        location /api {
            proxy_pass http://host.docker.internal:5001;
            proxy_set_header Host $host;
            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;

            # WebSocket support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

        location /grpc {
            grpc_pass grpc://host.docker.internal:50051;
        }
    }

}


I am using nginx:alpine.

Calling grpc://host.docker.internal:50051 on postman works fine but trying to call http:api.dev-local.com/grpc wont work.

curl -I on the domain shows HTTP/1.1 regardless of setting : http2 on;.
Now I also plan to put this in a EC2 server for production, I use nginx there but I think its gonna be easier to set it up using ALB.

Any ideas on why this is not working?

mehrdad...@gmail.com

unread,
Dec 2, 2024, 2:12:54 AM12/2/24
to grpc.io
Standard gRPC/Protobuf servicers are by default attached to specific paths like "/ServiceName/MethodName" not under "/grpc" so you need to proxy those specific paths in nginx config

--
Mehrdad Afshari
Reply all
Reply to author
Forward
0 new messages