Analysis and resolution of 404 Not Found in Nginx PHP environment in Docker containers
1. In the Docker container, the Nginx PHP environment responds to 404 not found, and opens a route mapped to the PHP file, as shown in Figure 1
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<center>
<h1>404 Not Found</h1>
</center>
<hr>
<center>nginx</center>
</body>
</html>
2. Open a route mapped to an HTML file and respond to 200, as shown in Figure 2
3. The configuration file of nginx, the content is as follows
server {
listen 80; ## listen for ipv4
server_name api.pcs.wjdev.chinamcloud.cn;
charset utf-8;
root /mcloud/www/pcs-api/api/web;
index index.php;
location / {
# 如果找不到真实存在的文件,把请求分发至 index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location / {
rewrite /interace /abc;
}
location /abc {
return 200 '{"msg":"success"}';
}
# uncomment to avoid processing of calls to non-existing static files by Yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~* /\. {
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
access_log off;
allow 127.0.0.1;
allow 10.42.0.0/16;
allow 10.244.0.0/8;
allow 192.168.0.0/8;
allow 172.17.0.0/8;
deny all;
}
}
4. It is found that there is duplicate content in the configuration file (location / { }), which causes the first configuration item to be overwritten, and decides to delete the second configuration item
location / {
rewrite /interace /abc;
}
location /abc {
return 200 '{"msg":"success"}';
}
server {
listen 80; ## listen for ipv4
server_name api.pcs.wjdev.chinamcloud.cn;
charset utf-8;
root /mcloud/www/pcs-api/api/web;
index index.php;
location / {
# 如果找不到真实存在的文件,把请求分发至 index.php
try_files $uri $uri/ /index.php$is_args$args;
}
# uncomment to avoid processing of calls to non-existing static files by Yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~* /\. {
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ ^/(status|ping)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
access_log off;
allow 127.0.0.1;
allow 10.42.0.0/16;
allow 10.244.0.0/8;
allow 192.168.0.0/8;
allow 172.17.0.0/8;
deny all;
}
5. Open a route mapped to the PHP file, the response is 200, as expected, as shown in Figure 3


