Skip to content

Conversation

appflowy
Copy link
Contributor

@appflowy appflowy commented Aug 27, 2025

Summary by Sourcery

Enable CORS for WebSocket connections in Nginx by handling preflight OPTIONS requests and adding requisite headers, and propagate client forwarding headers to the backend.

Enhancements:

  • Handle preflight OPTIONS requests on the /ws endpoint with appropriate CORS headers and a 204 response
  • Add Access-Control-Allow-* headers to all responses on the /ws location

Deployment:

  • Set X-Real-IP, X-Forwarded-For, and X-Forwarded-Proto headers when proxying WebSocket traffic

Copy link

sourcery-ai bot commented Aug 27, 2025

Reviewer's Guide

Extend the WebSocket proxy configuration to support CORS preflight requests, apply CORS headers to all WebSocket responses, and enrich proxy headers with real client details.

Sequence diagram for WebSocket CORS preflight handling

sequenceDiagram
    actor Client
    participant Nginx
    participant Backend

    Client->>Nginx: OPTIONS /ws (preflight)
    Nginx-->>Client: 204 No Content + CORS headers
    Note over Nginx,Backend: No request sent to Backend
Loading

File-Level Changes

Change Details Files
Handle WebSocket CORS preflight requests
  • Add an if block for OPTIONS method
  • Set Access-Control-Allow-* headers within the block
  • Return HTTP 204 with no body for preflight
nginx/nginx.conf
Add CORS headers to all WebSocket responses
  • Inject Access-Control-Allow-Origin, -Credentials, -Headers, and -Methods headers outside the preflight block
nginx/nginx.conf
Enrich proxy forwarding headers with client info
  • Add proxy_set_header X-Real-IP to forward remote address
  • Add proxy_set_header X-Forwarded-For to record proxy chain
  • Add proxy_set_header X-Forwarded-Proto to indicate original protocol
nginx/nginx.conf

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `nginx/nginx.conf:81` </location>
<code_context>
         # WebSocket
         location /ws {
+            # Handle preflight OPTIONS requests
+            if ($request_method = 'OPTIONS') {
+                add_header 'Access-Control-Allow-Origin' $http_origin always;
+                add_header 'Access-Control-Allow-Credentials' 'true' always;
+                add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' always;
+                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
+                add_header 'Access-Control-Max-Age' 1728000 always;
+                add_header 'Content-Type' 'text/plain charset=UTF-8' always;
+                add_header 'Content-Length' 0 always;
+                return 204;
+            }
+
</code_context>

<issue_to_address>
Consider using 'add_header' with 'always' only outside conditional blocks.

'add_header ... always;' is not effective inside 'if' blocks in Nginx. For reliable CORS headers on OPTIONS requests, use a dedicated location block or move header configuration outside the 'if'.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +81 to +89
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $http_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Content-Type' 'text/plain charset=UTF-8' always;
add_header 'Content-Length' 0 always;
return 204;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Consider using 'add_header' with 'always' only outside conditional blocks.

'add_header ... always;' is not effective inside 'if' blocks in Nginx. For reliable CORS headers on OPTIONS requests, use a dedicated location block or move header configuration outside the 'if'.

@appflowy appflowy merged commit 7a076bb into main Aug 27, 2025
5 checks passed
@appflowy appflowy deleted the fix_web_ws branch August 27, 2025 16:17
@appflowy appflowy changed the title chore: fix ws cors Fix: web websocket connect issue Aug 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant