nginx load balancer rewrite to listen port

Alaa Alomari asked:

I have implemented load balancer using nginx as below:

upstream lb_units {
  server 127.0.0.1:88 weight=7 max_fails=3 fail_timeout=30s; # Reverse proxy to  BES1
  server 10.200.200.107 weight=1 max_fails=3 fail_timeout=30s; # Reverse proxy to  BES2
  server 10.200.200.94 weight=1 max_fails=3 fail_timeout=30s; # Reverse proxy to  BES2
}
server {
 listen 80; 
 server_name  mysite.com; 
 location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass         http://lb_units; 
    proxy_redirect off;
 }
}
#########FINALLY THE REAL SITE##############
server {
    listen   88 backlog=128 default_server;
    rewrite  ^/en/businesses$             /en/business permanent;
    location / {
        proxy_redirect    off;

     }

now whenever i try to browse /en/businesses page, it redirects me to port 88 (ie.: http://mysite.com:88/en/business)
How can i force nginx to keep on port 80 when it runs the rewrite rule (ie. to make rewrite to http://mysite.com/en/business)?

My answer:


Since your server block is listening on port 88 and you used a relative URL in your rewrite, nginx uses port 88 in the resulting URL.

To fix this, specify the complete URL. For example:

        rewrite  ^/en/businesses$             http://mysite.com/en/business permanent;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.