2012-11-08 46 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

    相關問題