Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to cache all *.html files in a Nginx reverse proxy, So I added the config:

# 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;
}

Repeat twice proxy_pass and proxy_set_header feel bad

How can I optimize this? Thanks!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You could move this configuration

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;

into a separate file called proxy.conf and then include it in the right place

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.