Nginx 问题求解!! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MaiCong
V2EX    NGINX

Nginx 问题求解!!

  •  
  •   MaiCong
    maicong 2015-05-28 14:59:58 +08:00 3850 次点击
    这是一个创建于 3867 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我通过 nginx 的 $upstream_http_content_type 判断类型是否是图片,代码如下:

    add_header X-Content-Type $upstream_http_content_type; if ($upstream_http_content_type ~* ^image/(jpg|jpeg|pjpeg|png|x-png|gif|bmp|x-icon|webp|svg\+xml)) { return 301; } 

    可结果是 X-Content-Type 倒是有了,却没有 301 跳转?

    图片1

    理论上应该酱紫啊:

    图片2

    第 1 条附言    2015-05-28 15:43:32 +08:00
    呃,我是想判断所有类型是图片的请求都单独处理。。。

    完整代码:

    location ~ /url {
    if ($request_uri ~ /(http|https|ftp)://([^/]+)/(.+)) {
    set $proxyurl $1://$2/$3;
    set $proxyhost $2;
    set $proxyref $1://$2;
    }

    proxy_pass $proxyurl;
    proxy_pass_header Server;
    proxy_redirect off;
    proxy_set_header Accept-Encoding "";
    proxy_set_header Host $proxyhost;
    proxy_set_header Referer $proxyref;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_hide_header Content-Type;

    add_header X-Content-Type $upstream_http_content_type;

    if ($upstream_http_content_type ~* ^image/(jpg|jpeg|pjpeg|png|x-png|gif|bmp|x-icon|webp|svg\+xml)) {
    # 如果类型是图片,就?
    }

    error_page 301 302 400 403 404 500 502 503 504 = @check_header;
    }

    location @check_header {
    rewrite ^ /ErrorPages/404.html;
    }
    第 2 条附言    2015-05-28 15:45:37 +08:00
    少写了个,下面是修复的:

    location ~ /url {

    set $proxyurl "http://www.xxx.com";
    set $proxyhost "www.xxx.com";
    set $proxyref "http://www.xxx.com";

    if ($request_uri ~ /(http|https|ftp)://([^/]+)/(.+)) {
    set $proxyurl $1://$2/$3;
    set $proxyhost $2;
    set $proxyref $1://$2;
    }

    proxy_pass $proxyurl;
    proxy_pass_header Server;
    proxy_redirect off;
    proxy_set_header Accept-Encoding "";
    proxy_set_header Host $proxyhost;
    proxy_set_header Referer $proxyref;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_hide_header Content-Type;

    add_header X-Content-Type $upstream_http_content_type;

    if ($upstream_http_content_type ~* ^image/(jpg|jpeg|pjpeg|png|x-png|gif|bmp|x-icon|webp|svg\+xml)) {
    # 如果类型是图片,就?
    }

    error_page 301 302 400 403 404 500 502 503 504 = @check_header;
    }

    location @check_header {
    rewrite ^ /ErrorPages/404.html;
    }
    8 条回复    2015-05-28 21:01:21 +08:00
    rrfeng
        1
    rrfeng  
       2015-05-28 15:36:00 +08:00
    看不懂你想做什么……把图片请求全部 301?

    add_header 是给 response 增加 header,既然都 response 了还怎么 301.
    MaiCong
        2
    MaiCong  
    OP
       2015-05-28 15:46:18 +08:00
    @rrfeng = = 请看 附言。我是想对图片类型单独处理。
    lujiajing1126
        3
    lujiajing1126  
       2015-05-28 18:32:40 +08:00
    有CDN啊?
    wy315700
        4
    wy315700  
       2015-05-28 18:34:19 +08:00
    不是应该用

    rewrite ^/(.*)$ http://www.XXX.com/$1 permanent;

    Dreista
        5
    Dreista  
       2015-05-28 19:44:31 +08:00 via iPhone
    maicong.me 在手机端访问时顶栏高度是否可以调整,在 iPhone 4S 一类的小屏幕机型上不是很友好。
    MaiCong
        6
    MaiCong  
    OP
       2015-05-28 20:11:45 +08:00 via iPad
    @lujiajing1126
    @wy315700
    /url 后面是网址,然后就反代这个网址。如果是图片类型就单独处理,但是用$upstream_http_content_type 正则匹配无效啊,不执行。。。
    MaiCong
        7
    MaiCong  
    OP
       2015-05-28 20:12:54 +08:00 via iPad
    @Dreista 这个网站只是挂个页面。。。
    ryd994
        8
    ryd994  
       2015-05-28 21:01:21 +08:00 via Android   1
    @MaiCong 你这样不行的,if是在rewrite阶段就执行,而upstream变量至少要到content阶段后才有值。
    为什么不能用URL判断?对方是下载站?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3449 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 91ms UTC 05:04 PVG 13:04 LAX 21:04 JFK 00:04
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86