大网数据广告活动
服务器运维问答

nginx reverse proxy rewriting locations

· 52CE 我爱测

ok, so i have a virtual machine http://10.0.8.247:80/phppgadmin/ running phppgadmin. I wanted to expose phppgadmin to the internet on admin.example.com, so I did server { listen 80; server_name admin.example.com; location / { pro…

问题原文

原帖链接 · Killua · CC BY-SA 3.0

ok, so i have a virtual machine http://10.0.8.247:80/phppgadmin/ running phppgadmin. I wanted to expose phppgadmin to the internet on admin.example.com, so I did

server {
  listen 80;
  server_name admin.example.com;
  location / {
    proxy_pass              http://10.0.8.247:80/;
  }
}

and this works as expected. going to admin.example.com/phppgadmin proxies the request to 10.0.8.247 and i get the response I want. Now, i wanted to change things a little: have the same response on admin.example.com/postgres. So I tried

server {
  listen 80;
  server_name admin.example.com;
  location /postgres/ {
    proxy_pass              http://10.0.8.247:80/phppgadmin/;
  }
}

but this keeps failing me. The browser indicates that its trying to connect to 10.0.8.247 which naturally wont work, since I dont expose the virtual machines directly to the internet. Anyone got a hint? Thanks in advance, P.

======================================

Solution floows (Thanks lots, cnst!)

server {
  listen 80;
  server_name admin.example.com;

  location /postgres/ {
    proxy_pass              http://10.0.8.247:80/phppgadmin/;
    proxy_redirect          default;
  }
}

works like charm. Also note that

proxy_redirect              default;

is the same ase

proxy_redirect             http://10.0.8.247:80/phppgadmin/ /postgres/;

已采纳解答

原帖链接 · cnst · CC BY-SA 3.0

The issue you're experiencing is controlled through the proxy_redirect directive.

If your config is exactly as described, then it means that nginx isn't automatically normalising :80 part of the proxy_pass for the proxy_redirect to work correctly (which wouldn't surprise me too much, because most people don't proxy_pass to port 80 anyways), in which case, you can try either removing :80, or adding an explicit proxy_redirect directive.

Otherwise, if you're using regular expressions or variables in your location or proxy_pass directives, then don't, or just add an explicit proxy_redirect, too.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。