Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhanced nginx settings #203

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ ENV NGINX_VERSION=${NGINX_VERSION}
ARG NGX_HTTP_REDIS_VERSION
ENV NGX_HTTP_REDIS_VERSION=${NGX_HTTP_REDIS_VERSION}

# Copy specific configuration files
COPY nginx.conf /etc/nginx/nginx.conf
COPY overfast-api.conf /etc/nginx/conf.d/default.conf

# Download nginx and ngx_http_redis sources
RUN wget https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
tar -xzvf nginx-$NGINX_VERSION.tar.gz && \
Expand All @@ -34,6 +30,10 @@ RUN cd nginx-$NGINX_VERSION && \
make modules && \
cp objs/ngx_http_redis_module.so /usr/lib/nginx/modules/ngx_http_redis_module.so

# Copy specific configuration files
COPY nginx.conf /etc/nginx/nginx.conf
COPY overfast-api.conf /etc/nginx/conf.d/default.conf

# Clean everything
RUN rm -rf nginx-$NGINX_VERSION* && \
rm -rf ngx_http_redis-$NGX_HTTP_REDIS_VERSION*
55 changes: 55 additions & 0 deletions build/nginx/overfast-api.conf
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
server {
listen 80;

# Use nginx to serve static content
location /static {
alias /static;
try_files $uri =404;

sendfile on;
tcp_nopush on;

expires 1d;
add_header Cache-Control public;
}

# Favicon
location /favicon.png {
alias /static/favicon.png;
}
location /favicon.ico {
alias /static/favicon.ico;
}

# Redirect trailing slashes to routes without slashes
location ~ (?<no_slash>.+)/$ {
return 301 $scheme://$host$no_slash;
}

# Main route
location / {
# Always authorize any origin as it's a public API
add_header Access-Control-Allow-Origin "*" always;

# Handle OPTIONS method here
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Methods "GET, HEAD, OPTIONS";
add_header Access-Control-Allow-Headers "*";
return 204;
}

# Default behaviour using Redis cache
default_type application/json;
set $redis_key "api-cache:$request_uri";
redis_pass redisbackend;

# Fallback to app if data not in cache
error_page 404 502 504 = @fallback;
}

# FastAPI app fallback
location @fallback {
# As we're in fallback, we need to specify the header again
add_header Access-Control-Allow-Origin "*" always;

# Main proxy method
proxy_pass http://appbackend;

# Ensure headers are forwarded
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;
proxy_pass_request_headers on;

# Ensure HEAD requests are passed as GET
proxy_method GET;
}
}
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
command: /code/.venv/bin/fastapi run app/main.py --port 8080 --proxy-headers
env_file: ${APP_VOLUME_PATH:-.}/.env
volumes:
- static_volume:/code/static
- ${APP_VOLUME_PATH:-/tmp}/app-logs:/code/logs
depends_on:
redis:
Expand Down Expand Up @@ -53,6 +54,8 @@ services:
context: ./build/nginx
ports:
- "${APP_PORT}:80"
volumes:
- static_volume:/static
depends_on:
app:
condition: service_healthy
Expand All @@ -61,4 +64,7 @@ services:
healthcheck:
test: ["CMD-SHELL", "wget --spider --quiet http://localhost || exit 1"]
interval: 5s
timeout: 2s
timeout: 2s

volumes:
static_volume:
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "overfast-api"
version = "2.38.0"
version = "2.39.0"
description = "Overwatch API giving data about heroes, maps, and players statistics."
license = {file = "LICENSE"}
authors = [
Expand Down
Binary file added static/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.