我试图从Nginxredirect15个不同的路线从文件夹到子域(我已经阅读了一些线程)。
我很困惑,因为这个redirect正在工作:
rewrite ^/admin(.*) $scheme://admin.example.com/$1 301;
而这一个不(在我的浏览器中没有更改的url):
rewrite ^/app(.*) $scheme://apps.example.com/$1 301;
我错过了什么 ?
那时候,我只有这两个redirect(我以后需要15个更复杂的东西)。
编辑 :更多的解释与我的完整的nginxconfiguration
server { listen 80 default_server; listen [::]:80 default_server; server_name example.com; passenger_enabled on; rails_env production; root /home/admin/rails/current/public; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # redirects # admin rewrite ^/admin(.*) $scheme://admin.example.com/$1 302; # apps rewrite ^/app(.*) $scheme://apps.example.com/$1 302; }
编辑2 :汤姆问,我想要做什么(我宁愿保持URL的秘密):
GET http://example.com => http://apps.example.com GET http://example.com/app => http://apps.example.com GET http://example.com/app/iphone => http://apps.example.com/iphone GET http://example.com/app/ipad => http://apps.example.com/ipad GET http://example.com/support/iphone => http://apps.example.com/support/iphone GET http://example.com/support/ipad => http://apps.example.com/support/ipad GET http://example.com/legal/privacy?params=toto => http://apps.example.com/legal/privacy?params=toto GET http://example.com/legal/terms?params=toto => http://apps.example.com/legal/terms?params=toto GET http://example.com/legal/about => http://apps.example.com/legal/about
我也有一些POSTredirect,但我想我需要一个代理,不是吗?
提前致谢。
编辑3 :所以这里是我的GETredirect后,汤姆回答:
# pages to redirect to admin.$host location /admin { return 301 $scheme://admin.$host; } # pages to redirect to apps.$host location /app { return 301 $scheme://apps.$host; } location /app/iphone { return 301 $scheme://apps.$host/iphone; } location /app/ipad { return 301 $scheme://apps.$host/ipad; } location /support/iphone { return 301 $scheme://apps.$host/support/iphone$is_args$args; } location /support/ipad { return 301 $scheme://apps.$host/support/ipad$is_args$args; } location /legal/privacy { return 301 $scheme://apps.$host/legal/privacy$is_args$args; } location /legal/terms { return 301 $scheme://apps.$host/legal/terms$is_args$args; } location /legal/about { return 301 $scheme://apps.$host/legal/about; }
这是伟大的工作!
现在,我将打开一个新的线程因为我无法使用我的proxy_pass创buildPOSTredirect。 看到那里 。
testing时使用302redirect,301s被caching。 清除浏览器caching并重新启动。 当你对你的configuration感到满意时,将302的下面改为301。
试试这个 – 我已经给出了几个例子,你可以把其余的工作。 你应该在nginx中读取评估顺序,但是对于精确匹配来说并不重要 – 它只适用于正则expression式匹配。
请注意,这应该做你所要求的,这可能不是你想要的 – 注意你没有在你的例子中包含一个结尾的斜杠。
它也看起来像你只是想redirect到应用程序域,是这样的? 如果是这样,那就容易多了。 但是这是你要求的,testing的。
location /support/ipad { # change to 301 when you're 100% happy with all redirects return 302 http://example.com/support/ipad; } location /app/iphone { return 302 http://example.com/app/iphone; } location ~* ^/\Z { return 302 http://whatever; }
使用正则expression式可能会有更高效的方法,但是如果您不redirect整个域,则单独执行这些操作可能会比较谨慎。 请注意,〜*表示接下来是一个正则expression式,它是在一个精确的URI之后进行评估的。
根据你所说的,根redirect需要有一个正则expression式来确保它只redirect根域,而不是像http://www.example.com/pagea.html那样 。 如果你希望redirect的一切都更容易。
另外,你是这个意思吗? 注意尾部的斜杠
GET http://example.com/ => http://apps.example.com/
^表示“URI的开始”,即没有网站的URL。 这意味着它只会匹配网站的根目录,而不是任何文件夹。 / Z表示“URI的结束”。 您可以在浏览器标题中输出debugging信息来帮助解决这个问题
location whatever { add_header Z_LOCATION "(any text you want)"; add_header URI $uri; }