Koe*_*err 3 linux ubuntu proxy reverse-proxy nginx
我想缓存*.htmlNginx反向代理中的所有文件,所以我添加了配置:
# 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;
}
Run Code Online (Sandbox Code Playgroud)
重复两次proxy_pass并proxy_set_header感觉不好
我该如何优化呢?谢谢!
嵌套位置是使用正则表达式创建位置的正确方法,它应该可以实现您想要实现的目标.
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;
location ~* \.html$ {
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid any 1m;
expires 1m;
}
}
Run Code Online (Sandbox Code Playgroud)
我不完全确定嵌套位置是否真的使用外部位置块中的选项.如果没有(我现在无法测试),您可以创建单独的文件.
location / {
include proxy.conf;
location ~* \.html$ {
include proxy.conf;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid any 1m;
expires 1m;
}
}
Run Code Online (Sandbox Code Playgroud)
proxy.conf 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;
Run Code Online (Sandbox Code Playgroud)
这肯定会起作用.