nginx反向代理404s缓存静态内容

时间:2015-12-30 22:11:05

标签: caching nginx proxy

我正在尝试使用nginx作为ZNC webadmin的反向代理。当我不缓存任何内容时,它工作正常,因为访问日志显示

[30/Dec/2015:16:47:07 -0500] "GET /znc/ HTTP/2.0" 200 1283 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:08 -0500] "GET /znc/pub/dark-clouds.css HTTP/2.0" 200 1868 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:08 -0500] "GET /znc/skinfiles/_default_/global.css HTTP/2.0" 200 382 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:08 -0500] "GET /znc/pub/jquery-ui-sortable.1.11.4.min.css HTTP/2.0" 200 3609 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:08 -0500] "GET /znc/pub/selectize-0.12.1.css HTTP/2.0" 200 2373 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:08 -0500] "GET /znc/pub/jquery-1.11.2.min.js HTTP/2.0" 200 96159 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:08 -0500] "GET /znc/pub/jquery-ui-sortable.1.11.4.min.js HTTP/2.0" 200 38165 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:09 -0500] "GET /znc/pub/selectize-standalone-0.12.1.min.js HTTP/2.0" 200 42551 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:47:09 -0500] "GET /znc/pub/clouds-header.jpg HTTP/2.0" 200 31228 "https://herp.derp/znc/pub/dark-clouds.css" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"

这是设置。

server {
    listen                      443 ssl http2 default_server;
    listen                      [::]:443 ssl http2 default_server;
    server_name                 herp.derp;
    index                       index.html;
    root                        /home/derp/www/;
    access_log                  /var/log/nginx/access.log;

    # ZNC webadmin
    location /znc/ {
        proxy_set_header        X-Forwarded-For proxy_add_x_forwarded_for;
        proxy_pass              http://[::1]:7777;
    }
}

但是,如果我尝试在我的网站上缓存静态内容,那么https://herp.derp/znc/上除静态内容之外的所有内容都可以正常工作。

这是设置

server {
    listen                      443 ssl http2 default_server;
    listen                      [::]:443 ssl http2 default_server;
    server_name                 herp.derp;
    index                       index.html;
    root                        /home/derp/www/;
    access_log                  /var/log/nginx/access.log;

    # ZNC webadmin
    location /znc/ {
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://[::1]:7777;
    }

    # Client Caching
    location ~* \.(?:jpg|jpeg|png|gif|ico|css|js|ttf)$ {
        expires                 1y;
        add_header              Cache-Control public;
    }
}

这些是与上面完全相同的文件,现在抛出404s

[30/Dec/2015:16:35:31 -0500] "GET /znc/ HTTP/2.0" 200 1283 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"  
[30/Dec/2015:16:35:31 -0500] "GET /znc/pub/dark-clouds.css HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"  
[30/Dec/2015:16:35:31 -0500] "GET /znc/skinfiles/_default_/global.css HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"  
[30/Dec/2015:16:35:32 -0500] "GET /znc/pub/jquery-ui-sortable.1.11.4.min.css HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"  
[30/Dec/2015:16:35:32 -0500] "GET /znc/pub/selectize-0.12.1.css HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"  
[30/Dec/2015:16:35:32 -0500] "GET /znc/pub/jquery-1.11.2.min.js HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:35:32 -0500] "GET /znc/pub/jquery-ui-sortable.1.11.4.min.js HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:35:32 -0500] "GET /znc/pub/selectize-standalone-0.12.1.min.js HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"
[30/Dec/2015:16:35:32 -0500] "GET /znc/favicon.ico HTTP/2.0" 404 640 "https://herp.derp/znc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36"

我做错了什么?我如何完成我想做的事?

2 个答案:

答案 0 :(得分:1)

您不想继续将静态内容请求代理到后端吗?我认为您希望在两个位置都拥有相同的代理传递信息

答案 1 :(得分:1)

我设法通过复制/ znc / location块中的正则表达式位置块并传递相同的代理指令来解决我的问题。显然是nginx only matches one location block per request

server {
    listen                      443 ssl http2 default_server;
    listen                      [::]:443 ssl http2 default_server;
    server_name                 herp.derp;
    index                       index.html;
    root                        /home/derp/www/;
    access_log                  /var/log/nginx/access.log;


    # ZNC webadmin
    location /znc/ {
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://[::1]:7777;

        # Client Caching
        location ~* \.(?:jpg|jpeg|png|gif|ico|css|js|ttf)$ {
            expires             1y;
            add_header          Cache-Control public;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass          http://[::1]:7777;
        }
    }

    # Client Caching
    location ~* \.(?:jpg|jpeg|png|gif|ico|css|js|ttf)$ {
        expires                 1y;
        add_header              Cache-Control public;
    }
}