Question

Understanding Nginx Location Blocks: Differences, Use Cases, and Examples

  • Posted on April 18, 2023• Last validated on April 18, 2023
  • Nginx
  • KFSysAsked by KFSys

I’ve noticed that many people have asked me about how location blocks in Nginx work, mainly because the Nginx documentation isn’t as clear as it could be on this topic. So, I decided to create a comprehensive guide to help clarify the differences between various types of location blocks, explain when to use each type, and provide examples for a better understanding of their practical applications. This guide will be especially useful for those who are new to Nginx or want to improve their web server configuration skills.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
April 18, 2023
Accepted Answer

In this post, we will discuss the differences between various types of location blocks, when to use them, and provide examples to illustrate their use.

Exact match:

The exact match location block is denoted by the “=” modifier. It is used to define an exact match between the requested URL and the location block. Nginx will only process this block if the URL matches the specified path exactly.

Example:

location = /login {
    proxy_pass http://backend/login;
}

In this example, Nginx will route requests for “/login” to the specified backend server. Requests for “/login/other” or “/login-example” will not be processed by this location block

Prefix match:

The prefix match location block is the most commonly used type and has no modifier. It matches the beginning of the requested URL and is ideal for routing requests based on URL structure.

Example:

location /api/ {
    proxy_pass http://backend-api/;
}

In this example, all requests with URLs starting with “/api/” will be routed to the specified backend server. This includes requests for “/api/users”, “/api/posts”, and so on.

Regular expression match:

Regular expression (regex) match location blocks use the “" or "*” modifiers for case-sensitive and case-insensitive matches, respectively. These blocks provide more flexibility when matching URLs, as they can match complex patterns.

Example:

location ~* \.(jpg|jpeg|png|gif)$ {
    root /var/www/images;
}

In this example, Nginx will serve image files with the specified extensions from the “/var/www/images” directory. The case-insensitive match ensures that both uppercase and lowercase extensions will be served.

Non-matching regular expression:

The non-matching regular expression location block uses the “!" or "!*” modifiers for case-sensitive and case-insensitive matches, respectively. This block is useful for excluding specific patterns from being processed by other location blocks.

Example:

location !~* \.(php|cgi)$ {
    deny all;
}

In this example, Nginx will deny requests for files with “.php” and “.cgi” extensions, regardless of case.

When to use what:

  • Use exact match location blocks for specific paths that need unique handling.
  • Use prefix match location blocks for routing requests based on URL structure or serving static files from specific directories.
  • Use regular expression match location blocks for complex URL patterns or when you need case-sensitive or case-insensitive matching.
  • Use non-matching regular expression location blocks to exclude specific URL patterns from being processed.

Conclusion:

Nginx location blocks provide powerful and flexible URL routing capabilities for your web server configuration. Understanding the differences between the various types of location blocks and their use cases is essential to ensure optimal performance and functionality. Utilize the appropriate location block based on your requirements to create an efficient and maintainable Nginx configuration.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.