Javascript nginx:将所有请求发送到单个 html 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7027636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:18:59  来源:igfitidea点击:

nginx: send all requests to a single html page

javascriptnginxhistory

提问by prismofeverything

Using nginx, I want to preserve the url, but actually load the same page no matter what. I will use the url with History.getState()to route the requests in my javascript app. It seems like it should be a simple thing to do?

使用nginx,我想保留url,但无论如何实际上加载相同的页面。我将使用 urlHistory.getState()来路由我的 javascript 应用程序中的请求。看起来应该是一件很简单的事情吧?

location / {
    rewrite (.*) base.html break;
}

works, but redirects the url? I still need the url, I just want to always use the same page.

工作,但重定向网址?我仍然需要网址,我只想始终使用相同的页面。

回答by Alex Howansky

I think this will do it for you:

我认为这将为您做到:

location / {
    try_files /base.html =404;
}

回答by Kevan Stannard

Using just try_filesdidn't work for me - it caused a rewrite or internal redirection cycleerror in my logs.

使用只是try_files对我不起作用 - 它导致我的日志中出现重写或内部重定向循环错误。

The Nginx docs had some additional details:

Nginx 文档有一些额外的细节:

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

So I ended up using the following:

所以我最终使用了以下内容:

root /var/www/mysite;

location / {
    try_files $uri /base.html;
}

location = /base.html {
    expires 30s;
}

回答by kolbyHyman

Your original rewrite should almost work. I'm not sure why it would be redirecting, but I think what you really want is just

你原来的重写应该几乎可以工作。我不确定为什么会重定向,但我认为您真正想要的只是

rewrite ^ /base.html break;

You should be able to put that in a location or directly in the server.

您应该能够将其放在某个位置或直接放在服务器中。

回答by Mark Chackerian

This worked for me:

这对我有用:

location / {
    alias /path/to/my/indexfile/;
    try_files $uri /index.html;
}

This allowed me to create a catch-all URL for a javascript single-page app. All static files like css, fonts, and javascript built by npm run buildwill be found if they are in the same directory as index.html.

这允许我为 javascript 单页应用程序创建一个包罗万象的 URL。所有静态文件,如 css、字体和 javascript 构建的,npm run build如果它们与index.html.

If the static files were in another directory, for some reason, you'd also need something like:

如果静态文件位于另一个目录中,出于某种原因,您还需要以下内容:

# Static pages generated by "npm run build"
location ~ ^/css/|^/fonts/|^/semantic/|^/static/ {
    alias /path/to/my/staticfiles/;
}

回答by Abhishek

This worked for me:

这对我有用:

location / {
    try_files $uri $uri/ /base.html;
}

回答by Etherealone

The correct way would be:

正确的方法是:

location / {
    rewrite (.*) base.html last;
}

Using lastwill make nginx find a new suitable locationblock according to the result of rewriting.

使用last将使 nginxlocation根据重写的结果找到一个新的合适的块。

try_filesis also a perfectly valid approach to this problem.

try_files也是解决这个问题的一种完全有效的方法。