Skip to content

Instantly share code, notes, and snippets.

@nisovin
Last active July 9, 2024 07:45
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)
@terminatorbs
Copy link

The game in the browser was supposed to be a client, connecting to a "normal" server. The idea behind it was that, during development, i could send a link to a friend and be like "playtest this with me for a minute".

I've experimented some more and researched some more but could not get it to work - iirc i also saw someone mention that HTML5 export in Godot 4 does not support websockets properly yet, so after a while i chalked it up to that and decided to try again later.

The documentation on websockets is also still outdated, so once that's up to date i might give it another shot. I'm actually still not sure if i need to allow websockets somehow in my nginx conf or if it really just isn't working properly in godot 4 web exports yet.

The mention about proxing is just because literally EVERY result on google in my search was about proxying things, but in my case i'm running it directly on the nginx server so these results didn't help me. or at least that's how i understand it.

@nisovin
Copy link
Author

nisovin commented Apr 12, 2023

@terminatorbs I'm not sure if these headers affect connections to a websocket server, but if they do, then you may need to add headers to the websocket server. Also, you will need to make sure your websocket server is running SSL.

@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