Skip to content

Commit c86a092

Browse files
authored
Fix Nginx upstream name conflicts (#2526)
Previously, run names were used as upstream names in Nginx configs. Since run names can be duplicated in different projects, this could lead to name conflicts in Nginx configs. This commit solves the problem by constructing upstream names based on service domain names, which should be unique for all services across projects.
1 parent 686b374 commit c86a092

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

src/dstack/_internal/proxy/gateway/resources/nginx/service.jinja2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ limit_req_zone {{ zone.key }} zone={{ zone.name }}:10m rate={{ zone.rpm }}r/m;
33
{% endfor %}
44

55
{% if replicas %}
6-
upstream {{ run_name }} {
6+
upstream {{ domain }}.upstream {
77
{% for replica in replicas %}
88
server unix:{{ replica.socket }}; # replica {{ replica.id }}
99
{% endfor %}
@@ -37,7 +37,7 @@ server {
3737

3838
{% if replicas %}
3939
location @websocket {
40-
proxy_pass http://{{ run_name }};
40+
proxy_pass http://{{ domain }}.upstream;
4141
proxy_set_header X-Real-IP $remote_addr;
4242
proxy_set_header Host $host;
4343
proxy_http_version 1.1;
@@ -46,7 +46,7 @@ server {
4646
proxy_read_timeout 300s;
4747
}
4848
location @ {
49-
proxy_pass http://{{ run_name }};
49+
proxy_pass http://{{ domain }}.upstream;
5050
proxy_set_header X-Real-IP $remote_addr;
5151
proxy_set_header Host $host;
5252
proxy_read_timeout 300s;

src/dstack/_internal/proxy/gateway/services/nginx.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class LocationConfig(BaseModel):
5858
class ServiceConfig(SiteConfig):
5959
type: Literal["service"] = "service"
6060
project_name: str
61-
run_name: str
6261
auth: bool
6362
client_max_body_size: int
6463
access_log_path: Path

src/dstack/_internal/proxy/gateway/services/registry.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ async def get_nginx_service_config(
327327
domain=service.domain_safe,
328328
https=service.https_safe,
329329
project_name=service.project_name,
330-
run_name=service.run_name,
331330
auth=service.auth,
332331
client_max_body_size=service.client_max_body_size,
333332
access_log_path=ACCESS_LOG_PATH,

src/tests/_internal/proxy/gateway/routers/test_registry.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async def test_register(self, tmp_path: Path, system_mocks: Mocks) -> None:
112112
# no auth
113113
assert "auth_request /_dstack_auth;" not in conf
114114
# no replicas
115-
assert "upstream test-run" not in conf
115+
assert "upstream" not in conf
116116
assert "return 503;" in conf
117117

118118
async def test_register_with_https(self, tmp_path: Path, system_mocks: Mocks) -> None:
@@ -275,7 +275,7 @@ async def test_register(self, tmp_path: Path, system_mocks: Mocks) -> None:
275275
)
276276
assert resp.status_code == 200
277277
conf = (tmp_path / "443-test-run.gtw.test.conf").read_text()
278-
assert "upstream test-run" not in conf
278+
assert "upstream" not in conf
279279
# register 2 replicas
280280
resp = await client.post(
281281
"/api/registry/test-proj/services/test-run/replicas/register",
@@ -290,7 +290,7 @@ async def test_register(self, tmp_path: Path, system_mocks: Mocks) -> None:
290290
assert resp.status_code == 200
291291
assert resp.json() == {"status": "ok"}
292292
conf = (tmp_path / "443-test-run.gtw.test.conf").read_text()
293-
assert "upstream test-run" in conf
293+
assert "upstream test-run.gtw.test.upstream" in conf
294294
assert (m1 := re.search(r"server unix:/(.+)/replica.sock; # replica xxx-xxx", conf))
295295
assert (m2 := re.search(r"server unix:/(.+)/replica.sock; # replica yyy-yyy", conf))
296296
assert m1.group(1) != m2.group(1)

0 commit comments

Comments
 (0)