Browse Source

fix: enable proxy headers for SSL reverse proxy support in deployment

Zubarev Grigoriy 7 months ago
parent
commit
46c6e82387
4 changed files with 26 additions and 16 deletions
  1. 4 2
      Dockerfile
  2. 18 2
      README.md
  3. 4 1
      pyproject.toml
  4. 0 11
      src/rural_dict/__main__.py

+ 4 - 2
Dockerfile

@@ -1,3 +1,5 @@
 FROM python:3.12.4-alpine
-RUN --mount=source=dist,target=/dist PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir --disable-pip-version-check /dist/*.whl
-CMD [ "python", "-m", "rural_dict" ]
+RUN --mount=source=dist,target=/dist PYTHONDONTWRITEBYTECODE=1 \
+    pip install --no-cache-dir --disable-pip-version-check /dist/*.whl
+CMD [ "uvicorn", "rural_dict.__main__:app", "--no-access-log", "--proxy-headers", \
+    "--forwarded-allow-ips", "*", "--host", "0.0.0.0", "--port", "5758" ]

+ 18 - 2
README.md

@@ -47,7 +47,23 @@ docker compose up -d
 
 ```sh
 rye sync --no-dev
-rye run uvicorn src.rural_dict.__main__:app --port 5758
+rye run uvicorn src.rural_dict.__main__:app --no-access-log --proxy-headers \
+    --forwarded-allow-ips '*' --host 0.0.0.0 --port 5758
+```
+
+### 🛡️ Running behind a reverse proxy
+
+To run the app behind a reverse proxy, ensure that the appropriate proxy headers are added.
+Below is a sample configuration for NGINX:
+
+```text
+location / {
+    proxy_pass http://127.0.0.1:5758;
+    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;
+}
 ```
 
 ## 🔧 Development
@@ -84,7 +100,7 @@ to:
 
 ## 👥 Contributors
 
-- [thirtysix](https://thirtysix.pw), rewrote project in more modern Python stack
+- [thirtysix](https://thirtysix.pw), rewrote project in a more modern libraries stack
 - [zortazert](https://codeberg.org/zortazert), created the initial Urban Dictionary frontend using
   JavaScript and helped develop Rural Dictionary
 

+ 4 - 1
pyproject.toml

@@ -35,7 +35,10 @@ dev-dependencies = [
 ]
 
 [tool.rye.scripts]
-dev = "uvicorn src.rural_dict.__main__:app --port 5758 --reload --reload-dir src/rural_dict"
+dev = """uvicorn src.rural_dict.__main__:app --reload --reload-dir src/rural_dict
+--reload-include '**/*.py' --reload-include '**/*.html' --reload-include '**/*.css' --port 5758"""
+start = """uvicorn src.rural_dict.__main__:app --no-access-log --proxy-headers
+--forwarded-allow-ips '*' --host 0.0.0.0 --port 5758"""
 
 [tool.hatch.version]
 path = "src/rural_dict/__init__.py"

+ 0 - 11
src/rural_dict/__main__.py

@@ -124,14 +124,3 @@ async def catch_all(response: Request):
             "site_description": site_description,
         },
     )
-
-
-def main():
-    """Run the app in production mode. It is intended to be executed within a container."""
-    import uvicorn
-
-    uvicorn.run(app, host="0.0.0.0", port=5758, access_log=False)  # noqa: S104
-
-
-if __name__ == "__main__":
-    main()