Nginx 将路径重定向到另一个本地主机应用程序

Nginx redirect a path to another localhost app

在 nginx 中是否可以有路径 /phoenix,映射到 localhost:9090

我的网络应用程序内部没有 /phoenix 路由,因此当用户转到 domain.tld/phoenix 时,它将转到 localhost:9090 而不是 localhost:9090/phoenix

upstream phoenix {
  server localhost:9090;
}

upstream homepage {
  server localhost:9191;
}

server {
  listen 80;
  listen [::]:80;

  server_name domain.tld;
  return 404;
}

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name domain.tld;

  ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/domain.tld/chain.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

  ssl_ecdh_curve secp384r1;
  ssl_session_cache shared:SSL:10m;
  ssl_stapling on;
  ssl_stapling_verify on;

  add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
  add_header X-Frame-Options DENY;
  add_header X-Content-Type-Options nosniff;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://homepage;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

  location /phoenix {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://phoenix;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    # asset delivery
    location ~* ^.+\.(css|cur|gif|gz|ico|jpg|jpeg|js|png|svg|woff|woff2)$ {
      root /home/user/app/priv/static;
      etag off;
      expires max;
      add_header Cache-Control public;
    }
  }
}

@RichardSmith 谢谢。所以我们确实需要最后的斜杠。

proxy_pass http://phoenix/;