3

I have a vagrant vm running nginx on port 80. My host machine forwards port 8080 to the vagrant vm's port 80.

I need to rewrite a url with a 301 redirect, which works, but the port I use to access nginx through the tunnel (8080) is dropped and the redirect fails.

http://server.com:8080/blog/two

-becomes-

http://server.com/blog.php?article=two

- it should be -

http://server.com:8080/blog.php?article=two

example:

rewrite ^/blog/(.*)$ /blog.php?article=$1 last;

Thanks!

user3228313
  • 139
  • 1
  • 9
  • rewrite ^/blog/(.*)$ http://$http_host/blog.php?article=$1 last; [Whats the difference of host and http_host in nginx][1] [1]: http://stackoverflow.com/questions/15414810/whats-the-difference-of-host-and-http-host-in-nginx – user3228313 Jan 23 '14 at 17:32
  • if you don't want the port `8080` then why is nginx listening to port `8080` and not `80`, or am i understanding it wrong – Mohammad AbuShady Jan 23 '14 at 17:43

1 Answers1

0

Extract the original port number from the Host header field:

set $port '';

if ($http_host ~ :(\d+)$) {
    set $port :$1;
}

rewrite ^/blog/(.*)$ http://example.com$port/blog.php?article=$1;
Joó Ádám
  • 909
  • 1
  • 11
  • 22