我正在使用nginx创建一个反向代理,用于在现有的遗留系统和基于Rails的新应用程序之间进行代理。默认情况下,我们会将所有流量发送到Rails站点,除非它们与某些URL匹配,或者如果请求主体不包含某些参数。但是,在某些情况下,URL与遗留条件匹配,但如果存在某些参数,则需要重定向到新系统。例如,我们有一个/index.cfm?fuseaction=search的网址,它通常会被发送到遗留系统,但由于它包含fuseaction=search,我需要将其重定向到/search/events处的新系统。我已经编写了以下配置。
upstream legacy {
server 10.0.0.1:80;
}
upstream rails {
server 10.0.0.2:80;
}
server {
listen 0.0.0.0:80;
server_name mydomain.com
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://rails;
if ($request_body ~* fuseaction(?!=public\.search(_all(_by_type)?)?))
{
proxy_pass http://legacy;
}
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering 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;
}
location ~* /(index.cfm|images|uploads|admin|htmlArea|innovastudio|js|scripts|w3c)*$ {
proxy_pass http://legacy;
if ($request_body ~* fuseaction=public\.search(_all(_by_type)?)?)
{
proxy_pass http://rails/search/events;
}
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering 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;
}
} 然而,当我尝试启动nginx时,我得到了以下错误:
"proxy_pass" may not have URI part in location given by regular expression, or inside named location, or inside the "if" statement, or inside the "limit_except" block in /etc/nginx/sites-enabled/default:56我理解这个错误是什么意思,但我不确定如何修复它。也许proxy_pass不是我需要的?也许我需要使用proxy_redirect?任何关于我如何解决这个问题的帮助都将不胜感激。
发布于 2013-07-01 05:20:51
在试图理解这个问题的过程中,我意识到我真正需要的是重定向,而不是试图代理。除了我最终更改的许多内容之外,我还检查了$args变量,而不是$request_body。下面是我完成时重写代码块的样子
if ($args ~ fuseaction\=public\.race_search(_all(_by_type)?)?)
{
rewrite ^(.*)$ /search/events? permanent;
}
if ($args ~ fuseaction\=public\.tools)
{
rewrite ^(.*)$ /directors? permanent;
}
if ($args ~ fuseaction\=public\.contact)
{
rewrite ^(.*)$ /contact? permanent;
}
if ($args ~ fuseaction\=public\.spotlight)
{
rewrite ^(.*)$ /search/events? permanent;
}
if ($args ~ fuseaction\=public\.results)
{
rewrite ^(.*)$ /search/results? permanent;
}第一个参数匹配整个路径,第二个参数告诉它要重写到什么位置,尾随的?用于从重写中删除任何查询参数,第三个参数执行301 (永久)重定向。
https://stackoverflow.com/questions/16484255
复制相似问题