Skip to content

2. Edit Host Entries

Luna edited this page Apr 10, 2023 · 7 revisions

All the configuration for the domains are in the src/hosts.ts file.
If you are not sure how to not break the syntax, I recommend you using code.visualstudio.com on your computer.
If you know how to do it, use nano /root/proxy/src/hosts.ts to start editing the file.

Modifying Records

This is an example object for proxing a webserver:

{
    "proxy.local": {
        target: 3000,
        type: "WEB"
    }
}

key ("proxy.local")

This value can only be a String, this key is required.
This will be the subdomain (subdomain.proxy.local) or the domain (proxy.local) that will be managed.

target

This value can only be a String, this key is required.

  • (using "WEB" or "WS"): This is the port on which your webserver runs. Please note that this musst run on the same server as this proxy script.
  • (using "REDIRECT"): This is the domain/IP the proxy script will redirect to. Note that this can be any domain, it must include the protocal (i.e. "https://lunish.nl/luna")

type

This value can only be WEB, WS or REDIRECT, this key is required.
There are 3 essential types:

  • "WEB": You will use this if you want that for example the content of the page 123.456.789:4000 should be displayed on api.waya.one.
  • "WS": You will use this if you have a (server) websocket and you want i.e. to forward it from 123.456.789:4000 to api.waya.one.
  • "REDIRECT": YOu will use this if you want to redirect the user to another page, this requires setting target to a string.

Additional Keys

If you want to do more complex stuff with the proxy, you can do that too.
Here is an example object with all possible configurations:

"proxy.local": {
    target: 3000,
    type: "WEB",
    arc: true,
    ip: '127.0.9.1',
    overwrites: [
        {
            path: ['/alphabet', '/abc', 'xyz'],
            type: "REDIRECT",
            target: 'https://abc.xyz',
            ip: '127.0.9.1',
            ignoreIfTrue: (req) => req.url.includes('.css') || req.url.includes('.js')
        },
        {
            path: '/search/*',
            type: "REDIRECT",
            target: 'https://google.com/search?q={after_path}'
        }
    ]
}

arc

This value can only be true or false, this key is optional.
If your site is using arc.io you can just set this value to true to enable support for it.
Note: All requests to /arc-sw.js will be catched by the proxy and will NOT reach your webserver.

ip

This value can only be a String, this key is optional.
By default, this script will proxy using the local 127.0.0.1 IP. If you use docker or generally want to proxy domains for different servers, you can simply change this value to any other IP address.

overwrites

This value can only be a Overwrites Array, this key is optional.
Overwrites are made to redirect or proxy only specific parts (routes) of the domain and not the whole domain at once.
You can add as many as you need and also mix them. Pay attention that you do not overwrite the same path twice, else it lower overwrite will be ignored.

[overwrite].path

This value can only be a String or a String Array, this key is required inside of overwrites.
This will represent the path(s) that will be overwritten. It can be either one (a String) or multiple (an Array) like in the example object above.
Note: All requests to this/these path(s) will be catched by the proxy and will NOT reach your webserver.

Example of both variants:

  • Singel: path: '/abc'
  • Multiple: path: ['/alphabet', '/abc', 'xyz'] (doesn't support wildcards)

Wildcards

/* This allows you to overwrite all requests with any path. ([overwrite].path must be string)

https://example.com/test/abc will automatically lead to [overwrite].target/test/abc
https://example.com/ will not be overwritten by this


/search/* Allows you to only overwrite paths behind /search/ (excluding /search itself)

https://example.com/search/abc will automatically lead to [overwrite].target/searcg/abc
https://example.com/search/ will not be overwritten by this

[overwrite].target

This value can only be a String, this key is required inside of overwrites.
For more reference please go to #target.

Placeholders

[overwrite].type

This value can only be WEB or REDIRECT, this key is required inside of overwrites.
For more reference please go to #type.
Note: WS (websockets) aren't supported as overwrite.

[overwrite].ip

This value can only be a String, this key is optional inside of overwrites.
For more reference please go to #ip.

[overwrite].ignoreIfTrue

This value can only be a Function and must return a Boolean, this key is optional inside of overwrites.
This function will be always executed when the overwrite takes affect. If true is returned, the overwrite won't be executed, if falsey-value is returned or the function doesn't exist on your host entry, the overwrite will be executed as normal.

Example: ignoreIfTrue: (req) => req.url.includes('.css') || req.url.includes('.js')
If the url either inculdes .css or .js, the overwrite will be ignored.