Nginx 反代二级目录问题

2019-01-08 10:45:38 +08:00
 toyst

我想访问https://photo.abc.com代理到192.168.2.6/photo/上面,我在location加上/photo/可以通过https://photo.abc.com/photo/访问到,请教如何隐藏或 301 跳转,直接通过https://photo.abc.com访问呢? 谢谢! 这是代码

server
{
    listen 443 ssl http2;
    server_name photo.abc.com;
    ssl_certificate    /etc/default/cert.pem;
    ssl_certificate_key    /etc/default/privkey.pem;

    location /photo/ { 
    proxy_set_header Host $host:$proxy_port; 
    proxy_set_header X-Real-IP $remote_addr;  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
    proxy_pass https://192.168.2.6/photo; 
    }
}
3999 次点击
所在节点    NGINX
14 条回复
zh826256645
2019-01-08 10:48:58 +08:00
直接 location / {...;proxy_pass https://192.168.2.6/photo;}
toyst
2019-01-08 10:56:53 +08:00
@zh826256645 如果像您那样写,会报 400 Bad Request
zh826256645
2019-01-08 11:02:23 +08:00
https://192.168.2.6/photo 改为 https://192.168.2.6/photo/ 呢?
我这边是没有问题的
toyst
2019-01-08 11:05:34 +08:00
@zh826256645 我后端提示「无法加载数据」,只有像我题目里那样加上 /photo/访问就正常了,能用 301 处理一下吗?我对 nginx 不太熟悉,谢谢
bnlt
2019-01-08 11:16:00 +08:00
301 跳转

server {
...
return 301 https://photo.abc.com$request_uri
}

代理的话 @zh826256645 说的应该是对的,有可能是你们后端软件恰巧不支持被反代。
zh826256645
2019-01-08 11:16:55 +08:00
不太清楚 https://192.168.2.6/photo/ 上是什么东西
如果只是单纯的静态文件,你可以通过设置 nginx 指定文件夹来进行反向代理

location / {
alias 静态文件目录;
}

如果是动态路由,那我感觉使用 301 状态码可以会跟你预期的效果不符
bnlt
2019-01-08 11:18:02 +08:00
但是 301 跳不进内网
zh826256645
2019-01-08 11:21:16 +08:00
还是搞清楚为啥这样不能反代,首先你得弄清理代理成功了吗?如果成功了,在分析下一步原因吧

server
{
listen 443 ssl http2;
server_name photo.abc.com;
ssl_certificate /etc/default/cert.pem;
ssl_certificate_key /etc/default/privkey.pem;

location / {
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://192.168.2.6/photo/;
}
}
ldehai
2019-01-08 11:35:30 +08:00
```
server {
server_name photo.abc.com;

root /var/www/html/photo;
index index.html index.htm;

location / {
try_files $uri $uri/ /index.html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

server {
if ($host = photo.abc.com) {
return 301 https://$host$request_uri;
}

listen 80;
server_name photo.abc.com;
return 404;
}
digimoon
2019-01-08 11:51:52 +08:00
我怀疑 192.168.2.6/photo/这个本身会有跳转或者非相对连接,应该先正常访问一下看看会连到什么地方
举个例子,192.168.2.6/photo/是入口,但是里面有些数据是要连到 192.168.2.6/或者 192.168.2.6/abc/
SakuraKuma
2019-01-08 11:53:24 +08:00
rewrite
lazyfighter
2019-01-08 13:19:36 +08:00
photo 后面加 /
royzxq
2019-01-08 13:28:24 +08:00
location /photo { proxy_pass https://xxx.xx.xxx/photo }

或者

location /photo/ { proxy_pass https://xx/photo/ }
ucun
2019-01-08 13:44:00 +08:00
改一个明显的错误

把 proxy_pass https://192.168.2.6/photo; 改成 proxy_pass http://192.168.2.6/photo;

你之前不成功可能是证书问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/524891

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX