歡迎您光臨本站 註冊首頁

nginx反向代理怎麼解決圖片的問題?詳情見內

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

nginx反向代理怎麼解決圖片的問題?詳情見內

公司的需求是這樣的。
訪問者訪問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是這樣配置的:server
        {
                listen       80;
                server_name 1.a.b.com;
                index index.html index.htm index.php default.html default.htm default.php;

                location /
                        {
                                proxy_pass http://192.168.0.39:8088/pc1.html;
                                proxy_set_header Authorization "Basic aWRgc3li8jRpbxFtcYRU";
                                proxy_redirect off;
                        }

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

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

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

                log_format  1.a.b.com  '$remote_addr - $remote_user [$time_local] $request '
             '$status $body_bytes_sent $http_referer '
             '$http_user_agent $http_x_forwarded_for';
                access_log  /home/wwwlogs/1.a.b.com.log  1.a.b.com;
        }
現在的問題是頁面能打開,不過圖片全都顯示不出來。有沒有什麼辦法能解決這個問題呢?求各位大俠幫幫忙~謝謝了。
《解決方案》

《解決方案》

呃。。。這個是什麼情況?沒人知道么?:em14:
《解決方案》

location / 那裡寫的有問題把

把proxy_pass http://192.168.0.39:8088/pc1.html;
改為proxy_pass http://192.168.0.39:8088;
《解決方案》

location / 那裡寫的有問題把

把proxy_pass ;
改為proxy_pass ;
hlgao 發表於 2011-08-09 16:15 http://bbs.chinaunix.net/images/common/back.gif


   
呃。我知道這樣改可以。不過不符合我這邊的需求。這樣改相當於把整個主機映射到了某個域名下面。
我這邊的需求是訪問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也暴露給訪問者了。
《解決方案》

樓主把proxy放到一個判斷裡面測試下看看,比如,所有.htm(l)文件,全給proxy到你的那個頁面,別的proxy到http://192.168.0.39:8088.
沒測試過,樓主實驗下看行不行
《解決方案》

這個問題我後來用rewrite完成的,proxy_pass到http://192.168.0.39:8088下面。proxy_pass不支持在正則的後面。
另外,我是個新手,pc1.html中的所有圖片都是以pc1_xxx.png這樣的方式命名的。我怎樣能限制當rewrite到pc1.html的時候,只能返回pc1_xxx.png,直接訪問http://1.a.b.com/pc2_xxx.png的時候返回pc1_xxx.png或者403,這個要怎麼做呢?
《解決方案》

不知道為什麼要有這樣的思路,感覺很是奇怪~
不過要實現你的這個想法,你可以有條件的在location裡面proxy到對應的內網伺服器,而其他的文件你要返回什麼,看你是哪個伺服器提供的,直接403就可以了。
《解決方案》

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$我想問一下,你知道這句話是什麼意思么?

你可以借鑒,但是你首先要知道為什麼要這樣寫。

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

                 location /
                         {
                                 proxy_pass http://192.168.0.39:8088;
                                 proxy_set_header Authorization "Basic aWRgc3li8jRpbxFtcYRU";
                                 proxy_redirect off;
                         }

                 log_format  1.a.b.com  '$remote_addr - $remote_user [$time_local] $request '
              '$status $body_bytes_sent $http_referer '
              '$http_user_agent $http_x_forwarded_for';
                 access_log  /home/wwwlogs/1.a.b.com.log  1.a.b.com;
         }
《解決方案》

回復 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文件名就可以看到別人的流量圖了。
我貼一段我現在的配置估計你就能知道我什麼意思了。server
        {
                listen       80;
                server_name 1.a.b.com;
                index index.html index.htm index.php default.html default.htm default.php;

                location /
                        {
                                proxy_pass http://mrtg.a.b.com/Area1/Switch26/;
                                proxy_store on;
                                proxy_set_header Authorization "Basic xxxxxxxxxxxxx";
                                proxy_redirect off;
                                if ($request_filename !~ .(png)$)
                                {
                                        rewrite ^(.*)$ 192.168.2.26_gi0_2.html;
                                        break;
                                }
                                 if ($request_filename ~* .(png)$)
                                {
                                        rewrite ^(.*/)(.*)\.png$ $2.png;
                                        break;
                                }
                        }

                log_format  1.a.b.com  '$remote_addr - $remote_user [$time_local] $request '
             '$status $body_bytes_sent $http_referer '
             '$http_user_agent $http_x_forwarded_for';
                access_log  /home/wwwlogs/1.a.b.com.log  1.a.b.com;
        }我只想用戶訪問http://1.a.b.com/192.168.2.26_gi0_2_day.png返回正常,其他的PNG都返回404或者403。

[火星人 ] nginx反向代理怎麼解決圖片的問題?詳情見內已經有408次圍觀

http://coctec.com/docs/service/show-post-14148.html