admin管理员组文章数量:1192815
I have nginx running wordpress. Weird thing is the site won't 404 and instead rewrites the URL to the homepage. Any URL with a .php in the string will 404 correctly but /foo will not 404. Any help would be appreciated.
example/foo.php will 404
example/foo will not 404 and instead just loads the homepage.
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
#try_files $uri $uri/ =404;
}
# pass the PHP scripts
#
location ~ \.php$ {
fastcgi_intercept_errors on;
include snippets/fastcgi-php.conf;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
error_page 404 = /404.html;
location = /404.html {
root /home/web/error-pages;
internal;
}
I've been able to narrow this down to my nginx (Ubuntu 16.04, PHP 7, MySQL) because even a fresh install of WordPress has the same behavior.
I have nginx running wordpress. Weird thing is the site won't 404 and instead rewrites the URL to the homepage. Any URL with a .php in the string will 404 correctly but /foo will not 404. Any help would be appreciated.
example.com/foo.php will 404
example.com/foo will not 404 and instead just loads the homepage.
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
#try_files $uri $uri/ =404;
}
# pass the PHP scripts
#
location ~ \.php$ {
fastcgi_intercept_errors on;
include snippets/fastcgi-php.conf;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
error_page 404 = /404.html;
location = /404.html {
root /home/web/error-pages;
internal;
}
I've been able to narrow this down to my nginx (Ubuntu 16.04, PHP 7, MySQL) because even a fresh install of WordPress has the same behavior.
Share Improve this question edited Sep 4, 2018 at 20:43 pan asked Aug 28, 2018 at 21:38 panpan 212 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0I did a test on a fresh install of WordPress 6.0.1, served using php-fpm 7.4.3 and nginx 1.18.0 on Ubuntu 20.04.4 using this nginx configuration:
server {
listen 80;
server_name wp.test;
root /var/www/wp;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
}
(I also added to 127.0.0.1 wp.test
to /etc/hosts
).
Observation: accessing a URL that is supposed to be non-existent (e.g. http://wp.test/nonexistent
) would show the contents of the home page (i.e. exactly the same content as http://wp.test/
). This is unexpected, strange, and incorrect behavior.
I do not understand the cause of the behavior, but I found that it is possible to "fix" the problem by changing the permalink settings in the WordPress admin area. The problem only seems to affect the "Plain" permalink setting. The problem disappears when using another permalink structure. For example, with the "Post name" permalink setting, accessing http://wp.test/nonexistent
would show a 404 error.
本文标签: Wordpress Nginx Won39t 404
版权声明:本文标题:Wordpress Nginx Won't 404 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738471613a2088622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$ sudo service nginx reload
– Castiblanco Commented Aug 28, 2018 at 21:42try_files $uri $uri/ /index.php?$args;
sends anything that isn't a static file to WordPress. Which is actually a desired behaviour, as it allows WordPress to see every request and implement pretty permalinks (if you have them enabled). Are you not seeing a WordPress generated "not found" page? – Richard Smith Commented Aug 29, 2018 at 8:30