Reverse proxy and cache server using nginx
Why using Nginx web server?
Some of you will have this question, but others will feel this is the right thing to do. Well either way, I recommend using Nginx as a reverse proxy due to its scalability and performance. Nginx is being used to power up Cloudflare network, so this has been massively tested and used over a very big network. Not only that, but Nginx is lightweight and uses less memory for more requests. In this article I’m going to show you a quick way to use Nginx as a reverse proxy and cache server for static resources.
Setup the reverse proxy server
In my other article I’ve already shown you how to install Nginx, well based on the default settings there you can easily set a reverse proxy by using:
1 |
proxy_pass http://192.168.1.2:8081/; |
Where 192.168.1.2 is the backend web server which handles the requests.
The virtual host configuration would look similar to the below example:
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 |
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_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; } } |
A bit of explanation:
- listen — we make nginx listen on port 80 for requests
- access_log and error_log — I disabled these, you can use them only if you see issues
- proxy_pass — this is where you will forward requests from your reverse proxy, normally it should be the backend server, but you can use different reverse proxy server here too.
- proxy_set_header — set the correct http header for our backend servers to know the real IP address of the client browsing our websites
- proxy_max_temp_file_size — set the maximum amount of size that a file can take in our temp folder
The rest are some default proxy settings I have used which should remain as they are unless you know what you are doing.
That’s basically it for the reverse proxy configuration using nginx.
Page 2 → Reverse cache server using nginx
Pages: 1 2