Nginx перезаписывает каталоги коммутаторов

У меня есть следующее правило перезаписи:

location ^~ /assets {
    rewrite_log on;
    rewrite ^/assets/(.*)/(.*)$ /websites/$host/themes/

I have the following rewrite rule:

location ^~ /assets {
    rewrite_log on;
    rewrite ^/assets/(.*)/(.*)$ /websites/$host/themes/$1/assets/$2 break;
}

So, when I request http://localhost/assets/theme1/css/style.css I expect it to rewrite it to this:

/websites/localhost/themes/theme1/assets/css/style.css

Instead, it rewrites the request to this:

/websites/localhost/themes/theme1/css/assets/style.css

But, when I change the rewrite rule to:

rewrite ^/assets/(.*)/test/(.*)$ /websites/$host/themes/$1/assets/$2 break;

And I request http://localhost/assets/theme1/test/css/style.css everything works just fine.

Why does it switch assets with css?

/assets/ break;
}

Поэтому, когда я прошуhttp://localhost/assets/theme1/css/style.css, я ожидаю, что он перепишет его на это:

/websites/localhost/themes/theme1/assets/css/style.css

Вместо этого он переписывает запрос на это:

/websites/localhost/themes/theme1/css/assets/style.css

Но, когда я изменяю правило перезаписи на:

rewrite ^/assets/(.*)/test/(.*)$ /websites/$host/themes/

I have the following rewrite rule:

location ^~ /assets {
    rewrite_log on;
    rewrite ^/assets/(.*)/(.*)$ /websites/$host/themes/$1/assets/$2 break;
}

So, when I request http://localhost/assets/theme1/css/style.css I expect it to rewrite it to this:

/websites/localhost/themes/theme1/assets/css/style.css

Instead, it rewrites the request to this:

/websites/localhost/themes/theme1/css/assets/style.css

But, when I change the rewrite rule to:

rewrite ^/assets/(.*)/test/(.*)$ /websites/$host/themes/$1/assets/$2 break;

And I request http://localhost/assets/theme1/test/css/style.css everything works just fine.

Why does it switch assets with css?

/assets/ break;

И я прошуhttp://localhost/assets/theme1/test/css/style.css, чтобы все работало просто отлично.

Почему он переключается assetsс css?

1 ответ

  1. Ах, я думаю, что я немного запутался в том, как работает regex. 😉 Это работает отлично:

    rewrite ^/assets/([^/]+)/(.*)$ /websites/$host/themes/

    Ah, I guess I got a bit confused about how regex works. ;) This works perfectly:

    rewrite ^/assets/([^/]+)/(.*)$ /websites/$host/themes/$1/assets/$2 break;

    I matched everything in $1, which was theme1/css, but because I only want the theme name, I have to exclude the / character. Thanks anyways for reading this!

    /assets/ break;

    Я сопоставил все

    Ah, I guess I got a bit confused about how regex works. ;) This works perfectly:

    rewrite ^/assets/([^/]+)/(.*)$ /websites/$host/themes/$1/assets/$2 break;

    I matched everything in $1, which was theme1/css, but because I only want the theme name, I have to exclude the / character. Thanks anyways for reading this!

    , что былоtheme1/css, но поскольку я хочу только название темы, я должен исключить /персонажа. В любом случае спасибо за чтение!