Ubuntu VPS上Pocketbase无法通过Nginx反向代理正常运行
解决方案
问题根源
你的404和CSP错误来自两个核心问题:
- PocketBase管理面板的路径(
/_/开头)被Nginx转发到了前端的index.html,而非PocketBase服务 - PocketBase生成的管理面板页面中,API请求和静态资源路径未适配反向代理的
/api/前缀
步骤1:调整Nginx配置
更新你的Nginx配置,新增对PocketBase管理面板路径的代理规则,确保/_/开头的请求都转发到PocketBase服务:
server { listen 80; listen 443 ssl; server_name test.com; ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem; location / { try_files $uri $uri/ /index.html; root /var/www/html; index index.html; } # 代理PocketBase管理面板路径 location /_/ { proxy_set_header Connection ''; proxy_http_version 1.1; proxy_read_timeout 360s; 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_pass http://127.0.0.1:8090; } # 代理PocketBase API路径 location /api/ { proxy_set_header Connection ''; proxy_http_version 1.1; proxy_read_timeout 360s; 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_pass http://127.0.0.1:8090; } }
修改完成后重载Nginx配置:
sudo nginx -s reload
步骤2:设置PocketBase的PUBLIC_URL环境变量
启动PocketBase时,指定外部可访问的API前缀地址,确保管理面板生成的请求路径正确指向/api/:
PUBLIC_URL=https://test.com/api/ ./pocketbase serve
如果使用systemd托管PocketBase服务,在service文件中添加环境变量:
[Service] ... Environment="PUBLIC_URL=https://test.com/api/" ExecStart=/path/to/your/pocketbase serve ...
然后重启PocketBase服务:
sudo systemctl restart pocketbase
验证
访问https://test.com/_/即可正常打开PocketBase管理面板,API请求也会自动路由到/api/路径。
内容的提问来源于stack exchange,提问作者ts_97
相关产品推荐
相关产品推荐

