Score:0

NginX dynamic rewrite location and alias

gn flag

For developing purpose, my apps need to be tested on different version,
so my idea was to serve it in different url like this :

http://example.com/v3/
http://example.com/v4/
.....
http://example.com/v20/
http://example.com/v45/

my files/folder like this :

/var/www/
      | apps/
          | 3/public/index.php
          | 4/public/index.php
            ....
          | 20/public/index.php
          | 45/public/index.php

using below configuration, i could make it work like expected

location /v3 {
    alias /var/www/apps/3/public;
    try_files $uri $uri/ @versioning;
    include /var/local/config/php7.4.cnf;
}

location /v4 {
    alias /var/www/apps/4/public;
    try_files $uri $uri/ @versioning;
    include /var/local/config/php7.4.cnf;
}
# ......

location /v25 {
    alias /var/www/apps/25/public;
    try_files $uri $uri/ @versioning;
    include /var/local/config/php7.4.cnf;
}
# ......

location /v30 {
    alias /var/www/apps/30/public;
    try_files $uri $uri/ @versioning;
    include /var/local/config/php7.4.cnf;
}

location @versioning { 
    rewrite ^/v([0-9]+)/(.*)$ /v$1/index.php/$2 last; 
}

but, for every version release i need to alter the nginx.conf file.
Therefore I changed it to regex rewrite location with aliases

location ~ ^/v(?<version>[a-z0-9]*)/$ {
    alias /var/www/apps/$version/public;
    try_files $uri $uri/ @versioning;
    include /var/local/config/php7.4.cnf;
}

location @versioning { 
    rewrite ^/v([0-9]+)/(.*)$ /v$1/index.php/$2 last; 
}

using the later config return 404. am i missing something ?
nginx with debug, show following log :

[debug] *1 generic phase: 0
[debug] *1 rewrite phase: 1
[debug] *1 test location: "/"
[debug] *1 test location: ~ "\.php$"
[debug] *1 test location: ~ "/\.ht"
[debug] *1 test location: ~ "^/v(?<version>[a-z0-9]*)/$"
[debug] *1 http regex set $version to "3"
[debug] *1 test location: ~ "\.php$"
[debug] *1 using configuration "^/v(?<version>[a-z0-9]*)/$"
...
[debug] *1 try files handler
[debug] *1 http script copy: "/var/www/apps/"
[debug] *1 http script var: "3"
[debug] *1 http script copy: "/public"
[debug] *1 http script var: "/v3/"
[debug] *1 trying to use file: "/v3/" "/var/www/apps/3/public/v3/"
[debug] *1 add cleanup: 00005598627533A0
[debug] *1 malloc: 0000559862745E40:144
[debug] *1 malloc: 0000559862742430:27
[debug] *1 cached open file: /var/www/apps/3/public/v3/, fd:-1, c:0, e:2, u:1
[debug] *1 http script var: "/v3/"
[debug] *1 trying to use dir: "/v3/" "/var/www/apps/3/public/v3/"
[debug] *1 add cleanup: 00005598627533D8
[debug] *1 cached open file: /var/www/apps/3/public/v3/, fd:-1, c:0, e:2, u:2
[debug] *1 trying to use file: "@versioning" "/var/www/apps/3/public@versioning"
[debug] *1 test location: "@versioning"
[debug] *1 using location: @versioning "/v3/?"
[debug] *1 rewrite phase: 3
[debug] *1 http script regex: "^/v([0-9]+)/(.*)$"
[notice: *1 "^/v([0-9]+)/(.*)$" matches "/v3/", client: xx.192.153.44, server: _, request: "GET /v3/ HTTP/1.1", host: "xx.15.38.80"
[debug] *1 http script copy: "/v"
[debug] *1 http script capture: "3"
[debug] *1 http script copy: "/index.php"
[debug] *1 http script args
[debug] *1 http script copy: "/"
[debug] *1 http script capture: ""
[debug] *1 http script regex end

i think the problem is the path nginx look it up was wrong

[debug] *1 trying to use file: "/v3/" "/var/www/apps/3/public/v3/"
[debug] *1 cached open file: /var/www/apps/3/public/v3/, fd:-1, c:0, e:2, u:1
[debug] *1 trying to use dir: "/v3/" "/var/www/apps/3/public/v3/"

/var/www/apps/3/public/v3/ it append more v3 at the end, whereas it show the correct path /var/www/apps/3/public if i use static rewrite.

OldFart avatar
uz flag
what you see wrong is part of your try_files. One with uri and the other with uri/ (which is in your case, the same) and then @versioning. Your question lacks alot of important information in order to be able to properly reply without asking for more info, assuming stuff and thus potentially loosing everyone's time - yours included. Try creating a VERY basic, ALL-INCLUSIVE config of what you want stuff to act, php config included. A full config would give everyone better idea so as what exact url tried goes with what exact log you show. try_files can be outside of location blocks, too
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.