Use caddy instead of nginx (fixes 404 handling issues)

This commit is contained in:
JSH32 2022-09-23 00:09:32 -05:00
parent 065f440db1
commit 28ff9bd5b8
10 changed files with 48 additions and 55 deletions

View File

@ -33,6 +33,9 @@ JWT_KEY=
# This will usually be the same as API_URL if using compose
CLIENT_URL=http://localhost:3000
# File upload limit in megabytes
FILE_SIZE_LIMIT=100
# --------------------------------- STORAGE --------------------------------
# How files should be stored

View File

@ -24,7 +24,7 @@ COPY ./.git ./.git
RUN cargo build --release
# Running stage
FROM rust:1.60
FROM gcr.io/distroless/cc
# Copy the build artifact from the build stage
COPY --from=build /backpack/target/release/backpack .

View File

@ -18,10 +18,12 @@ services:
PORT: 3000
INTERNAL_API_URL: "http://backpack_api:3000"
proxy:
build: ./nginx
ports:
- ${PORT}:80
build: ./proxy
env_file: .env
ports:
- ${PORT}:3000
volumes:
- $PWD/proxy/Caddyfile:/etc/caddy/Caddyfile
depends_on:
- backpack_api
- backpack_frontend

View File

@ -1,5 +0,0 @@
FROM nginx
COPY default.conf.template /etc/nginx/templates/default.conf.template
CMD ["nginx", "-g", "daemon off;"]

View File

@ -1,8 +0,0 @@
## Docker proxy
This is an NGINX proxy for simple users who choose to use the `docker-compose` method of hosting. This will automatically run the API and client through a single port and handle local file storage.
### Environment Variables
- PROXY_FILES
Should files be proxied to the API. This should likely be enabled if using both `LOCAL_SERVE` and local `STORAGE_PROVIDER`
- FILE_SIZE_LIMIT
File or request limit in mb (megabytes)

View File

@ -1,38 +0,0 @@
upstream backpack_api {
server backpack_api:3000;
}
upstream backpack_frontend {
server backpack_frontend:3000;
}
server {
listen 80;
proxy_intercept_errors ${PROXY_FILES};
client_max_body_size ${FILE_SIZE_LIMIT}m;
location / {
proxy_pass http://backpack_frontend;
proxy_redirect off;
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-Host $server_name;
error_page 404 @file_error_handle;
}
location /api {
proxy_pass http://backpack_api;
proxy_redirect off;
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-Host $server_name;
}
# Redirect 404 on main site to file serving on the API
location @file_error_handle {
proxy_pass http://backpack_api;
}
}

30
proxy/Caddyfile Normal file
View File

@ -0,0 +1,30 @@
:3000 {
@proxy_files_on vars {env.PROXY_FILES} "on"
@proxy_files_off expression `{env.PROXY_FILES} != "on"`
# This is kind of jank but works for our needs.
handle /* {
reverse_proxy @proxy_files_on backpack_frontend:3000 {
# We assume that this is looking for a file on the backend at "/".
@not_found status 404
handle_response @not_found {
handle @proxy_files_on {
reverse_proxy backpack_api:3000 {
# Backend gave a 404 (no file was found) so we go back to frontend to display 404.
@not_found status 404
handle_response @not_found {
reverse_proxy backpack_frontend:3000
}
}
}
}
}
# Proxying files is off.
reverse_proxy @proxy_files_off backpack_frontend:3000
}
handle /api/* {
reverse_proxy backpack_api:3000
}
}

2
proxy/Dockerfile Normal file
View File

@ -0,0 +1,2 @@
FROM caddy:latest
COPY Caddyfile /etc/caddy/Caddyfile

6
proxy/README.md Normal file
View File

@ -0,0 +1,6 @@
## Docker proxy
This is a Caddy proxy for simple users who choose to use the `docker-compose` method of hosting. This will automatically run the API and client through a single port and handle local file storage.
### Environment Variables
- PROXY_FILES
Should files be proxied to the API. This should likely be enabled if using both `LOCAL_SERVE` and local `STORAGE_PROVIDER`

View File

@ -186,6 +186,7 @@ async fn main() -> std::io::Result<()> {
.allowed_origin(&client_url)
.allow_any_header()
.allow_any_method()
.supports_credentials()
.max_age(None),
)
.app_data(database.clone())