Spring MVC “redirect:/” prefix redirects with port number included
nginx, redirect, spring, spring-mvc
Solution
use `$server_port` instead of `$proxy_port` part in you configuration.
change this line
proxy_set_header Host $host:$proxy_port;
to
proxy_set_header Host $host:$server_port;
Catalina's `HttpServletResponse.sendRedirect` implementation uses the `getServerPort` method to build an absolute redirect url (`Location` Header-Value). `getServerPort` returns the part after the `:` from the request's `Host`Header-Value which has to be 8085 in your case.
Problem
I am using tomcat and nginx together to serve my web application. nginx listens to port 8085 and forwards requests to tomcat which is running on port 8084. If I do a redirect like the following: ``` @RequestMapping("/test") public String test() { return "redirect:/"; } ``` the page gets redirected to port 8084 (Tomcat port) instead of nginx port(8085). How can i redirect to the desired port? Edit: My nginx configuration is similar to this: ``` server { listen 8085; server_name www.mydomain.com; location /{ proxy_pass http://127.0.0.1:8084; include /etc/nginx/conf.d/proxy.conf; } } ``` and contents of /etc/nginx/conf.d/proxy.conf: ``` proxy_redirect off; proxy_set_header Host $host:$proxy_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 8m; client_body_buffer_size 256k; proxy_connect_timeout 120; proxy_send_timeout 120; proxy_read_timeout 120; proxy_buffer_size 4k; proxy_buffers 32 256k; proxy_busy_buffers_size 512k; proxy_temp_file_write_size 256k; ```