Skip to content

Instantly share code, notes, and snippets.

@nisovin
Last active September 8, 2024 04:27
Show Gist options
  • Save nisovin/cf9dd74678641fb70902866c79692b17 to your computer and use it in GitHub Desktop.
Save nisovin/cf9dd74678641fb70902866c79692b17 to your computer and use it in GitHub Desktop.
Godot Web Server Configs

Godot Web Server Configs

For Godot 4 and the threads export in Godot 3, you need to set special headers on your web server in order to enable SharedArrayBuffer. There are some examples here for various web servers.

Python

The script below can be used to start a web server that sets the required headers. The easiest way to use this is to place this python script in your export folder and double click it to start the server. Then you can view your game at http://localhost:8000.

If you're curious about the Python web server that Godot itself uses for its "Run In Browser" feature, you can find that here:

https://github.com/godotengine/godot/blob/master/platform/web/serve.py

Apache

You can set headers in Apache in an .htaccess file placed in your project directory, assuming you have AllowOverride enabled. You can also place this configuration in your Apache config files. This could be placed in a <VirtualHost> directive or a <Directory> directive, or probably others. You will need to have mod_headers enabled.

Header set Cross-Origin-Opener-Policy "same-origin"
Header set Cross-Origin-Embedder-Policy "require-corp"

Nginx

You can add headers in your nginx config file, which will look something like this:

server {
  # ... other stuff here ...
  location / {
    # ... other stuff here ...
    add_header 'Cross-Origin-Opener-Policy' 'same-origin';
    add_header 'Cross-Origin-Embedder-Policy' 'require-corp';
  }
}

Itch.io

When editing your project, under "Embed options", check the box labeled "SharedArrayBuffer support".

Newgrounds

When editing your project, check the box labeled "Uses SharedArrayBuffer/Cross Origin Isolation".

#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys
class CORSRequestHandler (SimpleHTTPRequestHandler):
def end_headers (self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == '__main__':
test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
@scadl
Copy link

scadl commented Jul 9, 2024

Really useful script! Thank you for sharing it!
So, because some of my client still uses IIS, here a config for it, using requred header and mime type.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".pck" mimeType="application/octet-stream" />
            <clientCache cacheControlMode="NoControl" />
        </staticContent>
        <httpProtocol>
            <customHeaders>
                <add name="Cross-Origin-Opener-Policy" value="same-origin" />
                <add name="Cross-Origin-Embedder-Policy" value="require-corp" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment