"proxy_pass"正则表达式不能在位置中包含URI部分 [英] "proxy_pass" cannot have URI part in location given by regular expression

查看:789
本文介绍了"proxy_pass"正则表达式不能在位置中包含URI部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个URL缩短Web应用程序.

I've developed a URL shortening web application.

它由两个单独的Docker容器组成:一个包含后端REST api,另一个包含前端静态网站.

It consists of two separate docker containers: one containing the backend REST api and another containing the frontend static website.

这两个容器链接到nginx容器. 此Nginx容器的配置如下:

These two containers are linked to an nginx container. The configuration for this nginx container is below:

worker_processes 1;

events { worker_connections 1024; }

http {
    upstream api {
        server short-url:8080;
    }

    upstream frontend {
        server short-url-frontend:8081;
    }

    gzip on;
    gzip_vary on;
    gzip_min_length 860;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml application/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.";

    server {
        listen 80;
        root    /user/share/nginx/html;

        location /urlshortener/v1 {
            proxy_pass         http://api/urlshortener/v1;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

        location ~ ^/([A-Za-z0-9]+) {
            rewrite ^/([A-Za-z0-9]+) /$1
            proxy_pass         http://api/urlshortener/v1;
        }

        location / {
            proxy_pass          http://frontend;
            proxy_set_header    Host              $host;
            proxy_set_header    X-Real-IP         $remote_addr;
            proxy_set_header    X-Forwarded-for   $remote_addr;
        }
    }

}

如果url以/urlshortening/v1结尾,则表示代理到后端.

If a url ends with /urlshortening/v1, I'm proxying to the backend.

如果网址以/开头,则表示代理到前端.

If a url starts with /, I'm proxying to the frontend.

缩短的网址,例如/3xTy/a0q需要代理到后端,以便可以将用户导航到原始URL.为此,我用正则表达式定义了一个位置.

Shortened urls e.g. /3xTy or /a0q need to be proxied to the backend so that the user can be navigated to the original url. In order to do this, I've defined a location with a regular expression.

location ~ ^/([A-Za-z0-9]+) {
    rewrite ^/([A-Za-z0-9]+) /$1
   proxy_pass         http://api/urlshortener/v1;
}

此代码段给我以下错误:

This block of code gives me the following error:

2018/11/17 16:47:03 [emerg] 1#1:"proxy_pass"不能在正则表达式指定的位置,命名位置内部,"if"语句内部或"limit_except"内部具有URI部分"/etc/nginx/nginx.conf:36

2018/11/17 16:47:03 [emerg] 1#1: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/nginx.conf:36

我已经查看了几个示例,并查看了答案的数量,我相信我的配置应该可以使用.有人可以解释为什么我会收到此错误吗?

I've gone through several examples and reviewed a number of answers and I believe that the configuration I have should work. Can someone please explain why I'm getting this error?

推荐答案

如果在正则表达式 location中使用带有proxy_pass语句的URI,则需要构建整个URI使用一个或多个变量.有关详细信息,请参见本文档.

If you use a URI with a proxy_pass statement within a regular expression location, you need to build the entire URI using one or more variables. See this document for details.

因此,替代方法是(1),从location表达式中捕获URI,并将其添加到proxy_pass语句中.例如:

So the alternatives are to (1), capture the URI from the location expression and add it to the proxy_pass statement. For example:

location ~ ^/([A-Za-z0-9]+) {
    proxy_pass http://api/urlshortener/v1/$1;
}

或(2),使用不带URI部分的proxy_pass,并使用rewrite...break构造所需的URI.例如:

Or (2), use proxy_pass without a URI part, and construct the desired URI using a rewrite...break. For example:

location ~ ^/([A-Za-z0-9]+) {
    rewrite ^/([A-Za-z0-9]+) /urlshortener/v1/$1 break; 
    proxy_pass http://api;
}

有关详细信息,请参见此文档.

See this document for details.

这篇关于"proxy_pass"正则表达式不能在位置中包含URI部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆