Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting redirects to include the port used to access the service

I have a setup like this:

enter image description here

A user working with my Vagrant development environment accesses localhost:8080 on their host machine, which is forwarded into Vagrant to NGINX running at localhost:80 on the guest. Some requests are forwarded into my application server running at localhost:8080 on the guest, and some are static files served from NGINX.

A weird thing happens when I access my site. I have a login page which redirects on success, and the URL is rewritten from http://localhost:8080/login to http://localhost:80/login.

Here's my NGINX configuration for the site:

upstream appserver {
    server 127.0.0.1:8080;
}

upstream production {
    server www.mysite.com:443;
}

server {
    listen 80 default_server;
    server_name _;

    client_max_body_size 20M;

    access_log /var/log/nginx/project.access.log;
    error_log /var/log/nginx/project.error.log;

    index /index;

    location ~ ^(/js/testpage.js) {
        alias   /vagrant/artifacts/www/js/testpage.js;
    }

    location ~ ^(/test/js/app.js) {
        alias   /vagrant/test/js/app.js;
    }

    location ~ /test/js/app_router.js {
        alias   /vagrant/test/js/app_router.js;
    }

    location ~ /test/js/app_layout_controller.js {
        alias   /vagrant/test/js/app_layout_controller.js;
    }

    location ~ /test/js/apps/navbar/sections/layout/navbar_layout_controller.js {
        alias   /vagrant/test/js/apps/navbar/sections/layout/navbar_layout_controller.js;
    }

    location ~ /test/js/apps/navbar/sections/navbar/navbar_options_view.js {
        alias   /vagrant/test/js/apps/navbar/sections/navbar/navbar_options_view.js;
    }

    location ~ /test/js/apps/navbar/sections/navbar_all_views.js {
        alias   /vagrant/test/js/apps/navbar/sections/navbar_all_views.js;
    }

    location ~ ^/test/js/apps/(.*/testpage_.*\.js)$ {
        alias   /vagrant/test/js/apps/$1;
    }

    location ~ ^/test/js/(.*)$ {
        alias /vagrant/js/$1;
    }

    location ~ ^/build/js/(.*)$ {
        alias /vagrant/artifacts/www/js/$1;
    }

    location ~ ^/build/css/(.*)$ {
        alias /vagrant/artifacts/www/css/$1;
    }

    location ~ ^/(.*main.*)\.[@\-_\/\d\w]+\.(js|css)$ {
        alias /vagrant/$1.$2;
    }

    location ~ ^/(css|js|fonts|favicon.ico) {
        root   /vagrant;
    }

    location ~ ^/receipts/js/(.*)$ {
        alias /vagrant/receipts/js/$1;
    }

    location ~ ^/bower_components/(.*)$ {
        alias /vagrant/bower_components/$1;
    }

    location ~ ^/login-promo/ {
        access_log off;
        proxy_pass https://production;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location ~ ^/(admin|login|logout|index|build|testpage|receipts|open|reset|resetpage|privacy|change|activeUser|personPrivacyAcceptances) {
        access_log off;

        proxy_pass http://appserver;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

I'm not sure where the redirect is actually coming from, it could be coming from the app server backend or from the front end JavaScript. Is there a way that I can make sure that all redirects take the port used by the accessing client?


Update: Simply adding a <a href="/login">login</a> link to the root page and trying to navigate with that link redirects me to http://localhost:80/login rather than http://localhost:8080/login.

like image 758
Naftuli Kay Avatar asked Nov 10 '22 13:11

Naftuli Kay


1 Answers

This is probably an undesired side-effect of fowarding the guest's nginx port to a different one on the host.

See http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

Desactivating the proxy redirection in the last location block should do the trick:

proxy_redirect off;
like image 167
Nicolas Bazire Avatar answered Nov 15 '22 06:11

Nicolas Bazire