Reverse proxy and cache server using nginx
Reverse cache server using nginx
Nginx can also be used as a cache server for serving cache static content. Doing this is also not that hard, but you need to be careful a bit of the configuration, especially about:
1 |
proxy_cache_key "$scheme$host$request_uri"; |
This is being used to read the exact hostname / subdomain / domain and requested URI for caching. It needs to have the $scheme variable in it if you use subdomain names also.
A default setting for this is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
http { # [...] # caching options proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=server-cache:8m max_size=1000m inactive=600m; proxy_temp_path /tmp/nginx; # [...] server { # [...] location / { # [...] proxy_cache_key "$scheme$host$request_uri"; proxy_cache server-cache; proxy_cache_valid 60m; proxy_cache_valid 404 1m; proxy_cache_bypass $arg_nocache; proxy_cache_bypass $http_pragma $http_authorization; # [...] } # [...] } } |
I only included what you need to add to your virtualhost configuration that you are already using, added […] to make you understand that it means some more configuration can exist there.
- proxy_cache_path and proxy_temp_path — are used to set default cache folder settings and temporary folder
- proxy_cache_key — this is important, do not forget to set this variable else content may be passed to other vhosts and that’s not good.
- proxy_cache — you pass here the name of your cache server
- proxy_cache_valid — set some validation on the cached files
- proxy_cache_bypass — set the bypass headers that will invalidate the cache and refresh it
Make sure though you have created the cache folders before you reload the configuration:
1 2 |
/var/cache/nginx /tmp/nginx |
And that’s about it for everything. A full configuration files for those who want only the code is bellow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { # caching options proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=server-cache:8m max_size=1000m inactive=600m; proxy_temp_path /tmp/nginx; include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; access_log off; error_log off; #access_log logs/host.access.log main; location / { proxy_pass http://192.168.1.2:8081/; proxy_cache_key "$scheme$host$request_uri"; proxy_cache server-cache; proxy_cache_valid 60m; proxy_cache_valid 404 1m; proxy_cache_bypass $arg_nocache; proxy_cache_bypass $http_pragma $http_authorization; proxy_redirect off; 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_max_temp_file_size 1m; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } } |
And that’s all for today, hope it helps someone and will see you again in our next article. Don’t forget to comment below and subscribe!
Cheers.
Page 1 ← Reverse cache server using nginx
Pages: 1 2