Solution 1: DDNS (Home Network Requires a Public IP)
How to determine if there is a public IP? The general process is to change the optical modem to bridge mode, switch from optical modem dialing to router dialing, and check the IP obtained from the router management page. Use this IP to verify on an IP query website. Specific operations can be searched independently, and will not be elaborated here.
We all know that there are two types of IP: IPv4 and IPv6.
Network operators generally allocate IPv6, which can be enabled on the router.
Using IPv6 has a drawback: if the accessing user does not have IPv6, they will not be able to access your server.
If the public IP is fixed, there is not much to say; just configure the domain name resolution directly. In the case of a dynamic home network IP, you can use DDNS services, and I recommend ddns-go (https://github.com/jeessy2/ddns-go).
The function of DDNS is to map a dynamic public IPv4 or IPv6 address to a fixed domain name resolution service.
This DDNS needs to be installed, or you can choose a more lightweight option by using the router’s DDNS service.
Solution 2: NAT Traversal (Requires a Server with a Public IP)
I recommend the tool frp (https://github.com/fatedier/frp).
Documentation for gofrp (https://gofrp.org/zh-cn/docs/).
There is a client and a server; the server needs to be deployed on a server with a public IP, and the domain name should resolve to the public IP. Configure according to the documentation, or you can directly copy my configuration.
frps.toml Server Configuration
bindPort = 7000 # Required, binding port, the client needs to connect to this port for communicationvhostHTTPPort = 18080 # HTTP proxy listening port
[auth]token = "asdfasdfasdfs" # Required, token, must be set, otherwise anyone can connect to your frps
[webServer] # Dashboard related configuration, for viewing information
addr = "0.0.0.0"
port = 7001
user = 'user'
password = 'password'
frpc.toml Client Configuration
user = "local-server" # Name, can be customized freely
serverAddr = "xx.xx.xx.xx" # Required, public IP server
serverPort = 7000 # Required, server's bindPort
[auth]token = "asdfasdfasdfs" # Token configured on the server
[webServer] # Dashboard related configuration, for viewing information
addr = '0.0.0.0'
port = 7001
user = 'user'
password = 'password'
[[proxies]] # Proxy related settings, accessing the server's port 6022 will forward requests to your local port 22
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6022
[[proxies]] # Proxy related settings, accessing the server's port 18080 will forward requests to your local port 80
name = "http"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains=["www.domains.com"] # Bound domain name, must be configured, new domain names need to be modified here
Check if your configuration is correct
./frps verify -c frps.toml
When users access this domainwww.domains.com:18080, the request will be forwarded to the server in your home network. However, if we want to access it without adding this port, we can add a layer of Nginx as a reverse proxy on the public IP server to forward requests on ports 80/443 to 18080.