I have a website running on nginx + php-fpm and I'm looking for a config file that actually shows me a 404 and some other error pages, because the current state of my config file only changes the URL and doesn't show me a 404 page. For example.
I go to mywebsite.com/some-non-existent/url
and I'm greeted with the homepage instead of the 404 page.
Also, is the try_files
syntax "safe" to use or should I use rewrites?
I've read the nginx documentation extensively and the wordpress codex config too.
Below I'l paste my Config file to see if someone can shed some light on the subject.
server {
listen 127.0.0.1:80;
server_name my.website.com;
root home/my.website.com;
access_log logs/my.website.com-access.log;
error_log logs/my.website.com-error.log notice;
charset utf-8;
index index.php;
location / {
#try_files $uri $uri/ /index.php$is_args&$args;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9054;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location ~ /\. {
deny all;
}
error_page 404 /index.php?error=404;
}
And also fastcgi_params included above
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Also, does nginx read location from top to bottom? if so, when it hits fastcgi_pass
does it read/execute the configuration below it or goes directly to php?
Edit: X-posted from WP StackExchange
You should always prefer
try_files
to rewrites.The only real issues I see with your configuration are:
For WordPress it's not necessary to pass in the
$args
; it gets them from the environment instead. My working configuration has:Second, you have a stray
/
here:This might not be a problem, but for sanity's sake I wouldn't leave it.