How Can I Have Same Rule for Two Locations in Nginx Config?
If you need to apply the same configuration rules to multiple locations in Nginx, you can do so by defining separate location
blocks and using common configuration directives within those blocks. Here’s how you can manage this efficiently:
Example: Applying Same Rules to Two Locations
Let’s assume you want to apply the same rules to two different locations, /location1
and /location2
. The rules include handling PHP files, setting cache control, and logging.
Here’s a step-by-step guide:
1. Open or Create the Nginx Configuration File
Locate your Nginx configuration file, typically found in /etc/nginx/sites-available/
or /etc/nginx/conf.d/
. For this example, let’s assume we’re editing /etc/nginx/sites-available/example.conf
.
sudo nano /etc/nginx/sites-available/example.conf
2. Define the Server Block and Locations
Here’s how you can set up the server
block to apply the same rules to /location1
and /location2
:
server {
listen 80;
server_name example.com;
# Root directory for default location
root /var/www/example;
index index.html index.htm;
# Default location
location / {
try_files $uri $uri/ =404;
}
# Common configuration for /location1 and /location2
location ~ ^/(location1|location2)/ {
# Root directory specific to each location
root /var/www/example;
# Handle PHP files (example for PHP-FPM)
location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Set cache control headers
expires 30d;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Handle static files
location ~* \\.(jpg|jpeg|png|gif|css|js)$ {
expires 30d;
access_log off;
}
}
}
3. Explanation
server
block: Defines the configuration for the specified domain.location ~ ^/(location1|location2)/
: A regular expression location block that matches both/location1
and/location2
.root /var/www/example;
: Sets the root directory for these locations. Adjust as necessary if different for each location.location ~ \\.php$ { ... }
: Handles PHP files, if applicable.expires 30d;
: Sets cache control headers for static files.location ~* \\.(jpg|jpeg|png|gif|css|js)$ { ... }
: Handles static files with specific cache control settings.
4. Save and Close the File
After making the necessary changes, save and close the configuration file.
5. Test Nginx Configuration
Before applying changes, test the configuration for syntax errors:
sudo nginx -t
6. Reload Nginx
Reload Nginx to apply the new configuration:
sudo systemctl reload nginx
Alternative Approach: Use a include
Directive
If you have complex or repeated configurations, you can define a common configuration in a separate file and include it in the location blocks.
1. Create a Common Configuration File
Create a file, e.g., /etc/nginx/snippets/common-location.conf
, with common configuration rules:
# /etc/nginx/snippets/common-location.conf
location ~ \\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
expires 30d;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location ~* \\.(jpg|jpeg|png|gif|css|js)$ {
expires 30d;
access_log off;
}
2. Include the Common File in Your Main Configuration
Modify your main configuration file to include the common file:
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# Include common configurations for /location1 and /location2
location /location1 {
include snippets/common-location.conf;
}
location /location2 {
include snippets/common-location.conf;
}
}
Summary
- Use Regular Expressions: For applying the same rules to multiple locations, use regex in the
location
block. - Create Common Configuration: For complex setups, use
include
to manage common configuration snippets. - Test and Reload: Always test your configuration and reload Nginx to apply changes.
By following these methods, you can efficiently manage configurations that apply to multiple locations in Nginx.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github