diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template index 52347c6e..24a2cd5f 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -1,3 +1,17 @@ +upstream nats_fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + +upstream fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + upstream mesop_backend { # Enable sticky sessions with IP hash ip_hash; @@ -18,9 +32,47 @@ map $fly_machine_id $sticky_action { default "replay"; # Cookie exists but doesn't match - need to replay } -# Main server block +# Fastapi server block +server { + listen $FASTAPI_PORT; + server_name localhost; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; + add_header X-XSS-Protection "1; mode=block"; + + location / { + # Handle cookie setting + if ($sticky_action = "set_cookie") { + add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/"; + } + + # Handle replay + if ($sticky_action = "replay") { + add_header Fly-Replay "instance=$fly_machine_id"; + return 307; + } + + proxy_pass http://fastapi_backend; + 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_redirect off; + proxy_buffering off; + + # WSGI support + proxy_set_header X-Forwarded-Host $server_name; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +# Mesop server block server { - listen $SERVICE_PORT; + listen $MESOP_PORT; server_name localhost; # Security headers diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh index 548457f6..77d9d2a9 100755 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -1,13 +1,9 @@ #!/bin/bash # Accept env variable for PORT - - -FASTAPI_PORT=${FASTAPI_PORT:-8008} - - +export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} +export FASTAPI_PORT=${FASTAPI_PORT:-8008} export MESOP_PORT=${MESOP_PORT:-8888} -export SERVICE_PORT=$MESOP_PORT # Default number of workers if not set WORKERS=${WORKERS:-1} @@ -20,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID" # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) + sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf + +envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf @@ -32,14 +41,20 @@ nginx -g "daemon off;" & # Run uvicorn server -uvicorn my_fastagency_app.deployment.main_1_fastapi:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 & +# Start multiple single-worker uvicorn instances on consecutive ports +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + echo "Starting fastapi uvicorn on port $PORT" + uvicorn my_fastagency_app.deployment.main_1_fastapi:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 & +done # Run gunicorn server # Start multiple single-worker gunicorn instances on consecutive ports for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) echo "Starting gunicorn on port $PORT" gunicorn --workers=1 my_fastagency_app.deployment.main_2_mesop:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 & done diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/fly.toml b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/fly.toml index 742cfd80..d19237b6 100644 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/fly.toml +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/fly.toml @@ -21,3 +21,30 @@ primary_region = 'ams' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[services]] + http_checks = [] + internal_port = 8008 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8008 +[[services]] + http_checks = [] + internal_port = 8888 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8888 diff --git a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh index a49aac06..bfd9711a 100755 --- a/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh +++ b/docs/docs_src/getting_started/basic_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -17,7 +17,7 @@ else echo -e "\033[0;32mAlready logged into fly.io\033[0m" fi -export FLY_APP_NAME=my-fastagency-app +export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}') echo -e "\033[0;32mRegistering app name in fly.io\033[0m" if flyctl apps create $FLY_APP_NAME; then @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then cat registered_app_domain.txt else echo -e "\033[1;31mError: App name is not available.\033[0m" - echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m" + echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m" exit 1 fi diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template index 52347c6e..626bce1c 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template @@ -1,3 +1,17 @@ +upstream nats_fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + +upstream fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + upstream mesop_backend { # Enable sticky sessions with IP hash ip_hash; @@ -18,9 +32,9 @@ map $fly_machine_id $sticky_action { default "replay"; # Cookie exists but doesn't match - need to replay } -# Main server block +# Mesop server block server { - listen $SERVICE_PORT; + listen $MESOP_PORT; server_name localhost; # Security headers diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh index 55aa17d3..a6cb4cf0 100755 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh @@ -1,11 +1,9 @@ #!/bin/bash # Accept env variable for PORT - - - +export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} +export FASTAPI_PORT=${FASTAPI_PORT:-8008} export MESOP_PORT=${MESOP_PORT:-8888} -export SERVICE_PORT=$MESOP_PORT # Default number of workers if not set WORKERS=${WORKERS:-1} @@ -18,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID" # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) + sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf + +envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf @@ -29,15 +40,12 @@ cat /etc/nginx/conf.d/default.conf nginx -g "daemon off;" & -# Run uvicorn server -uvicorn my_fastagency_app.deployment.main_:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 & - # Run gunicorn server # Start multiple single-worker gunicorn instances on consecutive ports for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) echo "Starting gunicorn on port $PORT" gunicorn --workers=1 my_fastagency_app.deployment.main:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 & done diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/fly.toml b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/fly.toml index 742cfd80..ed002a65 100644 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/fly.toml +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/fly.toml @@ -21,3 +21,17 @@ primary_region = 'ams' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[services]] + http_checks = [] + internal_port = 8888 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8888 diff --git a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh index a49aac06..bfd9711a 100755 --- a/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh +++ b/docs/docs_src/getting_started/basic_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh @@ -17,7 +17,7 @@ else echo -e "\033[0;32mAlready logged into fly.io\033[0m" fi -export FLY_APP_NAME=my-fastagency-app +export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}') echo -e "\033[0;32mRegistering app name in fly.io\033[0m" if flyctl apps create $FLY_APP_NAME; then @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then cat registered_app_domain.txt else echo -e "\033[1;31mError: App name is not available.\033[0m" - echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m" + echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m" exit 1 fi diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template index 52347c6e..3e295c7b 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -1,3 +1,17 @@ +upstream nats_fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + +upstream fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + upstream mesop_backend { # Enable sticky sessions with IP hash ip_hash; @@ -18,9 +32,85 @@ map $fly_machine_id $sticky_action { default "replay"; # Cookie exists but doesn't match - need to replay } -# Main server block +# Nats fastapi server block +server { + listen $NATS_FASTAPI_PORT; + server_name localhost; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; + add_header X-XSS-Protection "1; mode=block"; + + location / { + # Handle cookie setting + if ($sticky_action = "set_cookie") { + add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/"; + } + + # Handle replay + if ($sticky_action = "replay") { + add_header Fly-Replay "instance=$fly_machine_id"; + return 307; + } + + proxy_pass http://nats_fastapi_backend; + 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_redirect off; + proxy_buffering off; + + # WSGI support + proxy_set_header X-Forwarded-Host $server_name; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +# Fastapi server block +server { + listen $FASTAPI_PORT; + server_name localhost; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; + add_header X-XSS-Protection "1; mode=block"; + + location / { + # Handle cookie setting + if ($sticky_action = "set_cookie") { + add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/"; + } + + # Handle replay + if ($sticky_action = "replay") { + add_header Fly-Replay "instance=$fly_machine_id"; + return 307; + } + + proxy_pass http://fastapi_backend; + 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_redirect off; + proxy_buffering off; + + # WSGI support + proxy_set_header X-Forwarded-Host $server_name; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +# Mesop server block server { - listen $SERVICE_PORT; + listen $MESOP_PORT; server_name localhost; # Security headers diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh index 1db12dbb..0ba28573 100755 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -1,15 +1,9 @@ #!/bin/bash # Accept env variable for PORT - -NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} - - -FASTAPI_PORT=${FASTAPI_PORT:-8008} - - +export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} +export FASTAPI_PORT=${FASTAPI_PORT:-8008} export MESOP_PORT=${MESOP_PORT:-8888} -export SERVICE_PORT=$MESOP_PORT # Default number of workers if not set WORKERS=${WORKERS:-1} @@ -22,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID" # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) + sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf + +envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf @@ -33,18 +40,30 @@ cat /etc/nginx/conf.d/default.conf nginx -g "daemon off;" & # Run nats uvicorn server -uvicorn my_fastagency_app.deployment.main_1_nats:app --host 0.0.0.0 --port $NATS_FASTAPI_PORT > /dev/stdout 2>&1 & +# Start multiple single-worker uvicorn instances on consecutive ports +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) + echo "Starting nats fastapi uvicorn on port $PORT" + uvicorn my_fastagency_app.deployment.main_1_nats:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 & +done # Run uvicorn server -uvicorn my_fastagency_app.deployment.main_2_fastapi:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 & +# Start multiple single-worker uvicorn instances on consecutive ports +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + echo "Starting fastapi uvicorn on port $PORT" + uvicorn my_fastagency_app.deployment.main_2_fastapi:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 & +done # Run gunicorn server # Start multiple single-worker gunicorn instances on consecutive ports for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) echo "Starting gunicorn on port $PORT" gunicorn --workers=1 my_fastagency_app.deployment.main_3_mesop:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 & done diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/fly.toml b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/fly.toml index 742cfd80..ef085a35 100644 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/fly.toml +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/fly.toml @@ -21,3 +21,43 @@ primary_region = 'ams' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[services]] + http_checks = [] + internal_port = 8000 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8000 +[[services]] + http_checks = [] + internal_port = 8008 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8008 +[[services]] + http_checks = [] + internal_port = 8888 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8888 diff --git a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh index a49aac06..bfd9711a 100755 --- a/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh +++ b/docs/docs_src/getting_started/basic_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -17,7 +17,7 @@ else echo -e "\033[0;32mAlready logged into fly.io\033[0m" fi -export FLY_APP_NAME=my-fastagency-app +export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}') echo -e "\033[0;32mRegistering app name in fly.io\033[0m" if flyctl apps create $FLY_APP_NAME; then @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then cat registered_app_domain.txt else echo -e "\033[1;31mError: App name is not available.\033[0m" - echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m" + echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m" exit 1 fi diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template index 52347c6e..24a2cd5f 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -1,3 +1,17 @@ +upstream nats_fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + +upstream fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + upstream mesop_backend { # Enable sticky sessions with IP hash ip_hash; @@ -18,9 +32,47 @@ map $fly_machine_id $sticky_action { default "replay"; # Cookie exists but doesn't match - need to replay } -# Main server block +# Fastapi server block +server { + listen $FASTAPI_PORT; + server_name localhost; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; + add_header X-XSS-Protection "1; mode=block"; + + location / { + # Handle cookie setting + if ($sticky_action = "set_cookie") { + add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/"; + } + + # Handle replay + if ($sticky_action = "replay") { + add_header Fly-Replay "instance=$fly_machine_id"; + return 307; + } + + proxy_pass http://fastapi_backend; + 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_redirect off; + proxy_buffering off; + + # WSGI support + proxy_set_header X-Forwarded-Host $server_name; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +# Mesop server block server { - listen $SERVICE_PORT; + listen $MESOP_PORT; server_name localhost; # Security headers diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh index 548457f6..77d9d2a9 100755 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -1,13 +1,9 @@ #!/bin/bash # Accept env variable for PORT - - -FASTAPI_PORT=${FASTAPI_PORT:-8008} - - +export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} +export FASTAPI_PORT=${FASTAPI_PORT:-8008} export MESOP_PORT=${MESOP_PORT:-8888} -export SERVICE_PORT=$MESOP_PORT # Default number of workers if not set WORKERS=${WORKERS:-1} @@ -20,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID" # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) + sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf + +envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf @@ -32,14 +41,20 @@ nginx -g "daemon off;" & # Run uvicorn server -uvicorn my_fastagency_app.deployment.main_1_fastapi:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 & +# Start multiple single-worker uvicorn instances on consecutive ports +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + echo "Starting fastapi uvicorn on port $PORT" + uvicorn my_fastagency_app.deployment.main_1_fastapi:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 & +done # Run gunicorn server # Start multiple single-worker gunicorn instances on consecutive ports for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) echo "Starting gunicorn on port $PORT" gunicorn --workers=1 my_fastagency_app.deployment.main_2_mesop:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 & done diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/fly.toml b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/fly.toml index 742cfd80..d19237b6 100644 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/fly.toml +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/fly.toml @@ -21,3 +21,30 @@ primary_region = 'ams' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[services]] + http_checks = [] + internal_port = 8008 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8008 +[[services]] + http_checks = [] + internal_port = 8888 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8888 diff --git a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh index a49aac06..bfd9711a 100755 --- a/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh +++ b/docs/docs_src/getting_started/no_auth/fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -17,7 +17,7 @@ else echo -e "\033[0;32mAlready logged into fly.io\033[0m" fi -export FLY_APP_NAME=my-fastagency-app +export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}') echo -e "\033[0;32mRegistering app name in fly.io\033[0m" if flyctl apps create $FLY_APP_NAME; then @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then cat registered_app_domain.txt else echo -e "\033[1;31mError: App name is not available.\033[0m" - echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m" + echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m" exit 1 fi diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template index 52347c6e..626bce1c 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/nginx.conf.template @@ -1,3 +1,17 @@ +upstream nats_fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + +upstream fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + upstream mesop_backend { # Enable sticky sessions with IP hash ip_hash; @@ -18,9 +32,9 @@ map $fly_machine_id $sticky_action { default "replay"; # Cookie exists but doesn't match - need to replay } -# Main server block +# Mesop server block server { - listen $SERVICE_PORT; + listen $MESOP_PORT; server_name localhost; # Security headers diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh index 55aa17d3..a6cb4cf0 100755 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/docker/content/run_fastagency.sh @@ -1,11 +1,9 @@ #!/bin/bash # Accept env variable for PORT - - - +export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} +export FASTAPI_PORT=${FASTAPI_PORT:-8008} export MESOP_PORT=${MESOP_PORT:-8888} -export SERVICE_PORT=$MESOP_PORT # Default number of workers if not set WORKERS=${WORKERS:-1} @@ -18,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID" # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) + sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf + +envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf @@ -29,15 +40,12 @@ cat /etc/nginx/conf.d/default.conf nginx -g "daemon off;" & -# Run uvicorn server -uvicorn my_fastagency_app.deployment.main_:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 & - # Run gunicorn server # Start multiple single-worker gunicorn instances on consecutive ports for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) echo "Starting gunicorn on port $PORT" gunicorn --workers=1 my_fastagency_app.deployment.main:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 & done diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/fly.toml b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/fly.toml index 742cfd80..ed002a65 100644 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/fly.toml +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/fly.toml @@ -21,3 +21,17 @@ primary_region = 'ams' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[services]] + http_checks = [] + internal_port = 8888 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8888 diff --git a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh index a49aac06..bfd9711a 100755 --- a/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh +++ b/docs/docs_src/getting_started/no_auth/mesop/my_fastagency_app/scripts/register_to_fly_io.sh @@ -17,7 +17,7 @@ else echo -e "\033[0;32mAlready logged into fly.io\033[0m" fi -export FLY_APP_NAME=my-fastagency-app +export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}') echo -e "\033[0;32mRegistering app name in fly.io\033[0m" if flyctl apps create $FLY_APP_NAME; then @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then cat registered_app_domain.txt else echo -e "\033[1;31mError: App name is not available.\033[0m" - echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m" + echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m" exit 1 fi diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template index 52347c6e..3e295c7b 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/nginx.conf.template @@ -1,3 +1,17 @@ +upstream nats_fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + +upstream fastapi_backend { + # Enable sticky sessions with IP hash + ip_hash; + + +} + upstream mesop_backend { # Enable sticky sessions with IP hash ip_hash; @@ -18,9 +32,85 @@ map $fly_machine_id $sticky_action { default "replay"; # Cookie exists but doesn't match - need to replay } -# Main server block +# Nats fastapi server block +server { + listen $NATS_FASTAPI_PORT; + server_name localhost; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; + add_header X-XSS-Protection "1; mode=block"; + + location / { + # Handle cookie setting + if ($sticky_action = "set_cookie") { + add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/"; + } + + # Handle replay + if ($sticky_action = "replay") { + add_header Fly-Replay "instance=$fly_machine_id"; + return 307; + } + + proxy_pass http://nats_fastapi_backend; + 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_redirect off; + proxy_buffering off; + + # WSGI support + proxy_set_header X-Forwarded-Host $server_name; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +# Fastapi server block +server { + listen $FASTAPI_PORT; + server_name localhost; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-Content-Type-Options "nosniff"; + add_header X-XSS-Protection "1; mode=block"; + + location / { + # Handle cookie setting + if ($sticky_action = "set_cookie") { + add_header Set-Cookie "fly-machine-id=$FLY_MACHINE_ID; Max-Age=518400; Path=/"; + } + + # Handle replay + if ($sticky_action = "replay") { + add_header Fly-Replay "instance=$fly_machine_id"; + return 307; + } + + proxy_pass http://fastapi_backend; + 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_redirect off; + proxy_buffering off; + + # WSGI support + proxy_set_header X-Forwarded-Host $server_name; + + # WebSocket support + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +# Mesop server block server { - listen $SERVICE_PORT; + listen $MESOP_PORT; server_name localhost; # Security headers diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh index 1db12dbb..0ba28573 100755 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/docker/content/run_fastagency.sh @@ -1,15 +1,9 @@ #!/bin/bash # Accept env variable for PORT - -NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} - - -FASTAPI_PORT=${FASTAPI_PORT:-8008} - - +export NATS_FASTAPI_PORT=${NATS_FASTAPI_PORT:-8000} +export FASTAPI_PORT=${FASTAPI_PORT:-8008} export MESOP_PORT=${MESOP_PORT:-8888} -export SERVICE_PORT=$MESOP_PORT # Default number of workers if not set WORKERS=${WORKERS:-1} @@ -22,10 +16,23 @@ echo "Fly machine ID: $FLY_MACHINE_ID" # Generate nginx config for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) + sed -i "19i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + sed -i "12i\ server 127.0.0.1:$PORT;" nginx.conf.template +done + +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) sed -i "5i\ server 127.0.0.1:$PORT;" nginx.conf.template done -envsubst '${SERVICE_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf + +envsubst '${NATS_FASTAPI_PORT},${FASTAPI_PORT},${MESOP_PORT},${FLY_MACHINE_ID}' < nginx.conf.template >/etc/nginx/conf.d/default.conf echo "Nginx config:" cat /etc/nginx/conf.d/default.conf @@ -33,18 +40,30 @@ cat /etc/nginx/conf.d/default.conf nginx -g "daemon off;" & # Run nats uvicorn server -uvicorn my_fastagency_app.deployment.main_1_nats:app --host 0.0.0.0 --port $NATS_FASTAPI_PORT > /dev/stdout 2>&1 & +# Start multiple single-worker uvicorn instances on consecutive ports +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((NATS_FASTAPI_PORT + i)) + echo "Starting nats fastapi uvicorn on port $PORT" + uvicorn my_fastagency_app.deployment.main_1_nats:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 & +done # Run uvicorn server -uvicorn my_fastagency_app.deployment.main_2_fastapi:app --host 0.0.0.0 --port $FASTAPI_PORT > /dev/stdout 2>&1 & +# Start multiple single-worker uvicorn instances on consecutive ports +for ((i=1; i<$WORKERS+1; i++)) +do + PORT=$((FASTAPI_PORT + i)) + echo "Starting fastapi uvicorn on port $PORT" + uvicorn my_fastagency_app.deployment.main_2_fastapi:app --workers=1 --host 0.0.0.0 --port $PORT > /dev/stdout 2>&1 & +done # Run gunicorn server # Start multiple single-worker gunicorn instances on consecutive ports for ((i=1; i<$WORKERS+1; i++)) do - PORT=$((SERVICE_PORT + i)) + PORT=$((MESOP_PORT + i)) echo "Starting gunicorn on port $PORT" gunicorn --workers=1 my_fastagency_app.deployment.main_3_mesop:app --bind 0.0.0.0:$PORT > /dev/stdout 2>&1 & done diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/fly.toml b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/fly.toml index 742cfd80..ef085a35 100644 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/fly.toml +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/fly.toml @@ -21,3 +21,43 @@ primary_region = 'ams' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[services]] + http_checks = [] + internal_port = 8000 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8000 +[[services]] + http_checks = [] + internal_port = 8008 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8008 +[[services]] + http_checks = [] + internal_port = 8888 + processes = ["app"] + protocol = "tcp" + script_checks = [] + + [services.concurrency] + type = "connections" + + [[services.ports]] + handlers = ["tls", "http"] + port = 8888 diff --git a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh index a49aac06..bfd9711a 100755 --- a/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh +++ b/docs/docs_src/getting_started/no_auth/nats_n_fastapi/my_fastagency_app/scripts/register_to_fly_io.sh @@ -17,7 +17,7 @@ else echo -e "\033[0;32mAlready logged into fly.io\033[0m" fi -export FLY_APP_NAME=my-fastagency-app +export FLY_APP_NAME=$(grep "^app = " fly.toml | awk -F"'" '{print $2}') echo -e "\033[0;32mRegistering app name in fly.io\033[0m" if flyctl apps create $FLY_APP_NAME; then @@ -27,6 +27,6 @@ if flyctl apps create $FLY_APP_NAME; then cat registered_app_domain.txt else echo -e "\033[1;31mError: App name is not available.\033[0m" - echo -e "\033[1;31mPlease change the app name in fly.toml and scripts/register_to_fly_io.sh and run this script again.\033[0m" + echo -e "\033[1;31mPlease change the app name in fly.toml and run this script again.\033[0m" exit 1 fi