Nginx 在 404 页面(在我的例子中是索引)上仍然显示不存在的 URL

Nginx still showing the non existant URL when on the 404 page (index in my case)

我在使用 Nginx 时遇到了一个小问题,如果有人能帮助我找出它无法正常工作的原因,我将不胜感激。 我想做的是在请求的 URL 无效时让网站重定向到索引页面,但是在使用以下代码时:

server {
    listen   80;

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    server_name localhost;

    error_page 404 /index.php;
    error_page 500 502 503 504 /50x.php;


    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
    location ~ \.php$ {
            try_files $uri $uri/ =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

它的工作方式与我希望的不完全一样。此代码有效,但显示索引页时 URL 仍为 http://example.com/nonexistantpage。我想要的是将网站重定向到索引页面,使 URL http://example.com

好的,很简单试试这个

error_page 404 =301 /;

如果它不起作用则使用完整路径

error_page 404 =301 http://example.com;