Fixed an issue where Nginx reverse proxy cache does not work

Source: Internet
Author: User
Tags nginx reverse proxy

Yesterday tried to build the NuGet image server with Nginx, the mirror server requires two functions: 1) reverse proxy; 2) content cache.

Using Nginx as the reverse proxy, the configuration is very simple, just add a server setting with Proxy_pass in/etc/nginx/nginx.conf:

server {    listen       ;    Listen       [::]:80;    server_name the  host name of the mirror server;    Location/{        proxy_pass http://www.nuget.org;    }}

With the addition of caching, the configuration is slightly more complex.

The first thing to do is to create a folder to hold the cache files, such as/data/nuget-cache.

Mkdir/data/nuget-cache

Then add the Proxy_cache_path settings in the HTTP settings section of nginx.conf:

Proxy_cache_path/data/nuget-cache levels=1:2 keys_zone=nuget-cache:20m max_size=50g inactive=168h;

* Keys_zone refers to the cache space name.

* Max_size refers to the maximum amount of space a cache file can occupy.

* Inactive refers to how long a cached file will be deleted if it is not accessed for an extended period of time.

Then add the Proxy_cache and Proxy_cache_valid settings in the Server Settings section:

server {    listen       ;    Listen       [::]:80;    server_name the  host name of the mirror server;    Location/{        Proxy_pass http://www.nuget.org;        Proxy_cache Nuget-cache;        Proxy_cache_valid 168h;    }}

* Proxy_cache set is the value of Keys_zone in Proxy_cache_path.

* Proxy_cache_valid set the cache expiration time, such as 168 hours here expires.

After this set up to run Nginx, found that only a small part of the content is cached, most of the content can not be cached, such as the following URL of the response content can not be cached:

Http://www.myget.org/F/aspnetvnext/api/v2/FindPackagesById%28%29?id=%27System.Linq%27

Looking at the HTTP response header (HTTP headers), the following 2 HTTP headers were found:

cache-control:privateset-cookie:ai_session=ad829b6c509946098fa7f8e32fada661|2015-06-24t03:52:38.2032109+00:00| 2015-06-24t03:52:38.4219541+00:00; expires=wed, 24-jun-2015 04:22:38 GMT; path=/

The problem is that they are caused by the need to ignore them in nginx via the Proxy_ignore_headers setting, which is set up as follows:

server {    listen       ;    Listen       [::]:80;    server_name the  host name of the mirror server;    Location/{        Proxy_pass http://www.nuget.org;        Proxy_cache Nuget-cache;        Proxy_cache_valid 168h;        proxy_ignore_headers Set-cookie Cache-control;        Proxy_hide_header Cache-control;        Proxy_hide_header Set-cookie;    }}

Resources

Understanding the nginx proxy_cache_path directive

Nginx Caching

Fixed an issue where Nginx reverse proxy cache does not work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.