使用NGINX找不到文件时如何重写 [英] How to rewrite if file not found using NGINX

查看:304
本文介绍了使用NGINX找不到文件时如何重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu服务器上使用NGINX.我有这个虚拟主机:

I'm using NGINX On Ubuntu server. I have this vhost:

server {
        listen        80;
        server_name   *.example.com;
        root          /home/nginx/vhosts/example.com/web;

        location / {
                index     index.php;
        }

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9001;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        /etc/nginx/fastcgi_params;
        }
}

我必须添加一条规则...

I have to add a rule...

如果找不到文件/目录,请使用index.php

如何更改服务器{}指令?

How could I change my server {} directive?

谢谢!

推荐答案

您可以使用try_files指令:

You can use the try_files directive:

try_files $uri $uri/ /index.php

这将尝试首先查找文件和目录,如果不起作用,将使用index.php.

This will try to find files and directories first, and if that doesn't work, it will use index.php.

另请参见nginx Wiki上的前端控制器部分.

See also the front controller section on the nginx wiki.

这篇关于使用NGINX找不到文件时如何重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆