【问题标题】:nginx try_files does not work?nginx try_files 不起作用?
【发布时间】:2015-12-28 03:08:37
【问题描述】:

我的目录:

/
|- 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 的 Web 应用程序

当我转到 localhost/CKFM/ 时,它可以工作,因为有一个 index.html

但是当我去localhost/CKFM/signin/

它显示404 Not Found

我想要达到的是:

什么时候去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 指令...

【问题讨论】:

    标签: nginx react-router


    【解决方案1】:

    改变配置

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

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

    【讨论】:

    • 得到 500 错误!当访问localhost/CKFM/ 时,我更新了我的问题,你可以看看。我猜我的一些配置与 try_files 冲突,但我不知道是哪个。
    • 为您的服务器设置 root root html;
    • or for location location /CKFM { root html; try_files $uri /CKFM/index.html; } root是CKFM的父目录
    • 我必须在最后一个位置添加 =404 作为另一个参数:{ try_files $uri /index.html$is_args$args =404 }
    【解决方案2】:

    以下是我的配置谢谢@gutch @Max P。

    server {
      listen       80;
      server_name  localhost;
    
      location / {
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
      }
    
      #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   /usr/share/nginx/html;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-15
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2018-03-01
      • 1970-01-01
      • 2016-06-08
      相关资源
      最近更新 更多