Skip to content

Commit

Permalink
Merge pull request #25 from ComfyWorkflows/proxy_helpers
Browse files Browse the repository at this point in the history
Adding PROXY_MODE to support running the Launcher behind a proxy w/ only a single port exposed.
  • Loading branch information
thecooltechguy authored Mar 11, 2024
2 parents d6ec3d6 + 747d606 commit 9572a5f
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 155 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM python:3.10-slim

WORKDIR /app

RUN apt-get update && apt-get install -y nodejs npm gcc g++ make
RUN apt-get update && apt-get install -y nginx nodejs npm gcc g++ make && \
rm -rf /var/lib/apt/lists/*

COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
Expand All @@ -12,6 +13,9 @@ RUN cd /app/web && npm install && npm run build

COPY server /app/server

# Copy the Nginx configuration file into the container
COPY nginx.conf /etc/nginx/nginx.conf

WORKDIR /app/server

CMD ["./entrypoint.sh"]
CMD ["./entrypoint.sh"]
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,30 @@ git pull

## Usage

#### Using an existing ComfyUI models folder
### Using a reverse proxy (advanced)
If you're running ComfyUI Launcher behind a reverse proxy or in an environment where you can only expose a single port to access the Launcher and its workflow projects, you can run the Launcher with `PROXY_MODE=true` (only available for Docker).

```
docker run \
--gpus all \ # remove this line if you don't have a GPU or if you're on MacOS
--rm \
--name comfyui_launcher \
-p 4000:80 \
-v $(pwd)/comfyui_launcher_models:/app/server/models \
-v $(pwd)/comfyui_launcher_projects:/app/server/projects \
-e PROXY_MODE=true \
-it thecooltechguy/comfyui_launcher
```

Once the container is running, all you need to do is expose port 4000 to the outside world. This will allow you to access the Launcher and its workflow projects from a single port.

Currently, `PROXY_MODE=true` only works with Docker, since NGINX is used within the container.
If you're running the Launcher manually, you'll need to set up a reverse proxy yourself.

### Using an existing ComfyUI models folder
When starting the ComfyUI Launcher, you can set the `MODELS_DIR` environment variable to the path of your existing ComfyUI models folder. This will allow you to use the models you've already downloaded. By default, they're stored in `./server/models`

#### Using a different folder to store your Launcher projects
### Using a different folder to store your Launcher projects
When starting the ComfyUI Launcher, you can set the `PROJECTS_DIR` environment variable to the path of the folder you'd like to use to store your projects. By default, they're stored in `./server/projects`

## Donations
Expand Down
54 changes: 54 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
}

http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

gzip on;

include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;

# # Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;

location / {
proxy_pass http://127.0.0.1:4000;
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;
}

location ~ ^/comfy/(\d+)/ {
rewrite ^/comfy/(\d+)(/.*)?$ $2 break;
proxy_pass http://127.0.0.1:$1;
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 "upgrade";
}
}
}
6 changes: 6 additions & 0 deletions server/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ echo
celery -A server.celery_app --workdir=. worker --loglevel=INFO &
celery_worker_pid=$!

# if the environment variable PROXY_MODE is set to "true", start nginx
if [ "$PROXY_MODE" = "true" ]; then
echo "Starting Nginx reverse proxy (PROXY_MODE=true)..."
nginx -g "daemon off;" &
fi

python server.py

# kill Celery worker when server.py is done
Expand Down
13 changes: 10 additions & 3 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import torch
from flask import Flask, jsonify, request, render_template
from showinfm import show_in_file_manager
from settings import CELERY_BROKER_DIR, CELERY_RESULTS_DIR, PROJECTS_DIR, MODELS_DIR, TEMPLATES_DIR
from settings import ALLOW_OVERRIDABLE_PORTS_PER_PROJECT, CELERY_BROKER_DIR, CELERY_RESULTS_DIR, PROJECT_MAX_PORT, PROJECT_MIN_PORT, PROJECTS_DIR, MODELS_DIR, PROXY_MODE, TEMPLATES_DIR
import requests
import os, psutil, sys
from utils import (
Expand Down Expand Up @@ -65,6 +65,14 @@ def open_models_folder():
show_in_file_manager(MODELS_DIR)
return ""

@app.route("/api/settings")
def api_settings():
return jsonify({
"PROJECT_MIN_PORT": PROJECT_MIN_PORT,
"PROJECT_MAX_PORT": PROJECT_MAX_PORT,
"ALLOW_OVERRIDABLE_PORTS_PER_PROJECT": ALLOW_OVERRIDABLE_PORTS_PER_PROJECT,
"PROXY_MODE": PROXY_MODE
})

@app.route("/api/projects", methods=["GET"])
def list_projects():
Expand Down Expand Up @@ -157,7 +165,7 @@ def create_project():
if os.path.exists(template_workflow_json_fp):
with open(template_workflow_json_fp, "r") as f:
template_workflow_json = json.load(f)
res = get_launcher_json_for_workflow_json(template_workflow_json)
res = get_launcher_json_for_workflow_json(template_workflow_json, resolved_missing_models=[], skip_model_validation=True)
if (res["success"] and res["launcher_json"]):
launcher_json = res["launcher_json"]
else:
Expand All @@ -178,7 +186,6 @@ def create_project():
create_comfyui_project.delay(
project_path, models_path, id=id, name=name, launcher_json=launcher_json, port=port, create_project_folder=False
)
print("done")
return jsonify({"success": True, "id": id})


Expand Down
5 changes: 4 additions & 1 deletion server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
CELERY_RESULTS_DIR = os.path.join(os.environ.get("CELERY_DIR", ".celery"), "results")
CELERY_BROKER_DIR = os.path.join(os.environ.get("CELERY_DIR", ".celery"), "broker")

DISABLE_FIXED_PORTS = os.environ.get("DISABLE_FIXED_PORTS", "false").lower() == "true"
PROXY_MODE = os.environ.get("PROXY_MODE", "false").lower() == "true"
ALLOW_OVERRIDABLE_PORTS_PER_PROJECT = os.environ.get("ALLOW_OVERRIDABLE_PORTS_PER_PROJECT", "true").lower() == "true"
PROJECT_MIN_PORT = int(os.environ.get("PROJECT_MIN_PORT", "4001"))
PROJECT_MAX_PORT = int(os.environ.get("PROJECT_MAX_PORT", "4100"))
7 changes: 2 additions & 5 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import threading
from tqdm import tqdm
from urllib.parse import urlparse
from settings import PROJECTS_DIR
from settings import PROJECT_MAX_PORT, PROJECT_MIN_PORT, PROJECTS_DIR

def check_url_structure(url):
# Check for huggingface.co URL structure
Expand Down Expand Up @@ -54,9 +54,6 @@ def slugify(value, allow_unicode=False):

DEFAULT_CONFIG = {"credentials": {"civitai": {"apikey": ""}}}

START_PORT = 4001
END_PORT = 4100

import os
from typing import List, Dict, Optional, Union
import json
Expand Down Expand Up @@ -474,7 +471,7 @@ def get_project_port(id):
if os.path.exists(os.path.join(project_path, "port.txt")):
with open(os.path.join(project_path, "port.txt"), "r") as f:
return int(f.read().strip())
return find_free_port(START_PORT, END_PORT)
return find_free_port(PROJECT_MIN_PORT, PROJECT_MAX_PORT)

def is_port_in_use(port: int) -> bool:
import socket
Expand Down
37 changes: 24 additions & 13 deletions server/web/comfy_frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
<head>
<title>ComfyUI Launcher</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap" rel="stylesheet">
<style type="text/css">
.inter-400 {
font-family: "Inter", sans-serif;
font-optical-sizing: auto;
font-weight: 400;
font-style: normal;
font-variation-settings:
"slnt" 0;
}
</style>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap" rel="stylesheet">
<style type="text/css">
.inter-400 {
font-family: "Inter", sans-serif;
font-optical-sizing: auto;
font-weight: 400;
font-style: normal;
font-variation-settings: "slnt" 0;
}
</style>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
// Get the current URL's pathname
var currentPath = window.location.pathname;
// Ensure it ends with a "/"
if (!currentPath.endsWith('/')) {
currentPath += '/';
}
// Set the iframe src to currentPath + "comfyui_interface"
document.getElementById('comfyui-iframe').src = currentPath + "comfyui_interface";
});
</script>
</head>

<body style="margin: 0px; padding: 0px; height: 100%; width: 100%; display: flex; flex-direction: column;">
Expand All @@ -24,7 +35,7 @@
<div class="inter-400" style="margin-left: 10px;">ComfyUI Launcher</div>
</div>

<iframe src="/comfyui_interface" style="width: 100%; height: 100%; border: none;"></iframe>
<iframe id="comfyui-iframe" style="width: 100%; height: 100%; border: none;"></iframe>
</body>

</html>

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ComfyUI Launcher</title>
<script type="module" crossorigin src="/assets/index-BJzLC9xk.js"></script>
<script type="module" crossorigin src="/assets/index-iDLtCcjR.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BcHurkhF.css">
</head>
<body>
Expand Down
Loading

0 comments on commit 9572a5f

Please sign in to comment.