background
When debugging the local code, we can use the hosts file provided by the system to set a virtual domain name, and use Nginx to resolve the domain name to the specified IP address and port.
Modify the hosts file to access the domain name
Hosts is a system file without an extension that can be opened using tools such as Notepad. Its function is to some commonly used url domain name and its corresponding IP address to establish a “database” association.
When a user enters a web address in the browser, the system automatically searches for the corresponding IP address in the hosts file. Once the address is found, the system opens the corresponding web page. If the address is not found, the system submits the address to the DNS server for IP address resolution.
Knowing the principle, we opened the hosts file, added 127.0.0.1 sora.com, and completed the mapping between the IP address (127.0.0.1) and the domain name (sora.com).
In addition, maybe every time you want baidu “how to open the hosts file” “hosts file in which directory”, here provides a command line tool to manage the hosts file method.
First install hosts.sh globally
sudo npm install --global hosts.sh
Copy the code
Then the mapping between IP addresses and urls is established
hosts add IP URL
Copy the code
This will be accomplished ✨ program can refer to www.npmjs.com/package/hos for more usage…
Once the service is started locally, it can be accessed through the sora.com: port number. In this case, let’s assume sora.com:9080
Resolve port mapping issues through the Nginx agent
Note that the above access requires a port number. If you want to remove the port number, you need to use Nginx to move all requests to the default port 80.
A simple nginx configuration:
//nginx.conf server { listen 80; server_name sora.com; location /happy-sora/ { proxy_pass http://localhost:9080; proxy_set_header Cookie $http_cookie; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code
From here we can go to sora.com/happy-sora and have fun debugging! ✨
Refer to the blog
Most of the content of this article is carried from the use of Hosts and Nginx to map the custom domain name to the local, and add some of their own additional learning things, if there are mistakes, please also point out ~