nginx作为web服务器,正常登陆不报错,刷新直接404

情景描述:发布新版本以后,登陆以后,刷新页面报错 404 。但是旧版本就不会出现这样的情况。前端开发不知道什么原因。我是用nginx作为web服务器,因此,找nginx配置问题。

发现只要使用 try_files \$uri  \$uri/ /index.html;  代替:index  index.html; 即可。

【nginx try_files的理解】

以 try_files $uri $uri/ /index.php; 为例,当用户请求 http://servers.blog.ustc.edu.cn/example 时,这里的 $uri 就是 /example。try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是 WordPress 的安装目录)的文件,就直接把这个文件的内容发送给用户。显然,目录中没有叫 example 的文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。又找不到,就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于 nginx 发起一个 HTTP 请求到 http://servers.blog.ustc.edu.cn/index.php。这个请求会被 location ~ \.php$ { ... } catch 住,也就是进入 FastCGI 的处理程序。而具体的 URI 及参数是在 REQUEST_URI 中传递给 FastCGI 和 WordPress 程序的,因此不受 URI 变化的影响。

 

nginx配置如下:

user root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.abc.cn;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            #root   /home/web;
            root   /opt/application/web;
            #index  index.html;
           try_files $uri $uri/ /index.html;
        }
    
    location ~/group1/M00 {
        root /home/fastdfs/storage/data;
        ngx_fastdfs_module;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
   }
include vhost/*host.conf;
}

 

posted @ 2017-12-19 17:05  Nice_keep-going  阅读(890)  评论(0)    收藏  举报