I am currently facing a small problem using nginx to redirect to another host. I want to for example redirect https://service.company.com/new/test.html to https://new-service.company.com/test.html .
For now I have following configuration, which redirects me to https://new-service.company.com/new/test.html .
server {
# SSL
ssl_certificate /etc/nginx/cert/chained_star_company.com.crt;
ssl_certificate_key /etc/nginx/cert/star_company.com.key;
listen 443;
server_name service.company.com;
location /new/$1 {
return 301 $scheme://service-new.company.com/$1;
}
}
I also tried following with the same result:
return 301 $scheme://service-new.company.com/$request_uri
You want to rewrite the URI and redirect. You can achieve it using location
and return
directives, but a rewrite
directive would be the simplest approach:
rewrite ^/new(.*)$ https://new-service.company.com$1 permanent;
See this document for more.
BTW, the problem with your location
block solution, was the regular expression capture, wasn't. Use:
location ~ ^/new(.*)$ {
return 301 https://new-service.company.com$1$is_args$args;
}
See this document for more.