knadh / niltalk

Instant, disposable, single-binary web based live chat server. Go + VueJS.
GNU Affero General Public License v3.0
948 stars 118 forks source link

Deploying Niltalk as a Microservice in a Subdirectory #32

Closed ohmykreee closed 1 year ago

ohmykreee commented 1 year ago

I am attempting to deploy the Niltalk application on a subdirectory as a micro service, but I am encountering several issues.

Specifically, I have set the root_url in the config.toml file to https://example.site/chat, and I have set up a reverse proxy in Nginx to point to the application:

    location /chat/ {

      proxy_set_header        Host $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;
      proxy_set_header        Connection $http_connection;
      # proxy_set_header        X-NginX-Proxy true;

      proxy_pass          http://localhost:9000/;
      proxy_read_timeout  120;
    }

When I access https://example.site/chat, my browser is reporting multiple errors related to missing resources:

vue.min.js:1  Failed to load resource: the server responded with a status of 404 (https://example.site/vue.min.js)
client.js:1 Failed to load resource: the server responded with a status of 404 (https://example.site/client.js)
app.js:1 Failed to load resource: the server responded with a status of 404 (https://example.site/app.js)
logo.png:1  Failed to load resource: the server responded with a status of 404 (https://example.site/logo.png)
chat.png:1 Failed to load resource: the server responded with a status of 404 (https://example.site/chat.png)
style.css:1 Failed to load resource: the server responded with a status of 404 (https://example.site/style.css)

I tried using use custom static files and modify some files like template/base.html to reference them, but this approach only made things worse:

<script src="./static/vue.min.js"></script>
<script src="./static/client.js"></script>
<script src="./static/app.js"></script>

Is there a way to host Niltalk in a subdirectory as a microservice?

knadh commented 1 year ago

In your custom static templates, revert everything to the default value, and try adding this tag right after the <title>...</title> tag.

<base href="{{ .Config.RootURL }}">

ohmykreee commented 1 year ago

Thank you for your suggestion.

I have reverted to the default value and added the <base> tag as you recommended. Here's a partial snippet of my template/base.html file to show the changes I made:

{{ define "header" }}
<!DOCTYPE html>
<html lang="en">
<head>
        <title>{{ if .Data.Title }} {{ .Data.Title }} - Niltalk {{ else }}Niltalk &mdash; Instant disposable chat rooms{{ end }}</title>
        <base href="{{ .Config.RootURL }}">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        ...

However, I'm still encountering the same error when using custom static files to launch the application. Here are some screenshots of the errors I received in my browser: image image image

Please let me know if there's anything else I could try. Thank you for your assistance.

knadh commented 1 year ago

Ah, my bad. This would require the handlers that are registered in Go program to also be aware of the sub-path, which isn't possible out of the box.

You could look at Nginx rewrite rules to strip the subpaths before proxying the requests to the backend.