Files
romm/docker/nginx/default.conf
Michael Manganiello 45aeaf3265 fix: Backend URL redirection logic
Fix FastAPI and nginx configuration, to make the application correctly
redirect URLs. This is specially useful when URLs ended with forward
slash are redirected to their stripped version.

Included changes:
* Stop removing the `/api` prefix in nginx rewrite rules, so FastAPI
  knows what's the original URL path being requested.
* Use `$http_host` in nginx, so FastAPI receives both the original host
  and port, to build the redirect URL (as `$host` does not include the
  port, if present).
* Make all FastAPI included routers know their prefix, to correctly
  route incoming requests.

This fix was found based on a report that redirects from URLs ended with
forward slash were not working [1].

[1] https://github.com/rommapp/romm/issues/1051#issuecomment-2269049762
2024-08-05 11:15:52 -03:00

92 lines
2.8 KiB
Plaintext

worker_processes auto;
pid /tmp/nginx.pid;
events {
worker_connections 768;
multi_accept on;
}
http {
client_body_temp_path /tmp/client_body 1 2;
fastcgi_temp_path /tmp/fastcgi 1 2;
proxy_temp_path /tmp/proxy;
uwsgi_temp_path /tmp/uwsgi;
scgi_temp_path /tmp/scgi;
sendfile on;
client_max_body_size 0;
tcp_nopush on;
# types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
map $time_iso8601 $date {
~([^+]+)T $1;
}
map $time_iso8601 $time {
~T([0-9:]+)\+ $1;
}
#INFO: [nginx][2023-11-14 09:20:29] 127.0.0.1 - -"GET / HTTP/1.1" 500 177 "-" "Mozilla/5.0 (X11; Linux x86_64)"rt=0.000 uct="-" uht="-" urt="-"
log_format romm_log 'INFO: [nginx][$date $time] $remote_addr - $remote_user '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /dev/stdout romm_log;
error_log /dev/stderr;
gzip on;
# include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;
upstream wsgi_server {
server unix:/tmp/gunicorn.sock;
}
server {
root /var/www/html;
listen 8080;
server_name localhost;
proxy_set_header Host $http_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 / {
try_files $uri $uri/ /index.html;
proxy_redirect off;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
}
# Static files
location /assets {
try_files $uri $uri/ =404;
}
# OpenAPI for swagger and redoc
location /openapi.json {
proxy_pass http://wsgi_server;
}
# Backend api calls
location /api {
proxy_pass http://wsgi_server;
}
location /ws {
proxy_pass http://wsgi_server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}