Nginx配置改写:隐藏pgAdmin4的/pgadmin4/browser访问路径
解决Nginx代理pgAdmin4时隐藏/pgadmin4/browser路径的问题
当前你的Nginx配置会导致访问根域名时被重定向到带/pgadmin4/browser的路径,要实现仅用https://my-pgadmin.local访问,需调整代理规则和重定向处理,修改后的完整配置如下:
server { listen 80; return 301 https://$host$request_uri; } server { listen 443; server_name my-pgadmin.local; ssl_certificate /etc/ssl/certs/my-pgadmin.local.crt; ssl_certificate_key /etc/ssl/private/my-pgadmin.local.key; ssl on; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4; ssl_prefer_server_ciphers on; location / { # 告知pgAdmin4使用根路径作为脚本路径,避免生成带/pgadmin4的链接 proxy_set_header X-Script-Name ''; proxy_set_header X-Scheme $scheme; proxy_set_header Host $host; # 代理请求到本地pgAdmin4服务 proxy_pass http://localhost:7070/; # 将pgAdmin4返回的重定向地址中的/pgadmin4/browser替换为根路径 proxy_redirect https://$host/pgadmin4/browser/ https://$host/; proxy_redirect http://localhost:7070/pgadmin4/browser/ https://$host/; # 保留必要代理头,确保pgAdmin4能正确识别客户端信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
关键修改说明:
- 替换原
location /pgadmin4/为location /,直接将根路径请求代理到pgAdmin4服务 - 将
X-Script-Name设为空字符串,让pgAdmin4生成根路径的内部链接,而非带/pgadmin4的路径 - 新增
proxy_redirect规则,改写pgAdmin4返回的重定向地址,避免浏览器地址栏显示/pgadmin4/browser - 保留核心代理头信息,确保pgAdmin4正常获取请求来源和协议信息
修改完成后重启Nginx服务生效:
sudo systemctl restart nginx
此时访问https://my-pgadmin.local即可直接进入pgAdmin4界面,地址栏不会显示/pgadmin4/browser路径。
内容的提问来源于stack exchange,提问作者shaft01
相关产品推荐
相关产品推荐

