我试图创build一个自定义的redirect规则,但它的工作。 日志对我没有帮助。
我想configuration2个规则:
/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"
在我的testing中,一切都应该解决到本地主机。 在生产服务器中,DNS将关心应用程序名称/ DNS。
任何build议?
日志条目表示您的邮箱无法parsingapp1-edge主机名。 如果你想要本地主机来服务它,你需要编辑系统主机文件,并添加以下条目:
127.0.0.1 app1-edge
然后,我会稍微改变正则expression式:
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; } }
首先,正则expression式不需要引号或者斜线的转义。 其次,最好用“ +来表示“一个或多个”的匹配。
这里的假设是path的最后一部分至less包含一个字符,也就是说,没有像https://www.example.com/api/appname或https://www.example.com/api/appname/这样的URL https://www.example.com/api/appname/ 。
如果存在这样的URL,正则expression式可以是^/api/([^/]+)(/.*)$来处理这些情况。
你还应该注意,在你的configuration中只有一个server块使用server_name localhost 。 否则nginx将只使用其中一个虚拟主机,这可能不是这个虚拟主机。
我还删除了原始configuration中的两个location块,因为在server级别定义root和index指令时可以产生相同的效果。
通过在nginx.conf中添加这行代码,我们在Nginxredirect方面取得了成功:
include /etc/nginx/conf.d/*.conf;
然后为每个redirect提供一个文件/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 。