Nginx is a powerful, flexible web server and reverse proxy server used to serve static content, manage HTTP requests, and balance loads among multiple servers. In this blog post, we'll walk through a detailed Nginx configuration that includes multiple server blocks, each tailored for different purposes. Let's break down the configuration and understand how each directive functions.
First Server Block: General HTTP Server
server {
listen 80;
root /opt/training;
index index.html;
location /thane {
root /opt/;
index index.html;
access_log /var/log/nginx/thane.log;
error_log /var/log/nginx/thane_error.log debug;
}
location /mumbai {
root /opt;
index index.html;
access_log off;
}
location /tutorial {
return 301 $scheme://builddevops.com;
}
}
Key Directives and Locations:
listen 80;
# This directive tells Nginx to listen for incoming HTTP requests on port 80.
root /opt/training;
# Sets the root directory for the server. Any requests that do not match a specific location block will be served from this directory.
index index.html;
# Specifies the default file to serve when a directory is requested.
Location Blocks:
location /thane
root /opt/;
# Overrides the root for this location to /opt/.
access_log /var/log/nginx/thane.log;
# Logs access attempts to a specific file.
error_log /var/log/nginx/thane_error.log debug;
# Logs errors to a specific file with a debug level.
location /mumbai
root /opt; # Uses the /opt directory as the root.
access_log off; # Disables access logging for this location.
location /tutorial
return 301 $scheme://builddevops.com; # Redirects all requests to https://builddevops.com with a 301 permanent redirect status.
Second Server Block: PHP Application Server
server {
listen 9090;
root /opt/staging;
index index.php;
error_page 404 /404.html;
location ~ \.php {
try_files $uri @error;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* /application1 {
root /opt/staging/;
index index.php index.html;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
}
}
Key Directives and Locations:
listen 9090;
# Listens for incoming requests on port 9090.
root /opt/staging;
# Sets the root directory for this server block to /opt/staging.
index index.php;
# Sets the default file to index.php.
error_page 404 /404.html;
# Customizes the error page for 404 errors.
PHP Handling Location:
location ~ .php
try_files $uri @error;
# Attempts to serve the requested file, if it doesn't exist, it tries the @error named location.
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
# Sets the script filename parameter for FastCGI.
fastcgi_param PATH_INFO $fastcgi_script_name; # Sets the path info parameter.
fastcgi_pass unix:/dev/shm/php-fpm.sock; # Passes PHP requests to the PHP-FPM socket.
fastcgi_index index.php; # Sets the default PHP file.
include fastcgi_params; # Includes additional FastCGI parameters.
Application1 Location:
location ~ /application1*
root /opt/staging/; # Sets the root directory for this location.
index index.php index.html; # Specifies default files.
fastcgi_pass unix:/dev/shm/php-fpm.sock; # Passes requests to the PHP-FPM socket.
Kommentare