Nginx在OS X的配置

huangapple go评论105阅读模式
英文:

Nginx configuration in OS X

问题

以下是翻译好的内容:

我想为 gRPC 客户端和服务器实现服务器端负载平衡,并尝试创建适用于 OS X 的简单 Nginx 配置。我有两个运行在端口 50051 和 50052 上的服务器,以下是我启动服务器的代码:

  1. Server server = ServerBuilder.forPort(50051)
  2. .addService(new ImageStreamingServerImpl())
  3. .addService(ProtoReflectionService.newInstance())
  4. // .useTransportSecurity(
  5. // new File("ssl/server.crt"),
  6. // new File("ssl/server.pem")
  7. // )
  8. .build();
  9. server.start();

这是我的 Nginx 配置:

  1. worker_processes 1;
  2. error_log /usr/local/var/log/nginx/error.log;
  3. events {
  4. worker_connections 10;
  5. }
  6. http {
  7. access_log /usr/local/var/log/nginx/access.log;
  8. upstream backend {
  9. server 0.0.0.0:50051;
  10. server 0.0.0.0:50052;
  11. }
  12. server {
  13. listen 8080;
  14. location / {
  15. grpc_pass grpc://backend;
  16. }
  17. }
  18. }

所以我的想法是客户端可以通过 Nginx 上的端口 8080 进行监听,并执行与之前相同的代码。客户端的创建如下:

  1. ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
  2. .usePlaintext()
  3. .build();
  4. // ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", 8080)
  5. // .sslContext(GrpcSslContexts.forClient().trustManager(new File("ssl/ca.crt")).build())
  6. // .build();
  7. FileServiceGrpc.FileServiceStub imageStreamingClient = FileServiceGrpc.newStub(channel);

以前,客户端和服务器可以在同一个端口上(例如,50051 或 50052)良好运行,而不需要 Nginx。为什么现在不起作用呢?

以下是提供的 proto 文件内容:

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. package com.grpc.protobuf;
  4. message DownloadFileRequest {
  5. string url = 1;
  6. }
  7. message DataChunk {
  8. bytes data = 1;
  9. int32 size = 2;
  10. }
  11. service FileService {
  12. rpc downloadFile (DownloadFileRequest) returns (stream DataChunk);
  13. }

我为了简化目的省略了 TLS 加密部分。

英文:

I want to server-side load balance for gRPC client and server and trying to come up with simple Nginx config for the OS X. I have 2 servers running on the port 50051 and 50052 and this is I start the server:

  1. Server server = ServerBuilder.forPort(50051)
  2. .addService(new ImageStreamingServerImpl())
  3. .addService(ProtoReflectionService.newInstance())
  4. // .useTransportSecurity(
  5. // new File("ssl/server.crt"),
  6. // new File("ssl/server.pem")
  7. // )
  8. .build();
  9. server.start();

This is my Nginx config:

  1. worker_processes 1;
  2. error_log /usr/local/var/log/nginx/error.log;
  3. events {
  4. worker_connections 10;
  5. }
  6. http {
  7. access_log /usr/local/var/log/nginx/access.log;
  8. upstream backend {
  9. server 0.0.0.0:50051;
  10. server 0.0.0.0:50052;
  11. }
  12. server {
  13. listen 8080;
  14. location / {
  15. grpc_pass grpc://backend;
  16. }
  17. }
  18. }

So my idea is the client can listen to the port of 8080 over the Nginx and perform the code as before. Client creation is below:

  1. ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
  2. .usePlaintext()
  3. .build();
  4. // ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", 8080)
  5. // .sslContext(GrpcSslContexts.forClient().trustManager(new File("ssl/ca.crt")).build())
  6. // .build();
  7. FileServiceGrpc.FileServiceStub imageStreamingClient = FileServiceGrpc.newStub(channel);

Previously, the client and server can run decently in the same port (say, 50051 or 50052) without Nginx. Why does this not working now?

The proto file is provided below:

syntax = "proto3";

option java_multiple_files = true;
package com.grpc.protobuf;

  1. message DownloadFileRequest {
  2. string url = 1;
  3. }
  4. message DataChunk {
  5. bytes data = 1;
  6. int32 size = 2;
  7. }
  8. service FileService {
  9. rpc downloadFile (DownloadFileRequest) returns (stream DataChunk);
  10. }

I omit the TLS encryption for the simplification purpose.

答案1

得分: 4

这似乎你还没有配置Nginx来启用HTTP2,而这是gRPC使用的协议。

尝试使用:

  1. server {
  2. listen 8080 http2;
  3. ....
  4. }
英文:

It doesn't look like you have configured Nginx to enabled HTTP2, which is the protocol used by gRPC.

Try with:

  1. server {
  2. listen 8080 http2;
  3. ....
  4. }

huangapple
  • 本文由 发表于 2020年10月22日 22:57:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64484967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定