2012-11-08 47 views
0

我是Nginx新手,我已经使用this tutorial设置了服务器。现在,当我添加链接到其他网站,如http://www.anotherwebsite.com,当我从我的页面上点击此链接时,服务器将我指向http://www.mywebsite.com/http://www.anotherwebsite.com。服务器正在将其他链接附加到我的网站链接。我该如何改变这一点。我已经看过这些地方 How to redirect a url in NGINX, Rewrite to pretty links with nginx 但我只是不能让它的工作。任何帮助将不胜感激。由于在nginx服务器中打开其他网站的链接

server { 
    listen   80; 
    server_name  $hostname; 
    location /static { 
     alias /var/www/<app-name>/static; 
    } 
    error_page 404    /404.html; 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root /usr/share/nginx/html; 
    } 
    location/{ 
     include   uwsgi_params; 
     uwsgi_pass unix:/tmp/uwsgi.sock; 
     uwsgi_param UWSGI_PYHOME /var/www/<app-name>/env; 
     uwsgi_param UWSGI_CHDIR /var/www/<app-name>/project; 
     uwsgi_param UWSGI_MODULE <project-name>.wsgi:application; 
    }  
} 
+0

那么你的当前配置看起来像没有访问权限的黑客? – cobaco

+0

[链接到配置文件](http://pastecode.org/index.php/view/54100324) – Saad

回答

2

你没有做任何重定向从公布的nginx配置可言, 一切,除了/静态/和/50x.html是到uwsgi应用程序通过

因此重定向必须在uwsgi应用是发生

至于从内部去nginx的重定向,简单的情况是这样的:

location /redirected-path { 
    #for 302 redirect 
    rewrite^http://anotherwebsite.example.com/path-on-other-site redirect; 

    #for 301 redirect 
    # rewrite^http://anotherwebsite.example.com/path-on-other-site permanent; 
} 

(较复杂的案件涉及的正则表达式更复杂然后^

UPDATE:

正确的,所以从你的代码在下面的评论链接,你真正想要做的是改变输出的href值html代码锚定标记。

合适的地方这样做,是在后端代码(即在uwsgi应用程序要连接)

你可以用下面的改写做到这一点:

location /main { 
    rewrite ^/main(.*) http://www.mysite.com$1 permanent; 
} 

这有需要额外的往返到服务器的大缺点,客户端然后做:

  1. 请求到服务器
  2. 响应与重定向到另一服务器
  3. 请求到其他服务器从其他服务器

而如果你步骤1和2不再需要改变它在后台代码

  • 响应。

    除了造成潜在的(取决于连接速度和服务器负载)明显的延迟。它也会增加你的服务器负载。

    与服务器重写做起来你真的应该跳过,除非你有到后端代码

  • +0

    所以在阅读重定向后,我可以拿出这个 [编辑链接](http://pastecode.org/ index.php/view/95312000) – Saad

    +0

    所以我应该在我的应用程序代码中添加重定向。将尽力做到这一点,并非常感谢您的帮助。 – Saad

    相关问题