Nginx 正则表达式重定向

Nginx 正则表达式重定向

我正在尝试创建自定义重定向规则,但它根本不起作用。日志对我没有帮助。

我想配置2条规则:

/api/appname/* => http://appname-edge:3000/appname/*
/appname/* => http://appname-ui:80/*

这是我的尝试:

server {
    listen       80;
    server_name  localhost;

    # redirect /api/myApp => http://myApp-edge:3000/myApp/

    location ~* "^\/api\/(.*?)\/(.*)" {
        proxy_pass http://$1-edge:3000/$1/$2;
    }

    # redirect /myApp => http://myApp-ui:80/

    location ~* "^\/(.*?)\/(.*)" {
        proxy_pass http://$1-ui/$2;
    }

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

日志:

2017/03/30 16:32:25 [error] 13870#0: *1 no resolver defined to resolve app1-edge, client: 127.0.0.1, server: localhost, request: "GET /api/app1/foo/bar HTTP/1.1", host: "localhost"
2017/03/30 16:39:24 [warn] 14152#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2017/03/30 16:39:25 [warn] 14156#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2017/03/30 16:39:32 [warn] 14197#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2017/03/30 16:39:41 [error] 14199#0: *1 no resolver defined to resolve app1-edge, client: 127.0.0.1, server: localhost, request: "GET /api/app1/foo/bar HTTP/1.1", host: "localhost"
2017/03/30 16:42:22 [warn] 14321#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2017/03/30 16:42:24 [warn] 14325#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2017/03/30 16:42:32 [error] 14328#0: *1 no resolver defined to resolve app1-edge, client: 127.0.0.1, server: localhost, request: "GET /api/app1/foo/bar HTTP/1.1", host: "localhost"
2017/03/30 16:46:43 [emerg] 14628#0: host not found in resolver "$1-edge" in /etc/nginx/conf.d/ims.conf:9
2017/03/30 16:46:52 [emerg] 14658#0: host not found in resolver "$1-edge" in /etc/nginx/conf.d/ims.conf:8
2017/03/30 16:46:55 [error] 14328#0: *2 no resolver defined to resolve app1-edge, client: 127.0.0.1, server: localhost, request: "GET /api/app1/foo/bar HTTP/1.1", host: "localhost"

在我的测试中,所有内容都应解析为 localhost。在生产服务器中,DNS 将负责应用程序名称/DNS。

有什么建议吗?

答案1

日志条目意味着您的机器无法解析app1-edge主机名。如果您希望 localhost 为其提供服务,则需要编辑系统 hosts 文件并向其中添加以下条目:

127.0.0.1 app1-edge

然后,我会稍微改变一下正则表达式:

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    error_page   500 502 503 504  /50x.html;

    # redirect /api/myApp => http://myApp-edge:3000/myApp/

    location ~* ^/api/([^/]+)/(.+)$ {
        proxy_pass http://$1-edge:3000/$1/$2;
    }

    # redirect /myApp => http://myApp-ui:80/

    location ~* ^/([^/]+)/(.+)$ {
        proxy_pass http://$1-ui/$2;
    }
}

首先,正则表达式不需要引号或斜线转义。其次,+对于“一个或多个”匹配,最好使用。

这里的假设是路径的最后一部分在所有情况下都至少包含一个字符,也就是说,不存在像https://www.example.com/api/appname或 这样的 URL https://www.example.com/api/appname/

如果存在这样的 URL,则正则表达式可以^/api/([^/]+)(/.*)$处理这些情况。

你还应该注意,你server的配置中只有一个块server_name localhost。否则,nginx 将只使用其中一个虚拟主机,而这个虚拟主机可能不是这个。

我还删除了您原始配置中的两个块,因为您在级别上定义和指令location时可以获得相同的效果。rootindexserver

答案2

我通过以下行成功实现了 Nginx 重定向nginx.conf

include /etc/nginx/conf.d/*.conf;

然后/etc/nginx/conf.d/redirects.conf为每个重定向创建一个包含简短节的文件:

server {
    listen 80;
    server_name to-be-redirected.example.com
    return 301 https://example.com/its-redirected/sample.html
}

然后运行nginx -s reload

相关内容