How to match the program port and gateway port when running the website building program in the container
When running a website building program in a container, the program port and gateway port do not match. In most cases, it is not that a layer was written incorrectly, but that the three layers were treated as one. The mirror of AnQiCMS is anqicms/anqicms, and it listens to port 8001 by default. The deployment method provides Docker images and one-click deployment with Baota panel and aaPanel. The port relationships under these two paths are the same: program listening port, container mapping port, and gateway external port, each layer has its meaning.
What do the three layer ports refer to?
The first layer is inside the container, which is the address bound by the program itself, also known as the listening port in the configuration. This layer only cares about whether the process can start, and is unrelated to what port is used externally.
The second layer is the runtime mapping of the container, connecting a port on the host to the listening port inside the container. Pay attention to both sides when writing the mapping, confusing the 'host side' and 'container side' order is the most common mistake.
The third layer is the gateway facing outward, which is the port that visitors actually access, usually the 80 and 443 for web services, and is intercepted by the reverse proxy and then transferred to the host port.
| Layer | Configuration location | Typical values | The appearance when written incorrectly |
|---|---|---|---|
| Program listening | Application run configuration | 8001 | Container is up but cannot connect, health check failed |
| Container mapping | Run parameters or orchestration file | Host port points to container 8001 | Works with a different port, or access returns a connection refused |
| Gateway forwarding | Proxy site configuration | Forward 80, 443 to host port | Direct connection is fine, domain name access is not |
| External address | Site address and redirection table | Domain name and protocol | Redirected to an intranet address with a port after jumping |
Typical manifestation of wrong mapping
Three phenomena can locate to different layers. Normal direct connection with IP and port, abnormal domain name access, the problem is in gateway forwarding; direct connection is also abnormal, first check container mapping; if the mapping is correct but the page cannot be opened, go back to the program listening port and container log.
There is a type that is more concealed: the page can be opened, but the style and interface request returns 404. This is usually because the path forwarding or pseudo-static rules are not connected - the proxy only forwards the home page path, and the static resources and the rewritten addresses are not processed according to the same set of rules. When checking, you should look at the actual forwarding path of the proxy, not the writing in the browser address bar.
The request header that the reverse proxy needs to supplement
After proxying, the program sees the request coming from the forwarder, not the real visitor. To make the application correctly generate absolute addresses, judge the protocol and record the source, the protocol, hostname, and visitor address should be sent together during forwarding, and the application should be configured as a trusted proxy on the application side to avoid directly trusting the potentially forged header values.
The header is not transmitted to the application side, which will cause two typical consequences: first, the https access is judged to be plaintext, and a series of addresses fall back to the wrong protocol, resulting in a mixed content alert; second, the redirection follows the wrong Host, and the user is redirected to an internal address with a port. The 301 redirection table also needs to be written according to the same set of external addresses, otherwise the new and old addresses will bounce back and forth between the two semantic sets.
Four items to check after deployment
First, check the external access: domain, certificate, default page, and one internal page to confirm that the status code and content meet expectations. Then, check the backend entry and login redirect to confirm that the redirected address is still an external address. Third, check the data storage location: whether data is lost during container reconstruction, and whether the static files and attachment directory are mounted on a persistent volume. Fourth, check the backup: AnQiCMS supports data and static file backup and recovery, and the container environment backup should include database files, upload directories, and runtime configurations together, as only leaving the image is not enough because the image does not contain your later changes.
Common questions
Can you directly change the program's listening port to 80?Changing the port within the container merely shifts the mistake to the next layer, while the external side is still terminated by the gateway at 80; the program port remains default, and mapping and forwarding align with it more clearly.
How should ports be divided when multiple sites share a single container?On the program side, it is still a listening port, and multiple sites are distinguished by domain names and site configurations, forwarded by the gateway according to the hostname, and no need to open a port for each site.
What parts need to be changed when the host port changes?Changing the mapping and gateway forwarding at two places is sufficient, the listening port in the application configuration remains unchanged; then check if there are any remnants of the old port notation in the external address and site map.