0

I have read the documentation and seems it is the same as explained here enter link description here, but during my tests the root context is having precedence over the regular expression one.

Does anyone know the reason?

See my nginx.conf file and a test curl I did to validate the configuration:

http {
    server {
        listen 80;
        root  /usr/share/nginx/html;
        include /etc/nginx/mime.types;
                                      
        location ~ /ui(/.*) {
            try_files $1 $1/ @htmlext;
        }                             
         
        location @htmlext {
            rewrite ^/ui(/\w*)(/.*)*$ $1.html last;
        }                                          
         
        location / {
            return 301 /ui$request_uri;
        }
    }
}

/ # curl -v http://localhost/ui/message?msg=error.forbidden
*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /ui/message?msg=error.forbidden HTTP/1.1
> Host: localhost
> User-Agent: curl/7.67.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.19.0
< Date: Thu, 09 Jul 2020 22:34:49 GMT
< Content-Type: text/html
< Content-Length: 169
< Location: http://localhost/ui/ui/message?msg=error.forbidden
< Connection: keep-alive
< 
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>
* Connection #0 to host localhost left intact

As you can see in the curl it is redirecting my /ui/message page instead of rendering it. If I remove the location / from the configuration it seems working, if I remove the @htmlext location and add that logic in the location ~ /ui(/.*) it seems working too. Then I think the reason of my error is the @htmlext redirect.

Does anyone know why?

Thanks

javendo
  • 13
  • 1
  • 5
  • Your `location /` block matches **any** URI that does not begin with `/ui`. So as soon as the URI is rewritten to `/message.html` it generates the return 301. – Richard Smith Jul 10 '20 at 16:09
  • Thanks for your comment. By the way, that does not happen when I remove the `location @htmlext` and change the first one to `location ~ /videocon(/\w*)(/.*)*$ { try_files $1 $1/ $1.html last; }`. Do you know why? – javendo Jul 11 '20 at 18:35
  • Yes. The *file* terms of the `try_files` statement are evaluated within the same `location` context. The `rewrite...last` restarts the search for a `location` block and finds the one containing the `return 301`. – Richard Smith Jul 12 '20 at 07:23

0 Answers0