可以在Nginx中循环遍历块并使用变量吗?

huangapple go评论59阅读模式
英文:

Is it possible to loop over blocks in Nginx and use variables?

问题

我想要做的是将传入 URL 的 /locale/ 部分替换为等效的查询字符串。我想在我的反向代理中实现这个功能,使用的是 Nginx。

考虑以下 Nginx 代码:

include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

location /
{
    proxy_pass http://localhost:3500;
}

location = /fa
{
    proxy_pass http://localhost:3500;

    rewrite ^/fa/(.*)$ /$1?locale=fa;
    rewrite ^/fa /?locale=fa;
}

location /fa/
{
    proxy_pass http://localhost:3500;

    rewrite ^/fa/(.*)$ /$1?locale=fa;
    rewrite ^/fa /?locale=fa;
}

location = /en
{
    proxy_pass http://localhost:3500;

    rewrite ^/en/(.*)$ /$1?locale=en;
    rewrite ^/en /?locale=en;
}

location /en/
{
    proxy_pass http://localhost:3500;

    rewrite ^/en/(.*)$ /$1?locale=en;
    rewrite ^/en /?locale=en;
}

location = /ar
{
    proxy_pass http://localhost:3500;

    rewrite ^/ar/(.*)$ /$1?locale=ar;
    rewrite ^/ar /?locale=ar;
}

location /ar/
{
    proxy_pass http://localhost:3500;

    rewrite ^/ar/(.*)$ /$1?locale=ar;
    rewrite ^/ar /?locale=ar;
}

location = /ru
{
    proxy_pass http://localhost:3500;

    rewrite ^/ru/(.*)$ /$1?locale=ru;
    rewrite ^/ru /?locale=ru;
}

location /ru/
{
    proxy_pass http://localhost:3500;

    rewrite ^/ru/(.*)$ /$1?locale=ru;
    rewrite ^/ru /?locale=ru;
}

location = /tr
{
    proxy_pass http://localhost:3500;

    rewrite ^/tr/(.*)$ /$1?locale=tr;
    rewrite ^/tr /?locale=tr;
}

location /tr/
{
    proxy_pass http://localhost:3500;

    rewrite ^/tr/(.*)$ /$1?locale=tr;
    rewrite ^/tr /?locale=tr;
}

正如您所看到的,许多代码都是重复的,包括位置块、正则表达式和 proxy_pass 指令等。我想要能够使这一部分更加干燥(Don't Repeat Yourself)。

基本上,每个区域都重复了以下部分:

location = /tr
{
    proxy_pass http://localhost:3500;

    rewrite ^/tr/(.*)$ /$1?locale=tr;
    rewrite ^/tr /?locale=tr;
}

location /tr/
{
    proxy_pass http://localhost:3500;

    rewrite ^/tr/(.*)$ /$1?locale=tr;
    rewrite ^/tr /?locale=tr;
}

是否有一种方法可以循环处理各个区域,而不重复这个部分呢?

英文:

What I want to do is to replace the /locale/ part of the incoming URL with an equivalent query string. I want to do this in my reverse proxy which is Nginx.

Consider this Nginx code:

	include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location /
{
proxy_pass http://localhost:3500;
}
location = /fa
{
proxy_pass http://localhost:3500;
rewrite ^/fa/(.*)$ /$1?locale=fa;
rewrite ^/fa /?locale=fa;
}
location /fa/
{
proxy_pass http://localhost:3500;
rewrite ^/fa/(.*)$ /$1?locale=fa;
rewrite ^/fa /?locale=fa;
}
location = /en
{
proxy_pass http://localhost:3500;
rewrite ^/en/(.*)$ /$1?locale=en;
rewrite ^/en /?locale=en;
}
location /en/
{
proxy_pass http://localhost:3500;
rewrite ^/en/(.*)$ /$1?locale=en;
rewrite ^/en /?locale=en;
}
location = /ar
{
proxy_pass http://localhost:3500;
rewrite ^/ar/(.*)$ /$1?locale=ar;
rewrite ^/ar /?locale=ar;
}
location /ar/
{
proxy_pass http://localhost:3500;
rewrite ^/ar/(.*)$ /$1?locale=ar;
rewrite ^/ar /?locale=ar;
}
location = /ru
{
proxy_pass http://localhost:3500;
rewrite ^/ru/(.*)$ /$1?locale=ru;
rewrite ^/ru /?locale=ru;
}
location /ru/
{
proxy_pass http://localhost:3500;
rewrite ^/ru/(.*)$ /$1?locale=ru;
rewrite ^/ru /?locale=ru;
}
location = /tr
{
proxy_pass http://localhost:3500;
rewrite ^/tr/(.*)$ /$1?locale=tr;
rewrite ^/tr /?locale=tr;
}
location /tr/
{
proxy_pass http://localhost:3500;
rewrite ^/tr/(.*)$ /$1?locale=tr;
rewrite ^/tr /?locale=tr;
}

As you can see, a lot of code is repetitive. This includes the location block and the regex and the proxy_pass directive, etc. I want to be able to DRY this part.

Basically, this part is getting repeated for each locale:

	location = /tr
{
proxy_pass http://localhost:3500;
rewrite ^/tr/(.*)$ /$1?locale=tr;
rewrite ^/tr /?locale=tr;
}
location /tr/
{
proxy_pass http://localhost:3500;
rewrite ^/tr/(.*)$ /$1?locale=tr;
rewrite ^/tr /?locale=tr;
}

Is there a way to loop over locales and not repeat this section?

答案1

得分: 2

rewrite语句可以在正则表达式中捕获区域设置,例如:

rewrite ^/(\w\w)/(.*)$ /$2?locale=$1 break;
rewrite ^/(\w\w)$ /?locale=$1 break;

使用正则表达式选择任何带有区域设置的URI,例如:

location ~ ^/(fa|en|ar|ru|tr)(/|$) { ... }

或更一般地(如果适用于您):

location ~ ^/\w\w(/|$) { ... }

示例:

location / {
proxy_pass http://localhost:3500;
}
location ~ ^/\w\w(/|$) {
rewrite ^/(\w\w)/(.*)$ /$2?locale=$1 break;
rewrite ^/(\w\w)$ /?locale=$1 break;
proxy_pass http://localhost:3500;
}
英文:

The rewrite statements can capture the locale in the regular expression, for example:

rewrite ^/(\w\w)/(.*)$ /$2?locale=$1 break;
rewrite ^/(\w\w)$ /?locale=$1 break;

Use a regular expression to select any URI with a locale, for example:

location ~ ^/(fa|en|ar|ru|tr)(/|$) { ... }

Or more generally (if this works for you):

location ~ ^/\w\w(/|$) { ... }

Example:

location / {
proxy_pass http://localhost:3500;
}
location ~ ^/\w\w(/|$) {
rewrite ^/(\w\w)/(.*)$ /$2?locale=$1 break;
rewrite ^/(\w\w)$ /?locale=$1 break;
proxy_pass http://localhost:3500;
}

huangapple
  • 本文由 发表于 2023年6月2日 14:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定