@@ -90,34 +90,105 @@ done
9090# We need to sure '/var/www/html' exists for 'wp-cli'
9191wait_for_service " wordpress" 9001
9292
93- # To enable NPP - Nginx Cache Preload action:
94- # #####################################################################
95- # For Development Environment:
96- # - Cause HTTP_HOST is localhost,
97- # - Map the WordPress container's 'localhost' to Nginx's IP.
98- # - Note: This is a tricky hack and only used for the development environment!
93+ # Resolve host
94+ resolve_host () {
95+ local host=" $1 "
96+ local ipv4=" "
97+ local ip_fallback=" "
98+ local result=()
99+
100+ # Try to get IPv4 address
101+ if ping -4 -c 1 " ${host} " & > /dev/null; then
102+ ipv4=$( ping -4 -c 1 " $host " | awk -F'[ ()]' ' {print $2 }' | head -n 1)
103+ fi
104+
105+ # Fallback to find IP
106+ if getent hosts "${host}" &>/dev/null; then
107+ ip_fallback=$(getent hosts "${host}" | awk ' { print $1 }' )
108+ fi
109+
110+ # No IP found
111+ if [[ -z "${ipv4}" && -z "${ip_fallback}" ]]; then
112+ return 1
113+ # If both IPv4 and fallback IP are found
114+ elif [[ -n "${ipv4}" && -n "${ip_fallback}" ]]; then
115+ if [[ "${ipv4}" == "${ip_fallback}" ]]; then
116+ # If both IPs are equal, return only one
117+ result+=("${ipv4}")
118+ else
119+ # If both IPs are different, return both
120+ result+=("${ipv4}")
121+ result+=("${ip_fallback}")
122+ fi
123+ # If only one IP is found
124+ elif [[ -n "${ipv4}" ]]; then
125+ result+=("${ipv4}")
126+ else
127+ result+=("${ip_fallback}")
128+ fi
129+
130+ printf "%s\n" "${result[@]}"
131+ }
132+
133+ # To enable NPP Plugin Nginx Cache Preload action:
134+ # ############################################################################################################
135+ # The NPP WordPress plugin uses ' wget' with ' WP_SITEURL' from inside the WordPress container to Preload cache.
136+ # This means that if ' WP_SITEURL' is set to "localhost", wget will attempt to fetch URLs from
137+ # the container’s own loopback interface rather than reaching the Nginx server that handles
138+ # cache preload requests.
99139#
100- # For Production Environments: (Nginx sits on host or container)
101- # - I assume you use a publicly resolvable FQDN for WordPress (WP_SITEURL & WP_HOME);
102- # - Ensure outgoing traffic is allowed from the container.
103- # - Verify that /etc/resolv.conf in the container is correctly configured.
104- # - Verify that the container has internet access.
105- # + That's all for Cache Preload works like a charm.
106- # ######################################################################
140+ # To handle that;
141+ #
142+ # Development Environments:
143+ # - During ' wp core install' , the ' --url' parameter is hardcoded as ' https://localhost' ,
144+ # so WP_SITEURL ends up being ' https://localhost' within the container.
145+ # - In this scenario, Nginx Cache Preload requests will try to access ' https://localhost' , which
146+ # incorrectly refers to the wordpress container itself.
147+ # - To work around this, we update the wordpress container’s ' /etc/hosts' file to remap ' localhost' to either
148+ # ' host.docker.internal' or the actual ' Nginx container IP' . This forces to retrieve resources
149+ # from the correct endpoint, enabling the Nginx Cache Preload action during development.
150+ # - Keep in mind! Below settings will not work here because of priority issue in /etc/hosts
151+ # extra_hosts:
152+ # - "localhost:Nginx_LAN_IP"
153+ #
154+ # Production Environment:
155+ # - WP_SITEURL is typically set to an FQDN (example.com) pointing to Nginx.
156+ # - If the WordPress container has WAN access, can resolve external domains, and allows outgoing traffic,
157+ # Cache Preload requests will correctly reach Nginx over the WAN route.
158+ # - If the wordpress container lacks WAN access, external DNS resolution, or outgoing traffic:
159+ # - WP_SITEURL (example.com) must resolve internally to Nginx LAN IP. (Nginx can sits on host or as a container)
160+ # - Solutions:
161+ # 1. Internal DNS resolver mapping WP_SITEURL to Nginx' s LAN IP.
162+ # 2. Manually adding WP_SITEURL to /etc/hosts inside the wordpress container.
163+ # 3. Recommended docker way, edit wordpress service in docker-compose.yml,
164+ # extra_hosts:
165+ # - "example.com:Nginx_LAN_IP"
166+ # ##############################################################################################################
107167if [[ " ${NPP_DEV_ENABLED} " -eq 1 ]]; then
108- IP=" ${NPP_NGINX_IP} "
109- LINE=" ${IP} ${NPP_HTTP_HOST} "
168+ # Create array
169+ mapfile -t ip_array < <( resolve_host host.docker.internal)
170+
171+ # Create temporary file
172+ TEMP_HOSTS=" $( mktemp /tmp/hosts.XXXXXX) "
110173 HOSTS=" /etc/hosts"
111174
112- # Check if the Nginx static IP defined
113- if ! grep -q " ${IP} " " ${HOSTS} " ; then
114- # Map localhost to Nginx Static IP
115- echo -e " ${LINE} \n$( cat ${HOSTS} ) " > /tmp/hosts.new
116- cat /tmp/hosts.new > " ${HOSTS} "
117- rm -f /tmp/hosts.new
118- echo -e " ${COLOR_GREEN}${COLOR_BOLD} NPP-WP:${COLOR_RESET} Mapped '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET} ' to Nginx IP '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET} ' in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET} ."
175+ # Hack /etc/hosts kindly, not make container upset
176+ # Map to host.docker.internal if available
177+ if (( ${# ip_array[@]} )) ; then
178+ for IP in " ${ip_array[@]} " ; do
179+ echo " ${IP} ${NPP_HTTP_HOST} " >> " ${TEMP_HOSTS} "
180+ done
181+ cat " ${HOSTS} " >> " ${TEMP_HOSTS} "
182+ cat " ${TEMP_HOSTS} " > " ${HOSTS} "
183+ echo -e " ${COLOR_GREEN}${COLOR_BOLD} NPP-WP:${COLOR_RESET} ${COLOR_RED} Hacked!${COLOR_RESET} Mapped ${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET} to host.docker.internal ${COLOR_LIGHT_CYAN}${ip_array[@]}${COLOR_RESET} in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET} ."
119184 else
120- echo -e " ${COLOR_YELLOW}${COLOR_BOLD} NPP-WP:${COLOR_RESET} Mapping already exists: '${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET} ' -> '${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET} '."
185+ # Fallback, Map to NGINX container IP
186+ IP=" ${NPP_NGINX_IP} "
187+ LINE=" ${IP} ${NPP_HTTP_HOST} "
188+ HOSTS=" /etc/hosts"
189+ echo -e " ${LINE} \n$( cat ${HOSTS} ) " > " ${TEMP_HOSTS} "
190+ cat " ${TEMP_HOSTS} " > " ${HOSTS} "
191+ echo -e " ${COLOR_GREEN}${COLOR_BOLD} NPP-WP:${COLOR_RESET} ${COLOR_RED} Hacked!${COLOR_RESET} Mapped ${COLOR_LIGHT_CYAN}${NPP_HTTP_HOST}${COLOR_RESET} to Nginx container IP ${COLOR_LIGHT_CYAN}${IP}${COLOR_RESET} in ${COLOR_LIGHT_CYAN}${HOSTS}${COLOR_RESET} ."
121192 fi
122193fi
123194# ######################################################################
0 commit comments