0

I have an app with endpoint http://localhost:3000/seller/ and I use nginx proxy_pass

server {
    include default_proxy_headers;
    listen 80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name domain.my;
    location /seller {
        proxy_buffering off;
        proxy_pass http://localhost:3000;
    }
    location /seller/api {
        rewrite    /seller/(.*) /$1 break;
        proxy_pass http://localhost:5000;
        proxy_buffering off;
    }
}

It works as I expected with https://domain.my/seller/ - thats OK.

If I'm trying to use https://domain.my/seller (without "/" slash at the end) it redirects to https://domain.my:3000/seller/.

How to disable redirecting to ":3000" port? I want it to work only as https://domain.my/seller/ (with / slash at the end).

Alex R.
  • 103
  • 3

1 Answers1

0

Ok. If I understand you correctly, take a look. What if you were to use upstream? The port numbers are moved inside each upstream allowing you to proxy_pass them by alias.

  upstream backend1 {

    # PHP-CGI GATEWAY 1 - listening on IPv4 & Port...
    server                  127.0.0.1:3000 weight=3 max_fails=1 fail_timeout=1s;

  } # END upstream backend1

  upstream backend2 {

    # PHP-CGI GATEWAY 2 - listening on IPv4 & Port...
    server                  127.0.0.1:5000 weight=3 max_fails=1 fail_timeout=1s;

  } # END upstream backend2

  server {
    listen                  80 default_server;
    listen                  [::]:80 default_server;
  
    server_name             *.example1.com;
    root                    /var/www/example1.com/public;
    index                   index.html;


    location /seller {

      proxy_pass                         http://backend1;
      proxy_http_version                 1.1;
      proxy_cache_bypass                 $http_upgrade;

      # Proxy headers
      proxy_set_header Upgrade           $http_upgrade;
      proxy_set_header Connection        "upgrade";
      proxy_set_header Host              $host;
      proxy_set_header X-Real-IP         $remote_addr;
      proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host  $host;
      proxy_set_header X-Forwarded-Port  $server_port;

      # Proxy timeouts
      proxy_connect_timeout              60s;
      proxy_send_timeout                 60s;
      proxy_read_timeout                 60s;

    }

    location /seller/api {

      proxy_pass                         http://backend2;
      proxy_http_version                 1.1;
      proxy_cache_bypass                 $http_upgrade;

      # Proxy headers
      proxy_set_header Upgrade           $http_upgrade;
      proxy_set_header Connection        "upgrade";
      proxy_set_header Host              $host;
      proxy_set_header X-Real-IP         $remote_addr;
      proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host  $host;
      proxy_set_header X-Forwarded-Port  $server_port;

      # Proxy timeouts
      proxy_connect_timeout              60s;
      proxy_send_timeout                 60s;
      proxy_read_timeout                 60s;

    }

  }
suchislife
  • 356
  • 4
  • 11
  • Actually, I'm using upstream and docker resolver `127.0.0.11 ipv6=off valid=10s;`. `set $upstream_prod_api http://prod_api:5000; set $upstream_prod_web http://prod_web:3000;`. And inside location it looks like this `proxy_pass $upstream_prod_web;` – Alex R. Jun 26 '20 at 21:38
  • I am personally... unfamiliar with Docker. But in NGINX, from many examples I have seen, the port number isn't generally passed in the `proxy_pass` or `fastcgi_pass` when using `upstream`; it is not its natural context. See https://nginxconfig.io - It's a tool Hosted on https://www.digitalocean.com/ to generate `nginx.conf` files that has a page on GitHub. You can clarify a lot of thoughts playing around with this amazing open source tool. – suchislife Jun 27 '20 at 00:09