Nginx反向代理子目录时资源请求路径异常问题求助
问题
用Nginx反向代理Nextcloud和Syncthing,配置了/nextcloud和/syncthing的location规则,访问http://192.168.40.231:408/nextcloud能正常加载页面,但CSS等资源请求直接走http://192.168.40.231:408/style.css,未经过代理转发到容器;页面重定向也直接跳转到主机根路径,导致资源丢失、跳转错误。需要让所有资源请求和重定向自动带上对应服务的前缀(比如/nextcloud/style.css),且所有代理路径都实现该效果。
解决方案
1. 修改Nginx配置
核心是保留代理路径前缀转发,同时修正响应中的路径和重定向地址,确保页面资源链接自动带上服务前缀。修改后的nginx.conf如下:
user nginx; worker_processes 1; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; proxy_buffering off; proxy_cache off; proxy_http_version 1.1; # 通用代理头配置,传递客户端真实信息 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; server { listen 80; server_name nginx_container; location /nextcloud { # 保留前缀转发到容器对应路径 proxy_pass http://nextcloud_container:80/nextcloud; # 修正容器返回的重定向地址 proxy_redirect http://nextcloud_container:80/nextcloud/ /nextcloud/; proxy_redirect http://nextcloud_container:80/nextcloud /nextcloud; # 替换页面中资源的根路径,自动添加前缀 sub_filter 'href="/' 'href="/nextcloud/'; sub_filter 'src="/' 'src="/nextcloud/'; sub_filter_once off; } location /syncthing { proxy_pass http://syncthing_container:8384/; # 修正Syncthing的重定向地址 proxy_redirect http://syncthing_container:8384/ /syncthing/; proxy_redirect http://syncthing_container:8384 /syncthing; # 替换页面资源路径 sub_filter 'href="/' 'href="/syncthing/'; sub_filter 'src="/' 'src="/syncthing/'; sub_filter_once off; } } }
2. 调整应用容器配置
Nextcloud容器
需要让Nextcloud知道自己运行在/nextcloud路径下,在docker-compose.yml的nextcloud服务中添加环境变量:
environment: - NEXTCLOUD_TRUSTED_DOMAINS=192.168.40.231:408 - OVERWRITEHOST=192.168.40.231:408 - OVERWRITEPROTOCOL=http - OVERWRITEWEBROOT=/nextcloud
Syncthing容器
Syncthing需配置基础URL,在docker-compose.yml的syncthing服务中修改启动命令:
command: ["-home", "/var/syncthing", "-gui-address", "0.0.0.0:8384", "-gui-url", "/syncthing"]
修改后的完整docker-compose.yml
version: "3" services: nginx: image: nginx build: ./nginx container_name: nginx_container ports: - 408:80 networks: - primary_network volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf # 确保配置文件挂载生效 nextcloud: container_name: nextcloud_container image: nextcloud build: ./nextcloud networks: - primary_network environment: - NEXTCLOUD_TRUSTED_DOMAINS=192.168.40.231:408 - OVERWRITEHOST=192.168.40.231:408 - OVERWRITEPROTOCOL=http - OVERWRITEWEBROOT=/nextcloud syncthing: container_name: syncthing_container image: syncthing/syncthing build: ./syncthing ports: - 22000:22000/tcp # TCP文件传输 - 22000:22000/udp # QUIC文件传输 - 21027:21027/udp # 本地发现广播 restart: unless-stopped networks: - primary_network command: ["-home", "/var/syncthing", "-gui-address", "0.0.0.0:8384", "-gui-url", "/syncthing"] networks: primary_network: ipam: driver: default
关键说明
proxy_pass保留前缀转发,确保容器收到带前缀的路径请求;proxy_redirect修正容器返回的重定向地址,替换成外部可访问的带前缀路径;sub_filter批量替换页面中的资源根路径,避免资源请求走主机根目录;- 应用容器本身配置基础URL,让应用生成的链接自动带前缀,减少Nginx的替换工作,更稳定可靠。
内容的提问来源于stack exchange,提问作者JustScott
相关产品推荐
相关产品推荐

