如何配置Ngnix不缓存特定网址?

时间:2016-07-26 15:04:24

标签: nginx cache-control

我有一台Ngnix服务器作为前端缓存服务器,我想在特定网址上禁用缓存。

以下是Nginx上的配置:

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m  inactive=120m max_size=1000m;
proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";

server {
  listen       10.0.0.45:80 default_server;
  server_name  proxy2.jjd;
  include      /etc/nginx/default.d/*.conf;
  location / {

    client_max_body_size 20m;
    proxy_cache my_zone;
    proxy_cache_bypass  $http_cache_control;

    proxy_no_cache $http_pragma $http_authorization $cookie_nocache $arg_nocache;

    add_header      X-Proxy-Cache-NGINX $upstream_cache_status;
    add_header      X-Real-IP $remote_addr;
    add_header      Cache-Control "public";
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      X-Forwarded-Proto https;
    proxy_set_header      X-Forwarded-Port 443;
    proxy_set_header      Host $host;

    proxy_pass            http://127.0.0.1:8080;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_redirect        off;
  }
}

3 个答案:

答案 0 :(得分:0)

添加以下位置以避免出现网址:

location ^~ /your-url/ {
    add_header            X-Real-IP $remote_addr;

    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      X-Forwarded-Proto https;
    proxy_set_header      X-Forwarded-Port 443;
    proxy_set_header      Host $host;

    proxy_pass            http://127.0.0.1:8080;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_redirect        off;
}

它只是将此位置分配给代理,而没有为其启用缓存。

答案 1 :(得分:0)

据我所知,您只需要一个带有单个字符串 location 的嵌套 proxy_cache off; 来禁用嵌套 URL 的缓存。像这样:

location / {
    proxy_cache my_zone;
    proxy_cache_bypass  $http_cache_control;
    // other stuff related to proxying or other processing
    location /do/not/cache/this/url/ {
        proxy_cache off;
    }
}

答案 2 :(得分:-1)

您只需为禁用缓存指定位置do proxy_pass

location / will / not / cache {     proxy_pass http://127.0.0.1:8080;     ..set_header .. }