免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 13961 | 回复: 9
打印 上一主题 下一主题

[proxy] nginx反向代理怎么解决图片的问题?详情见内 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-03 13:37 |只看该作者 |倒序浏览
公司的需求是这样的。
访问者访问URL为1.a.b.com时,看到的内容应该是http://192.168.0.39:8088/pc-1.html的内容。
访问者访问URL为2.a.b.com时,看到的内容应该是http://192.168.0.39:8088/pc-2.html的内容。
我现在利用NGINX是这样配置的:
  1. server
  2.         {
  3.                 listen       80;
  4.                 server_name 1.a.b.com;
  5.                 index index.html index.htm index.php default.html default.htm default.php;

  6.                 location /
  7.                         {
  8.                                 proxy_pass http://192.168.0.39:8088/pc1.html;
  9.                                 proxy_set_header Authorization "Basic aWRgc3li8jRpbxFtcYRU";
  10.                                 proxy_redirect off;
  11.                         }

  12.                 location ~ .*\.(php|php5)?$
  13.                         {
  14.                                 fastcgi_pass  unix:/tmp/php-cgi.sock;
  15.                                 fastcgi_index index.php;
  16.                                 include fcgi.conf;
  17.                         }

  18.                 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  19.                         {
  20.                                 expires      30d;
  21.                         }

  22.                 location ~ .*\.(js|css)?$
  23.                         {
  24.                                 expires      12h;
  25.                         }

  26.                 log_format  1.a.b.com  '$remote_addr - $remote_user [$time_local] $request '
  27.              '$status $body_bytes_sent $http_referer '
  28.              '$http_user_agent $http_x_forwarded_for';
  29.                 access_log  /home/wwwlogs/1.a.b.com.log  1.a.b.com;
  30.         }
复制代码
现在的问题是页面能打开,不过图片全都显示不出来。有没有什么办法能解决这个问题呢?求各位大侠帮帮忙~谢谢了。

论坛徽章:
0
2 [报告]
发表于 2011-08-04 10:30 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2011-08-09 15:38 |只看该作者
呃。。。这个是什么情况?没人知道么?

论坛徽章:
0
4 [报告]
发表于 2011-08-09 16:15 |只看该作者
location / 那里写的有问题把

把proxy_pass http://192.168.0.39:8088/pc1.html;
改为proxy_pass http://192.168.0.39:8088;

论坛徽章:
0
5 [报告]
发表于 2011-08-13 12:57 |只看该作者
location / 那里写的有问题把

把proxy_pass ;
改为proxy_pass ;
hlgao 发表于 2011-08-09 16:15



   
呃。我知道这样改可以。不过不符合我这边的需求。这样改相当于把整个主机映射到了某个域名下面。
我这边的需求是访问http://1.a.b.com返回的页面仅仅是http://192.168.0.39:8088/pc1.html的内容,当然也包括图片。
按照您提出的修改方案,我访问http://1.a.b.com/pc1.html才是我想要访问的页面,但是当我访问http://1.a.b.com/pc2.html的时候,http://192.168.0.39:8088/pc2.html也暴露给访问者了。

论坛徽章:
381
CU十二周年纪念徽章
日期:2014-01-04 22:46:58CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52CU大牛徽章
日期:2013-03-14 14:08:55CU大牛徽章
日期:2013-04-17 11:17:19CU大牛徽章
日期:2013-04-17 11:17:32CU大牛徽章
日期:2013-04-17 11:17:37CU大牛徽章
日期:2013-04-17 11:17:42CU大牛徽章
日期:2013-04-17 11:17:47CU大牛徽章
日期:2013-04-17 11:17:52CU大牛徽章
日期:2013-04-17 11:17:56
6 [报告]
发表于 2011-08-13 20:23 |只看该作者
楼主把proxy放到一个判断里面测试下看看,比如,所有.htm(l)文件,全给proxy到你的那个页面,别的proxy到http://192.168.0.39:8088.
没测试过,楼主实验下看行不行

论坛徽章:
0
7 [报告]
发表于 2011-08-22 03:31 |只看该作者
这个问题我后来用rewrite完成的,proxy_pass到http://192.168.0.39:8088下面。proxy_pass不支持在正则的后面。
另外,我是个新手,pc1.html中的所有图片都是以pc1_xxx.png这样的方式命名的。我怎样能限制当rewrite到pc1.html的时候,只能返回pc1_xxx.png,直接访问的时候返回pc1_xxx.png或者403,这个要怎么做呢?

论坛徽章:
0
8 [报告]
发表于 2011-08-22 09:10 |只看该作者
不知道为什么要有这样的思路,感觉很是奇怪~
不过要实现你的这个想法,你可以有条件的在location里面proxy到对应的内网服务器,而其他的文件你要返回什么,看你是哪个服务器提供的,直接403就可以了。

论坛徽章:
0
9 [报告]
发表于 2011-08-22 10:03 |只看该作者
  1. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
复制代码
我想问一下,你知道这句话是什么意思么?

你可以借鉴,但是你首先要知道为什么要这样写。

要是只是做代理使用:
  1. server
  2.          {
  3.                  listen       80;
  4.                  server_name 1.a.b.com;
  5.                  index index.html index.htm index.php default.html default.htm default.php;

  6.                  location /
  7.                          {
  8.                                  proxy_pass http://192.168.0.39:8088;
  9.                                  proxy_set_header Authorization "Basic aWRgc3li8jRpbxFtcYRU";
  10.                                  proxy_redirect off;
  11.                          }

  12.                  log_format  1.a.b.com  '$remote_addr - $remote_user [$time_local] $request '
  13.               '$status $body_bytes_sent $http_referer '
  14.               '$http_user_agent $http_x_forwarded_for';
  15.                  access_log  /home/wwwlogs/1.a.b.com.log  1.a.b.com;
  16.          }
复制代码

论坛徽章:
0
10 [报告]
发表于 2011-08-22 14:37 |只看该作者
回复 8# hlgao


   
思路是这样的,我们为客户提供流量图查看服务,现在有一套我们自用的MRTG,不想搭建另外一套了。直接用nginx来调用现有的这套MRTG里面的图反馈给用户,这也就不难理解为什么会出现Basic Auth了。但是同一交换机上还有其他的用户,当然要防止用户根据自己的流量图图片地址推断去看别人的流量图了。
我现在已经完成了除PNG以外任何的URI或Filename都返回到192.168.2.26_fa_0_1.html这样的页面了,但是用户可以根据自己看到的192.168.2.26_fa_0_1.html这个页面推断出其他用户的流量图图片文件,比如192.168.2.26_fa_0_2_day.png,用自己的子域名加上这个PNG文件名就可以看到别人的流量图了。
我贴一段我现在的配置估计你就能知道我什么意思了。
  1. server
  2.         {
  3.                 listen       80;
  4.                 server_name 1.a.b.com;
  5.                 index index.html index.htm index.php default.html default.htm default.php;

  6.                 location /
  7.                         {
  8.                                 proxy_pass http://mrtg.a.b.com/Area1/Switch26/;
  9.                                 proxy_store on;
  10.                                 proxy_set_header Authorization "Basic xxxxxxxxxxxxx";
  11.                                 proxy_redirect off;
  12.                                 if ($request_filename !~ .(png)$)
  13.                                 {
  14.                                         rewrite ^(.*)$ 192.168.2.26_gi0_2.html;
  15.                                         break;
  16.                                 }
  17.                                  if ($request_filename ~* .(png)$)
  18.                                 {
  19.                                         rewrite ^(.*/)(.*)\.png$ $2.png;
  20.                                         break;
  21.                                 }
  22.                         }

  23.                 log_format  1.a.b.com  '$remote_addr - $remote_user [$time_local] $request '
  24.              '$status $body_bytes_sent $http_referer '
  25.              '$http_user_agent $http_x_forwarded_for';
  26.                 access_log  /home/wwwlogs/1.a.b.com.log  1.a.b.com;
  27.         }
复制代码
我只想用户访问返回正常,其他的PNG都返回404或者403。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP