class_docstr: Nginx keeps track of registered domains, updates nginx config and issues SSL certificates.
  Its internal state could be serialized to a file and restored from it using pydantic.
class_docstr_tok: ["Nginx","keeps","track","of","registered","domains",",","updates","nginx","config","and","issues","SSL","certificates",".","Its","internal","state","could","be","serialized","to","a","file","and","restored","from","it","using","pydantic","."]
code: def get_service_conf(
      cls, domain: str, server: str, upstream: Optional[str] = None
  ) -> dict:
      if upstream is None:
          upstream = re.sub(
              r"[^a-z0-9_.\-]", "_", server, flags=re.IGNORECASE
          )
      return {
          f"upstream {upstream}": {
              "server": server,
          },
          "server": {
              "server_name": domain,
              "location /": {
                  # the first location is required, always fallback to the @-location
                  "try_files": "/nonexistent @$http_upgrade",
              },
              "location @websocket": {
                  "proxy_pass": f"http://{upstream}",
                  "proxy_set_header X-Real-IP": "$remote_addr",
                  "proxy_set_header Host": "$host",
                  # web socket related headers
                  "proxy_http_version": "1.1",
                  "proxy_set_header Upgrade": "$http_upgrade",
                  "proxy_set_header Connection": '"Upgrade"',
              },
              "location @": {
                  "proxy_pass": f"http://{upstream}",
                  "proxy_set_header X-Real-IP": "$remote_addr",
                  "proxy_set_header Host": "$host",
              },
              "listen": "80",
              **cls.get_ssl_conf(domain),
          },
      }
  
file_path: dstack/gateway/src/dstack/gateway/services/nginx.py
imports: import logging
  import re
  import subprocess
  import tempfile
  from asyncio import Lock
  from functools import lru_cache
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Set, Tuple, Union
  from pydantic import BaseModel
  from dstack.gateway.common import run_async
  from dstack.gateway.errors import GatewayError
label_desc: Use image node_id 1 for calling the Nginx obj's underlying member method code with example usage: obj.get_service_conf(cls, domain, server, upstream) and returns: dict