【问题标题】:Nginx `location /` does not match `/some_path`Nginx `location /` 与 `/some_path` 不匹配
【发布时间】:2016-06-26 15:40:03
【问题描述】:

请帮我配置 nginx:我希望 nginx 返回 index.html 用于所有 URL,例如 10.10.256.142/10.10.256.142/some_path10.10.256.142/other_path/lala。问题:目前它只为10.10.256.142/ URL 返回index.html

我的当前设置

listen 80;
server_name  10.10.256.142; 
server_name_in_redirect  off;
resolver  127.0.0.1;

location / {
    error_page 405 =200 $uri;
    root   /some_path/project_dir;
    index  index.html index.htm;
}

location /websocket {
    # ....

【问题讨论】:

    标签: nginx location


    【解决方案1】:

    对我来说,最简单的解决方案是:

    root /some_path/project_dir;
    
    location / {
        rewrite ^ /index.html break;
    }
    
    location /websocket/ {
        # ...
    }
    

    【讨论】:

    • 感谢您的回答!你能解释一下它是如何工作的吗?
    • 您的代码不起作用,因为 nginx 停止返回静态资产 (assets/bundle.js) 并改为返回 inedx.html
    • 你错过了问题中的那部分
    • 我的错。我认为 rewrite ^ 我应该写像 rewrite <everything_except_/assets/_regexp> 这样的正则表达式。那正确吗?我该怎么做?
    【解决方案2】:

    只是为了完成上面的答案并返回我必须编写的静态资产

    root  /srv/www/betbull;
    location / {
        if ($uri !~ (/assets/.*)) { # do not return index.html instead of static assets
            rewrite ^ /index.html break;
        }
    }
    

    更新

    更好的解决方案:

    location / {
        try_files $uri $uri/ index.html$query_string
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 2020-01-18
      • 1970-01-01
      相关资源
      最近更新 更多