0

I am new to Nginx and I have set up the server using this tutorial. Now when I add links to other sites like http://www.anotherwebsite.com and when I click on this link from my page the server directs me to http://www.mywebsite.com/http://www.anotherwebsite.com. The server is appending the other link to my website link. How do I change that. I have looked into these places How to redirect a url in NGINX, Rewrite to pretty links with nginx but I just cant make it work. Any help will be appreciated. Thanks

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;
    }      
}
Community
  • 1
  • 1
Saad
  • 1,856
  • 1
  • 22
  • 28

1 Answers1

2

you're not doing any redirecting at all from your posted nginx config, everything except /static/ and /50x.html is passed on to the uwsgi app

consequently the redirecting has to be happening in the uwsgi app

As far as redirecting from within nginx goes, the simple case goes like this:

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

(the more complicated cases involve regexes more complicated then ^)

UPDATE:

right, so from the code you linked in the comment below, what you really want to do is change the href value of the outputted html code anchor tag.

The proper place to do so is in the backend code (i.e. in the uwsgi app you're connecting to)

You could do it with the following rewrite:

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

BUT this has the big drawback of requiring an extra roundtrip to the server, a client then does:

  1. request to your server
  2. response with redirect to the other server
  3. request to the other server
  4. response from the other server

whereas if you change it in the backend code steps 1 and 2 are no longer needed.

Besides causing a potentially (depending on connection speed and server load) noticable delay. It will also increases your server load.

Doing it with a server-rewrite is a hack you really should skip unless you have no access to the backend code

cobaco
  • 10,224
  • 6
  • 36
  • 33
  • so after reading about redirects I could come up with this [edited link](http://pastecode.org/index.php/view/95312000) – Saad Nov 08 '12 at 11:08
  • so I should add redirection in my app code. Will try to do that then and thanks a lot for your help. – Saad Nov 08 '12 at 23:11