grpc-webとnginxをhttpsで利用する(構成2)

諸事情により、単一オリジンでgrpcとhttpsのサーバを運用が必要なことがわかった。 grpcのパスはルートでないと動作しなかったので、httpsサーバのパスを/app/にして妥協した。 またnginxのconfを晒す。

ちなみに、nginxからgrpcサーバ(localhost:9090)への通信はhttpにしている。

server {
  listen 443 ssl http2;
  server_name xxx.net;

  location / {
    if ($request_method = 'GET') {
      return 301 https://$host/app/;
    }

    grpc_set_header Content-Type application/grpc;
    grpc_pass localhost:9090;
  }

  location /app/ {
     root   /usr/share/nginx/html;
     index  index.html index.htm;
  }
  ssl_certificate /etc/letsencrypt/live/xxx.net/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/xxx.net/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

grpcは基本POSTなので、GETは/app/にリダイレクトしている。