Skip to content

Commit 78faab8

Browse files
committed
feat: add catchall options route
1 parent be08d18 commit 78faab8

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# frozen_string_literal: true
22

33
Rails.application.routes.draw do
4+
# Handle CORS preflight requests (OPTIONS) for all routes
5+
match '*path', to: proc { [204, {}, ['']] }, via: :options
6+
47
# Mount Rswag API documentation
58
mount Rswag::Ui::Engine => '/api-docs'
69
mount Rswag::Api::Engine => '/api-docs'

nginx/app.conf

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ server {
4343
access_log /var/log/nginx/app.access.log detailed;
4444
error_log /var/log/nginx/app.error.log warn;
4545

46-
# CORS Headers
46+
# CORS Headers (applied to all responses)
4747
add_header Access-Control-Allow-Origin $cors_origin always;
4848
add_header Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD" always;
4949
add_header Access-Control-Allow-Headers "Authorization, Content-Type, Accept, Origin, X-Requested-With, X-Request-Id" always;
5050
add_header Access-Control-Allow-Credentials "true" always;
5151
add_header Access-Control-Max-Age 86400 always;
5252

53-
# Handle preflight OPTIONS requests
54-
if ($request_method = OPTIONS) {
53+
# Handle preflight OPTIONS requests for all paths
54+
location @cors_preflight {
5555
add_header Access-Control-Allow-Origin $cors_origin always;
5656
add_header Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD" always;
5757
add_header Access-Control-Allow-Headers "Authorization, Content-Type, Accept, Origin, X-Requested-With, X-Request-Id" always;
@@ -155,6 +155,11 @@ server {
155155

156156
# Authentication endpoints - moderate rate limiting
157157
location ~ ^/api/(auth|login|logout|signup) {
158+
# Handle OPTIONS preflight
159+
if ($request_method = OPTIONS) {
160+
return 204;
161+
}
162+
158163
limit_req zone=general_api burst=30 nodelay;
159164

160165
proxy_pass http://puma_rails_app;
@@ -174,6 +179,11 @@ server {
174179

175180
# General API endpoints
176181
location /api/ {
182+
# Handle OPTIONS preflight
183+
if ($request_method = OPTIONS) {
184+
return 204;
185+
}
186+
177187
limit_req zone=general_api burst=100 nodelay;
178188

179189
proxy_pass http://puma_rails_app;

0 commit comments

Comments
 (0)