“proxy_pass"正则表达式给出的位置中不能有 URI 部分 [英] "proxy_pass" cannot have URI part in location given by regular expression

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

问题描述

我开发了一个 URL 缩短网络应用程序.

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.

如果一个 url 以 / 开头,我正在代理前端.

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?

推荐答案

如果您在 正则表达式 locationproxy_pass 语句的 URIcode>,您需要使用一个或多个变量构建整个 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天全站免登陆