Using Domain Names to Access Deployed Projects
12006The project deployed in the previous lesson can only be accessed via the server's IP address and port number. This method is not only difficult to remember but also requires opening the corresponding port on the operator's and server's firewall, making management very cumbersome. Therefore, users generally access network services through domain names.
A domain name is an easy-to-remember name used on the Internet to identify computers (such as servers). It serves as an address for locating and accessing websites, replacing complex numerical IP addresses. When a user enters a domain name in the browser's address bar, the DNS (Domain Name System) translates that domain name into the corresponding IP address, allowing the user's device to connect to the server hosting the website. For example, when entering www.example.com, DNS resolves it to the server's IP address, such as 192.0.2.1, thus enabling the connection and access.
In this lesson, we will learn how to directly access the deployed project on the server through a domain name. We need to purchase a domain name through a domain registrar and add DNS resolution records for the domain name.
1. Registering a Domain Name
There are many domain registrars on the Internet, such as GoDaddy, Namecheap, and Google Domains abroad, and Alibaba Cloud and Tencent Cloud in China also provide domain registration services. You need to first apply for an account on the domain registrar's website and then choose to register a domain name. You can refer to the following official documents for this step:
- GoDaddy:
- Apply for an account: https://sg.godaddy.com/zh/help/create-a-godaddy-account-16618
- Register a domain: https://sg.godaddy.com/zh/help/how-do-i-register-a-domain-with-godaddy-341
- Alibaba Cloud:
- New user guide: https://help.aliyun.com/zh/dws/user-guide/novice-guide
- Register a domain: https://help.aliyun.com/zh/dws/user-guide/quick-start-1
- Tencent Cloud:
- Register a domain: https://cloud.tencent.com/document/product/242
Note: When registering a domain name with Alibaba Cloud or Tencent Cloud, it is necessary to complete real-name authentication for the domain name. When deploying a website on domestic servers, an ICP filing must be completed; otherwise, the website cannot be accessed. The procedures for domain name real-name authentication and ICP filing can be found in the reference materials for this lesson.
2. Adding Domain Name Resolution Records
After registering the domain name and completing real-name authentication, you can add domain name resolution records. This step can refer to the official documents of various domain registrars:
- GoDaddy: https://sg.godaddy.com/zh/help/manage-dns-records-680
- Alibaba Cloud: https://help.aliyun.com/document_detail/106669.html
- Tencent Cloud: https://cloud.tencent.com/document/product/302/3446
Note: When adding domain name resolution records, you need to select the record type as A. The server's IP can be found in the server list on the operator's console.
3. Jianghu Panel - Website Configuration
Next, we need to add a site through the "Website" feature of the Jianghu panel on the server.
- In the left sidebar of the Jianghu panel, select "Website" and then "Add Site."
- Enter the domain name of the website and the root directory. Typically, the root directory of the website will be located under
/www/wwwrootand will have the same name as the website domain name. It is not recommended to modify this step. - Click "Submit," and the website will be created. You can see the newly added site in the website list.
After the website is created, we also need to configure reverse proxy settings. A reverse proxy is a server that sits between the client (usually a web browser) and the actual backend server providing the service. Its main function is to receive requests from clients and forward them to the backend server according to certain rules. Configuring a reverse proxy involves setting up these forwarding rules.
- In the website list, click on the "Settings" of the website, and in the pop-up window, click on "Configuration File" on the left, then clear the content.
- Copy the following configuration code and paste it into the configuration file, then write the registered domain name into the configuration file. This code adds a reverse proxy configuration that proxies the port 7201 used by the project to the domain name.
server {
listen 80;
server_name www.example.com; # Here, change www.example.com to your domain name
# Configure root directory to forward to port 7201
location / {
# Forward requests to the backend API server
proxy_pass http://localhost:7201;
# Some HTTP header handling for reverse proxy
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}You can refer to the explanation of the above code in this course's Lesson 8: Jianghu Panel - Website Management.
4. Accessing the Site via Domain Name
Now, you can access the jianghujs-1table-crud project in your browser using the domain name. Don't forget to close port 7201 in the cloud server's security group and the Jianghu panel.
Assignment
- Configure the domain name to add resolution to the server.
- Configure the website to resolve the domain name.
- Ensure that the deployed jiagnhujs_1table_crud project can be accessed normally via the domain name.
Reference Materials
- Domain Name Real-name Authentication and ICP Filing
- Alibaba Cloud:
- Domain Name Real-name Authentication: https://help.aliyun.com/zh/dws/user-guide/real-name-authentication-for-domains
- ICP Filing: https://help.aliyun.com/zh/icp-filing/user-guide/icp-filing-application-overview
- Tencent Cloud:
- Domain Name Real-name Authentication: https://cloud.tencent.com/document/product/242/6707
- ICP Filing: https://cloud.tencent.com/document/product/243