Web site not working on port redirection

cox123456a nginx-forum at forum.nginx.org
Fri May 10 11:57:14 UTC 2019


I've post this on SO but no solution after weeks, so I think here I have a
better chance as this is being critical to myself.

I have the following NGINX setup:

server {
  listen 80 default_server;

  root /var/www/serviceserver1;
  index index.html index.htm;      

  location /api {
        proxy_redirect          http://localhost:3001/  /api;
        proxy_pass_header       Server;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Scheme $scheme;
        proxy_set_header        Host $http_host;
        proxy_set_header        X-NginX-Proxy true;
        proxy_connect_timeout   5;
        proxy_read_timeout      240;
        proxy_intercept_errors  on;

        proxy_pass              http://localhost:3001;
    }

    location /graphql {
        proxy_redirect          http://localhost:3001/  /graphql;
        proxy_pass_header       Server;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Scheme $scheme;
        proxy_set_header        Host $http_host;
        proxy_set_header        X-NginX-Proxy true;
        proxy_connect_timeout   5;
        proxy_read_timeout      240;
        proxy_intercept_errors  on;

        proxy_pass              http://localhost:3001;
   }

  # Root route
  location = / {
    try_files $uri /landing/index.html;
  }

  # admin routes
  location /admin {
    try_files $uri $uri/ /admin/index.html;
  }

  # analytics routes
  location /analytics {
    try_files $uri $uri/ /analytics/index.html;
  }

  # landing routes
  location /landing {
    try_files $uri $uri/ /landing/index.html;
  }

  # Any other route default to landing
  location / {
    try_files $uri $uri/ /landing/index.html;
  }
}

landing, analytics, admin are my application modules, where I navigate using
window.location = "/admin" in example. All works fine in my LAN, all fine,
no problems.

Now I have setup a Virtual Server on my access point/router to access the
system from the internet. I'm not allowed on this IP to access port 80 from
the Internet, so I've redirected port 8000 to 80 in the Virtual Server.

Now, every time I navigate from the Internet, my pages are breaking. Let me
explain better:

Navigating inside a module works fine:
http://170.180.190.200:8000/admin => http://170.180.190.200:8000/admin
(FINE)
http://170.180.190.200:8000/admin/home =>
http://170.180.190.200:8000/admin/home (FINE)
http://170.180.190.200:8000/admin/page1 =>
http://170.180.190.200:8000/admin/page1 (FINE)

When I click to change the module (from admin to analytics, for example),
I'm loosing the port number:
http://170.180.190.200:8000/admin => http://170.180.190.200/analytics  (Page
breaks as the port is missing)

How can I configure NGINX to forcelly add the port number when I change the
module?

Inside the application, just before calling window.location:

console.log(windows.location);

ancestorOrigins: DOMStringList {length: 0}
assign: ƒ ()
hash: ""
host: "170.80.224.142:8000"
hostname: "170.80.224.142"
href: "http://170.80.224.142:8000/"
origin: "http://170.80.224.142:8000"
pathname: "/"
port: "8000"
protocol: "http:"
reload: ƒ reload()
replace: ƒ ()
search: ""
toString: ƒ toString()
valueOf: ƒ valueOf()
Symbol(Symbol.toPrimitive): undefined
__proto__: Location

Component that changes the module (ReactJS):

class NavButton extends Component {
    handleModuleNav = action => {
        let to = "/" + action;
        window.location = to;
    };

    render = () => {
        return (
          <div onClick={() => this.handleModuleNav("admin")}>
             GO TO ADMIN
          </div>
        )
    }
}


My router configuration (page 8000 to 80):

https://unix.stackexchange.com/questions/518085/accessing-nginx-behind-a-virtual-server-looses-port-number/518183?noredirect=1#5181833

Thanks for helping. This is being critical for my production site.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,284120,284120#msg-284120



More information about the nginx mailing list