我该如何优化这个 nginx 代理缓存配置?

我该如何优化这个 nginx 代理缓存配置?

我想*.html在 Nginx 反向代理中缓存所有文件,因此我添加了配置:

# Original configuration
location = / {
    proxy_pass   http://192.168.12.12:91;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}

# Added for cache
location ~ \.html {
    proxy_pass   http://192.168.12.12:91;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_cache cache_one;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 301 302 1m;
    proxy_cache_valid any 1m;
    expires     1m;
}

重复两次proxy_passproxy_set_header感觉很糟糕

我该如何优化它?谢谢!

答案1

您可以移动此配置

proxy_pass              http://192.168.12.12:91;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

放到一个单独的文件中proxy.conf,然后将其包含在正确的位置

location = / {
    include proxy.conf;
}

# Added for cache
location ~ \.html {
    include proxy.conf;

    proxy_cache cache_one;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 301 302 1m;
    proxy_cache_valid any 1m;
    expires     1m;
}

相关内容