@@ -64,8 +64,8 @@ for var in \
64
64
NPP_UID \
65
65
NPP_GID \
66
66
NPP_DEV_ENABLED \
67
- NPP_NGINX_IP \
68
67
NPP_HTTP_HOST \
68
+ NPP_HACK_HOST \
69
69
NPP_DEV_PLUGIN_NAME \
70
70
NPP_DEV_PLUGIN_DIR \
71
71
NPP_DEV_TMP_CLONE_DIR \
90
90
# We need to sure '/var/www/html' exists for 'wp-cli'
91
91
wait_for_service " wordpress" 9001
92
92
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
+ ipv4=" $( ping -4 -c 1 " $host " | grep -oP ' (?<=\()[^)]+' | head -n 1) "
102
+
103
+ # Fallback to find IP
104
+ ip_fallback=" $( getent hosts " ${host} " | awk ' { print $1 }' ) "
105
+
106
+ # No IP found
107
+ if [[ -z " ${ipv4} " && -z " ${ip_fallback} " ]]; then
108
+ return 1
109
+ # If both IPv4 and fallback IP are found
110
+ elif [[ -n " ${ipv4} " && -n " ${ip_fallback} " ]]; then
111
+ if [[ " ${ipv4} " == " ${ip_fallback} " ]]; then
112
+ # If both IPs are equal, return only one
113
+ result+=(" ${ipv4} " )
114
+ else
115
+ # If both IPs are different, return both
116
+ result+=(" ${ipv4} " )
117
+ result+=(" ${ip_fallback} " )
118
+ fi
119
+ # If only one IP is found
120
+ elif [[ -n " ${ipv4} " ]]; then
121
+ result+=(" ${ipv4} " )
122
+ else
123
+ result+=(" ${ip_fallback} " )
124
+ fi
125
+
126
+ printf " %s\n" " ${result[@]} "
127
+ }
128
+
129
+ # To enable NPP Plugin Nginx Cache Preload action:
130
+ # ##################################################################################################################
131
+ # The NPP WordPress plugin uses "wget" with "WP_SITEURL" from inside the WordPress container to Preload Nginx Cache.
132
+ # This means that if "WP_SITEURL" is set to "localhost", wget will attempt to fetch URLs from
133
+ # the containers own loopback interface rather than reaching the Nginx server that handles
134
+ # Cache Preload requests.
99
135
#
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
- # ######################################################################
107
- if [[ " ${NPP_DEV_ENABLED} " -eq 1 ]]; then
108
- IP=" ${NPP_NGINX_IP} "
109
- LINE=" ${IP} ${NPP_HTTP_HOST} "
136
+ # To handle that;
137
+ #
138
+ # Development Environments:
139
+ # - During "wp core install", the "--url" parameter is hardcoded as "https://localhost",
140
+ # so WP_SITEURL ends up being "https://localhost" within the container.
141
+ # - In this scenario, Nginx Cache Preload requests will try to access "https://localhost", which
142
+ # incorrectly refers to the wordpress container itself.
143
+ # - To work around this, we update the wordpress containers "/etc/hosts" file to remap "localhost" to either
144
+ # "host.docker.internal" or the actual "Nginx container IP". This forces to retrieve resources
145
+ # from the correct endpoint, enabling the Nginx Cache Preload action during development.
146
+ # - Keep in mind! Below settings will not work here because of priority issue in /etc/hosts
147
+ # extra_hosts:
148
+ # - "localhost:Nginx_LAN_IP"
149
+ #
150
+ # Production Environment:
151
+ # - WP_SITEURL is typically set to an FQDN (example.com) pointing to Nginx.
152
+ # - If the WordPress container has WAN access, can resolve external domains, and allows outgoing traffic,
153
+ # Cache Preload requests will correctly reach Nginx over the WAN route.
154
+ # - If the wordpress container lacks WAN access, external DNS resolution, or outgoing traffic:
155
+ # - WP_SITEURL (example.com) must resolve internally to Nginx LAN IP. (Nginx can sits on host or as a container)
156
+ # - Solutions:
157
+ # 1. Internal DNS resolver mapping WP_SITEURL to Nginx's LAN IP.
158
+ # 2. Manually adding WP_SITEURL to /etc/hosts inside the wordpress container.
159
+ # 3. Recommended docker way, edit wordpress service in docker-compose.yml,
160
+ # extra_hosts:
161
+ # - "example.com:Nginx_LAN_IP"
162
+ # ##################################################################################################################
163
+ if [[ " ${NPP_HACK_HOST} " -eq 1 ]]; then
164
+ # Create array
165
+ mapfile -t ip_array < <( resolve_host host.docker.internal)
166
+
167
+ # Create temporary file
168
+ TEMP_HOSTS=" $( mktemp /tmp/hosts.XXXXXX) "
110
169
HOSTS=" /etc/hosts"
111
170
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} ."
119
- 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} '."
171
+ # Hack /etc/hosts kindly, not make container upset
172
+ # Map to host.docker.internal
173
+ if (( ${# ip_array[@]} )) ; then
174
+ for IP in " ${ip_array[@]} " ; do
175
+ echo " ${IP} ${NPP_HTTP_HOST} " >> " ${TEMP_HOSTS} "
176
+ done
177
+
178
+ cat " ${HOSTS} " >> " ${TEMP_HOSTS} "
179
+ cat " ${TEMP_HOSTS} " > " ${HOSTS} "
180
+ 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} ."
121
181
fi
122
182
fi
123
- # ######################################################################
183
+ # ###################################################################################################################
124
184
125
185
# Check ownership of webroot for consistency
126
186
check_ownership () {
0 commit comments