
Imgproxy is an open-source image-processing application that lets you process, convert, and resize images using URL requests. Imgproxy acts as an image processor that enhances source images with better resolutions and settings. It's compatible with modern web applications that may require standardized image formats or sizes to improve performance and user experience.
Imgproxy integrates with services such as CDNs, load balancers, and proxy servers to cache and deliver images on your web applications as an intermediate service. This article explains how to deploy ImgProxy on a Ubuntu server to process, resize, and convert images on the fly.
Before you begin:
imgproxy.example.com.ImgProxy uses the libvips image processing library to process and deliver images from source files on your server. You can install ImgProxy using Docker or build the application from source using Go. Follow the steps below to install ImgProxy using the application source code and enable all necessary dependencies to optimize your server performance.
Add the libvips package to your APT repository sources using the dhor/myway PPA.
Update the server package index.
Install the libvips dependency package.
Install the latest Go version using Snap.
Clone the official ImgProxy GitHub repository using Git.
The above command creates a new imgproxy directory on your server with all necessary application files. When the process is successful, your output should appear like the one below.
Switch to the imgproxy directory.
Build and compile the ImgProxy binary using Go.
The above command compiles a new ImgProxy binary on your server using the "-s|-w" linker flags that optimize the binary file using Go. The go build process outputs the resultant binary file to the /usr/local/bin/ directory to enable system-wide access. When the build process is complete, your output should be similar to the one below.
Create a new ImgProxy environment configuration file in the /usr/local/bin directory.
Open the file using a text editor like Nano.
Add the following environment configurations to the file.
Save and close the file.
The above ImgProxy environment configuration enables the application to run on your server with specific runtime options. Within the configuration:
IMGPROXY_BIND: Enables ImgProxy to run on a specific port on your server. By default, ImgProxy uses the TCP port 8080.IMGPROXY_NETWORK: Sets the ImgProxy network protocol to use with the port.IMGPROXY_READ_TIMEOUT: Sets the maximum duration in seconds to read source image requests.IMGPROXY_WRITE_TIMEOUT: The maximum duration in seconds to write a response including the body.IMGPROXY_WORKERS: The maximum number of images ImgProxy can process on your server. The value is calculated using the number of CPU cores * 2. For example, if your server has 4 vCPUs, set the maximum workers value to 8.IMGPROXY_REQUESTS_QUEUE_SIZE: The maximum number of image processing requests to allow in a queue. 0 sets the queue size to unlimited.IMGPROXY_QUALITY: Sets the maximum processed image quality.IMGPROXY_PREFERRED_FORMATS: Sets the default image formats to convert images on your server.IMGPROXY_LOG_FORMAT: Sets the ImgProxy log messages format.IMGPROXY_LOG_LEVEL: Enables the type of ImgProxy log messages.IMGPROXY_WATERMARK: Enables watermarking of processed images on the server. The URL https://www.vultr.com/_images/og/default.pnghttps://www.vultr.com/_images/og/default.png loads a Vultr promotional banner to use as the watermark file in this article while the opacity value 1 enables watermarking on the server.Create a new IMGPROXY_ENV_LOCAL_FILE_PATH environment variable to set the location of your ImgProxy configuration.
ImgProxy uses the above variable to read all environment configuration options while running on the server.
Switch to your user home directory.
Run the imgproxy command to verify that ImgProxy is available on your server.
Output:
Press Ctrl + C to cancel the ImgProxy process and configure your server to run the application as a system service.
ImgProxy is installed and available in the /usr/local/bin/ directory on your server. You can run the imgproxy command to start the application, but all changes are lost when the server session ends. Follow the steps below to set up ImgProxy as a system service to manage the application processes and monitor events such as error logs on your server.
Create a new ImgProxy system user account without a home directory, login or shell privileges.
Grant the user full privileges to the ImgProxy binary file /usr/local/bin/imgproxy.
Grant the user ownership privileges to the environment configuration file /usr/local/bin/imgproxy.env.
Create a new systemd unit file.
Add the following configurations to the file.
Save and close the file.
The above configuration creates a new ImgProxy service owned by the imgproxy user and group you created earlier. The service uses the IMGPROXY_ENV_LOCAL_FILE_PATH= variable to set your ImgProxy environment configuration. In addition, all application logs are written to the /var/log/imgproxy/ directory using the human-readable format pretty defined in your environment configuration.
Create the ImgProxy application logs directory.
Grant the ImgProxy user full privileges to the directory.
Restart the systemd daemon to enable the new ImgProxy service on your server.
Enable ImgProxy to start at boot time.
Start the ImgProxy system service.
View the ImgProxy system service status and verify that it's actively running on your server.
Your output should be similar to the one below.
ImgProxy runs on the server port 8080 defined in your environment configuration. Install and configure Nginx as a reverse proxy to enable secure access to the ImgProxy without exposing the main application port on your server. Follow the steps below to create a new Nginx reverse proxy configuration to accept connections using your domain name and forward all requests to the backend ImgProxy application port.
Install Nginx on your server.
Create a new Nginx configuration file. For example, imgproxy.example.com.
Add the following configurations to the file.
Save and close the file.
The above Nginx configuration creates a new reverse proxy connection to the ImgProxy application port 8080 and accepts HTTP connections using your domain imgproxy.example.com.
Activate the configuration file.
Test Nginx for configuration errors.
Restart Nginx to apply the new configuration changes.
Allow the HTTP port 80 through the default UFW firewall.
Restart the firewall to apply the new changes.
ImgProxy supports extended security features such as URL signing to protect and limit requests on your server. Generate trusted SSL certificates to encrypt connections between all requesting clients and the ImgProxy server. Follow the steps below to install the Certbot Let's Encrypt client and generate new SSL certificates to use on the server.
Install the Certbot plugin for Nginx.
Generate new Let's Encrypt SSL certificates using your domain name. Replace imgproxy.example.com with your actual domain.
Restart Nginx to apply the configuration changes.
Allow the HTTPS port 443 through the firewall.
Restart UFW to apply the configuration changes.
ImgProxy accepts source images using HTTP GET requests and delivers processed images as a response. The application uses the following URL request format to load and process images on your server.
ImgProxy supports plain, encoded, and encrypted source URL formats as valid request types. Follow the steps below to process sample source images using the plain URL format on your server.
Access your ImgProxy domain using a web browser such as Firefox.
Verify that the default ImgProxy web page displays in your browser.
In a new browser tab, load a sample source image using the plain URL format. For example, load a sample image of a train from Pexels using the direct URL: https://images.pexels.com/photos/72594/japan-train-railroad-railway-72594.jpeg
The above URL loads the source image using the insecure plain URL format. It’s recommended to use encrypted formats when working with single images to tighten your server security. Use the plain format when working with multiple image files to simultaneously process requests on your server.
Press Enter to load the web page and verify that ImgProxy displays the image with your default processing settings.
Modify the URL with a new image processing value to resize the image to a resolution such as 800*800.
The above rs resize option modifies the source image with a fill type to keep the original image aspect ratio while resizing the image to the new 800*800 resolution.
Press Enter to view the processed image and verify that the image is resized to the new resolution.
Right-click the resized image and select Inspect from the list of browser options to convert the image to a new format.
Navigate to the Network tab and reload your ImgProxy URL to view the request details.
Verify that the processed image name is available in the Name column. Then, click the image name to view the ImgProxy request data.
Find Content-Type in the Response Headers section and verify the image file format depending on your source image. For example, image/jpeg.
Keep the Network tab open and modify your ImgProxy URL request to include a new image format using the @ option. For example, @webp to convert and process the image to the Webp format.
Monitor the processing data in your Network tab and verify that the image displays in your browser.
Click the image name in your Network tab and verify that the Content-Type field in your Response Headers changes to your new format. For example, /image/webp.
Right-click the image and select Save Image As from your browser options to verify that you can save the new processed image format.
Modify your ImgProxy URL again and include the /watermark:1:ce:2:2/ processing option to test the ImgProxy watermarking option on your server.
The above URL request watermarks the processed images using the source watermark URL in your ImgProxy configuration. The value 1 enables the watermark while ce positions the image to the center, and 2:2 sets the x|y offsets.
Press Enter to view the new watermarked image in your browser.
ImgProxy offers multiple processing options to modify source images on your server. Visit the ImgProxy processing options documentation to view all supported functions.
ImgProxy protects your server using key/salt pairs to enable URL signing on all server requests. When a valid signature is not attached to an ImgProxy URL request, it’s not processed by the server which safeguards your server from unpermitted client requests. Follow the steps below to create a new key/salt pair and generate a URL signature to protect your ImgProxy server requests.
Generate a new random 32-byte key using OpenSSL.
Output:
Generate a new 16-byte salt to use with the key.
Output:
Open the ImgProxy environment configuration file.
Add the following key/salt pair configurations at the end of the file. Replace the random hex-encoded values with your generated values.
Save and close the file.
The above key/salt pair enables ImgProxy URL signing on every server request.
Create a new bash script to generate a valid signature using your key/salt pair.
Add the following configurations to the file.
Save and close the file.
The above script calculates a new HMAC digest using SHA256 and your salt value. In the above configuration, a new signature is generated for the source image URL https://images.pexels.com/photos/72594/japan-train-railroad-railway-72594.jpeg.
Run the script to generate a new signed ImgProxy URL.
Output:
Copy the generated signed URL to use with your ImgProxy requests.
Restart ImgProxy to apply your new configuration changes.
Access the generated ImgProxy URL in your web browser to securely access your processed image.
Verify that ImgProxy displays the processed image in your web browser.
You have successfully protected ImgProxy with URL signing on your server. However, you cannot use the same signature with multiple source URLs.
Create a new ImgProxy request using your valid signature with a different processing option such as blur to test the access limits.
Verify that ImgProxy returns a 439 error with a Forbidden message and the following log entry.
Add the signature to your ImgProxy environment configuration to enable access and usage with multiple source image URLs.
Add your trusted signature using the IMGPROXY_TRUSTED_SIGNATURES option at the end of the file. Separate multiple signatures with,.
Save and close the file.
Restart ImgProxy to apply the new configuration changes.
Create a new ImgProxy request with a different source image URL such as https://i.imgur.com/iPsORz6.jpeg and convert it to a new format such as .png to test your new signature limits.
Verify that the processed ImgProxy image displays in your web browser.
ImgProxy accepts all requests on your server without limiting access to the image URL. A trusted signature enables ImgProxy to ignore the key/salt pair in your configuration and process images with any included options. This enables ImgProxy integration with dynamic web application frameworks such as WordPress that use multiple image names and requests.
ImgProxy supports multiple image sources including local files, URLs, and S3-compatible storage. Follow the steps below to configure ImgProxy to process images using Vultr Object Storage.
Create a new Vultr Object Storage bucket such as imgproxy-data.
Download the example image you applied earlier and upload it to your bucket using a tool such as S3 Browser. Then, save the image as image1.jpeg.
Open the ImgProxy configuration file.
Add the following configuration to enable S3 storage sources on your server. Replace all example values with your actual Vultr Object Storage details.
Save and close the file.
The above configuration enables ImgProxy to process images from your imgproxy-data bucket. To access the processed images, you must replace your source URL format with the object storage s3:// URI format.
ImgProxy uses the following URL format to process images using a Vultr Object Storage bucket.
Create a new signed ImgProxy URL request using the image.jpeg file in your Vultr Object Storage bucket. Then, rotate it by 90 degrees and convert it to a new format such as png.
Verify that your processed image displays in your web browser.
If your web application uploads and serves image files using Vultr Object Storage, configure your web server to securely serve the processed files using ImgProxy to optimize your web application performance.
You have successfully deployed ImgProxy and delivered processed images on the fly on your server. ImgProxy is a processing tool that integrates with your web application to serve optimized images with next-generation formats such as .webp and .avif. Set up monitoring applications such as Prometheus and Grafana to enable continuous monitoring of image processing requests on your server. For more information, visit the ImgProxy documentation.
0 Comments
Be the first to comment and share your perspective with the community.