Ubuntu Nginx Reverse Proxy and Cache

Step 1: Download and install

sudo apt-get install nginx

The default nginx configuration file "nginx.conf" is in /etc/nginx.

Step 2: Config

sudo vi /etc/nginx/nginx.conf 

You may find there are two configuration folders included.

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

Open the default enabled site configuration file

sudo vi /etc/nginx/sites-enabled/default

Add a new reverse proxy configuration

location /api/ {
    proxy_pass http://localhost:8080/qpe/;
}

Restart the nginx service.

service nginx restart

Open a browser and try http://localhost/api/some

Step 3: Config proxy_cache

Read this first: https://www.nginx.com/resources/admin-guide/content-caching/

Open nginx.conf

sudo vi /etc/nginx/nginx.conf

Add lines in HTTP context

proxy_cache_path /etc/nginx/cache/ keys_zone=qpe:64m max_size=256m;
proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
# cache 60 minutes when http status 200 or 302
proxy_cache_valid 200 302 60m;
# cache 1 minute when 404
proxy_cache_valid 404 1m;

Open "default" in sites-enabled folder

# begin, proxy to Tomcat
location /api/ {
  proxy_pass http://localhost:8080/qpe/;
  proxy_cache qpe;
}

Restart the nginx service

你可能感兴趣的:(Ubuntu Nginx Reverse Proxy and Cache)