【问题标题】:opening other websites links in nginx server在 nginx 服务器中打开其他网站链接
【发布时间】:2012-10-28 08:20:25
【问题描述】:

我是 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 但我就是不能让它工作。任何帮助将不胜感激。谢谢

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;
    }      
}

【问题讨论】:

标签: nginx


【解决方案1】:

您根本没有从您发布的 nginx 配置中进行任何重定向, 除 /static/ 和 /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;
}

(更复杂的情况涉及比^更复杂的正则表达式)

更新:

对,所以从您在下面评论中链接的代码中,您真正想要做的是更改输出的 html 代码锚标记的 href 值。

这样做的正确位置是在后端代码中(即在您要连接的 uwsgi 应用程序中)

您可以通过以下重写来做到这一点:

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

但是这有一个很大的缺点,即需要额外往返服务器,然后客户端会这样做:

  1. 向您的服务器发出请求
  2. 响应重定向到其他服务器
  3. 向其他服务器请求
  4. 来自其他服务器的响应

而如果您在后端代码中更改它,则不再需要步骤 1 和 2。

除了导致潜在的(取决于连接速度和服务器负载)显着延迟。它还会增加您的服务器负载。

使用服务器重写是一种技巧,除非你无权访问后端代码,否则你真的应该跳过它

【讨论】:

  • 所以在阅读了重定向之后,我可以想出这个edited link
  • 所以我应该在我的应用代码中添加重定向。届时将尝试这样做,非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 2014-01-21
  • 2021-03-29
  • 2015-01-21
  • 2016-03-03
相关资源
最近更新 更多