nginx try_files 不起作用?

标签 nginx react-router

我的目录:

/
|- static/
  |- index.html  // my pc page
  |- CKFM/
    |-index.html // my mobile web app

更新: 我的 nginx.conf(完整版):

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    ignore_invalid_headers off;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ^~ /cooka {
            proxy_pass http://localhost:8090 ;
        }
        location ^~ /cooka-search {
            proxy_pass http://localhost:8089 ;
        }
        location ^~ /m/cooka {
            proxy_pass http://localhost:8090 ;
        }

        location ^~ /CKFM {
            try_files $uri $uri/ /CKFM/index.html;
        }

        location / {
            root   static;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

我在 /CKFM/ 文件夹中有一个基于 react-router 构建的网络应用

当我转到 localhost/CKFM/ 时,它起作用了,因为有一个 index.html

但是当我转到 localhost/CKFM/signin/

它显示 404 未找到

我想实现的是:

当转到localhost/CKFM/sigin

它也将服务于 /CKFM/index.html


更多信息:

我的应用是一个 react-router 应用

我正在按照文档配置 nginx。

https://github.com/rackt/react-router/blob/latest/docs/guides/basics/Histories.md#createbrowserhistory

我在一个全新下载的 nginx 中测试了 try_files 配置,它有效。

有效的配置文件:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /CKFM {
            try_files $uri /CKFM/index.html;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

所以我猜我的一些配置破坏了 try_files 指令...

最佳答案

改变配置

location ^~ /CKFM {
    root <your root folder>;
    try_files $uri $uri/ /CKFM/index.html;
}

你还错过了这一行末尾的 ; :)

关于nginx try_files 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487232/

相关文章:

reactjs - react-router:beforeunload 事件不适用于路由更改

javascript - REST 服务/网站安全 - 停止页面 'flashing' 向上

react-router - 如何在 React Router v4 中访问 <Route/> 之外的历史对象

reactjs - 如何处理 react + react 路由器+通量中的查询参数

javascript - React Router 4相对路径

reactjs - 我无法使用 Glamorous 设计 React-router-dom 链接

docker - 如何通过 Nginx 将外部请求的端口 80 和 443 分别转发到端口 81 和 444?

ruby-on-rails - 跨前端服务器共享缓存文件的可扩展方式

nginx - 在nginx中worker_connections、keepalive_timeout和$connection是什么关系

node.js - Angular 前端 Docker 容器不从 Node Express 后端 Docker 容器发送/接收数据