Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

nginx can be tricky sometimes:

I want to

  • redirect all incoming requests for www.sub.domain.com/* to www.other.domain.com/*
  • use a proxy so users still see the original www.sub.domain.com when browsing the site
  • add an exception for specific URLs (all the same kind): www.sub.domain.com/script needs to redirect to www.yet.another.domain.com/www.sub.domain.com/new_script, preserving the entered POST data for the script. With the little trickiness that the folder containing new_script is named just after the original domain.

Here's what I got so far:

server { listen xx.xx.xx.xx:80; root /var/www; index index.html index.htm; server_name www.sub.domain.com;

    location /cms/ {
            proxy_pass              http://yy.yy.yy.yy:8080;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
    }

    location /script {
            proxy_pass              http://www.yet.another.domain.com/www.sub.domain.com/;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
    }

    location / {
            proxy_pass              http://yy.yy.yy.yy:8080;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;

            rewrite ^/int/a.([a-zA-Z]+).([0-9]+)$ /site/int/a?l=$1&i=$2 last;
            rewrite ^(.*)$ /site/$1 last;
    }

}

On accessing www.sub.domain.com/script, this happens:

curl -I www.sub.domain.com/script HTTP/1.1 301 Moved Permanently Server: nginx/1.6.0 Date: Mon, 04 Aug 2014 04:12:23 GMT Content-Type: text/html Content-Length: 184 Location: http://www.sub.domain.com/script/ Connection: keep-alive

In addition, a 404 error is shown: "Not Found: The requested URL /www.sub.domain.com/ was not found on this server."

Where's my mistake? I can't spot it. (Or is it a bunch of mistakes? I'm quite new to nginx redirects.)

Thanks a lot!

share|improve this question
    
The second location should be location /script, not location /new_script. –  Antonis Christofides Aug 4 at 5:49
    
Correct. I made a mistake when anonymising the configuration. Thanks! I edited the line. –  user2092982 Aug 4 at 11:21
    
I'm still not confident you have transcripted it correctly. Maybe you should try a real minimal configuration that reproduces the problem and copy/paste it here. I also think that you are confusing the terminology: proxy passing and redirecting are different things. –  Antonis Christofides Aug 4 at 13:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.