0

I would like to do like this

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

And I tried to do this but infinity mistakes stoped me. When I'm trying to use localhost then I see it.

Rewrite or internal redirection cycle while processing "/index.html" - this error 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;
        # }

    }

I know stack has a lot of questions for this problem but I used to be all of them and always catched error.

1
  • I have one file - index.html and inside it has many addiction. Client code on angular 2. I used to be break and then there is a mistake too... on side angular 2. Nginx don't give all files which need. I don't now how to do this have you any suggestion? Commented Jan 28, 2017 at 10:38

1 Answer 1

1

Using rewrite ... last inside a location block will just cycle around rewriting /index.html to /index.html. You should use rewrite ... break instead. See this document for details.

If your application also requires resources (css, js, images), you may adopt an alternative approach that returns the static file if it exists, for example:

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;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I get a mistake nginx: [emerg] named location "@other" can be on the server level only in C:\nginx-1.11.9/conf/nginx.conf:61. With break - nginx works - but one probrem, it don't give all addiction files inside index.html.
Angular 2 has a lot files and it spread to many folders. Thank you for your help
My answer should replace your existing location / block, so that both blocks in my answer are at the server level - see updated answer.
Oh, yes but a mistake on side angular still exist. If I use this code location / { root C:\Users\Sergey\Desktop\xxx\angular2do } All works but nginx requested only for mine page "/" - on other page( for example localhost/category) - no. I don't understand why in your suggestion angular showed in console that some files addiction can not get.
Seems all works. I removed cash and all works. Thank you very match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.