所有网址上的nginx index.html

时间:2017-01-28 09:34:57

标签: nginx nginx-location

我想这样做

localhost/ | index.html
localhost/category | index.html
localhost/notes | index.html
localhost/notes/note1 | index.html

我试图这样做,但是无限错误使我陷入困境。 当我尝试使用localhost时,我会看到它。

处理“/index.html”时,

重写或内部重定向循环 - 此错误500.

server {
        listen       80;
        server_name  localhost;

        root C:/Users/Sergey/Desktop/xxx/angular2do;

        location / {
            if ($request_method = POST) {
                proxy_pass http://localhost:3000;
            } 

            if ($request_method = GET) {
                rewrite ^.*$ /index.html last;
            }

        }

        #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;
        # }

    }

我知道堆栈对于这个问题有很多问题,但我曾经是所有这些问题并且总是遇到错误。

1 个答案:

答案 0 :(得分:1)

rewrite ... last块中使用location只会循环重写/index.html/index.html。您应该使用rewrite ... break代替。有关详细信息,请参阅this document

如果您的应用程序还需要资源(css,js,images),您可以采用另一种方法返回静态文件(如果存在),例如:

server {
    listen       80;
    root C:/Users/Sergey/Desktop/xxx/angular2do;

    location / {
        try_files $uri @other;
    }
    location @other {
        if ($request_method != POST) { rewrite ^ /index.html last; }
        proxy_pass http://localhost:3000;
    }
}