Imagine you have a website with a public_html directory containing your website's files. You can configure Nginx to serve content from this directory by creating a server block like this:
server {
listen 80; # Listen on port 80 (default HTTP port)
server_name yourdomain.com; # Replace with your domain name
location / {
root /var/www/html/public_html; # Path to your website files
index index.html index.htm; # Default files to serve
}
}
Reverse Proxy:
Let's say you have two web applications running on different ports on your server. Nginx can act as a reverse proxy, directing traffic to the appropriate application based on the URL. Here's an example:
server {
listen 80;
server_name yourdomain.com;
location /app1 {
proxy_pass http://localhost:3000; # Forward requests to app on port 3000
}
location /app2 {
proxy_pass http://localhost:5000; # Forward requests to app on port 5000
}
}
SSL/TLS Termination:
To enable HTTPS, you'll need an SSL certificate. Nginx can handle the encryption and decryption process. Here's a basic example (remember to replace the placeholders with your actual certificate and key files):
server {
listen 443 ssl; # Listen on port 443 (default HTTPS port)
server_name yourdomain.com;
ssl on;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
# Rest of your server block configuration
}
Basic Authentication:
Nginx can restrict access to specific directories using a username and password. Here's an example configuration for a directory named admin:
You'll need to create a separate file (htpasswd.users) containing usernames and encrypted passwords using the htpasswd tool.
URL Rewriting:
Nginx can rewrite URLs on the fly using regular expressions. For example, you can rewrite all requests for .php files to remove the extension:
rewrite ^/(.*)\.php$ /$1 permanent;
This will rewrite /about.php to /about while maintaining the correct behavior.
Load Balancing:
Imagine you have two web servers handling traffic for your website. Nginx can distribute incoming requests between them using a simple load balancing configuration:
upstream my_backend {
server server1.example.com:80;
server server2.example.com:80;
}
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://my_backend; # Forward requests to the upstream block
}
}
Caching:
Nginx can cache frequently accessed static content like images and CSS files. This reduces server load and improves website loading speed. Here's an example configuration:
location /images/ {
expires 30d; # Set cache expiration time to 30 days
proxy_cache_valid 30d; # Cache valid for 30 days
proxy_cache_revalidate after 1d; # Revalidate cache after 1 day
proxy_cache http_cache; # Enable caching
}
Logging and Monitoring:
Nginx can log access requests and errors for monitoring purposes. This allows you to analyze website traffic and identify potential issues. You can configure the level of detail logged in the http block of your Nginx configuration file.
Geolocation:
With additional modules, Nginx can identify a user's location based on their IP address. You can then potentially serve them region-specific content or language variations.
Multi-Site Management:
Nginx allows hosting multiple websites or applications on a single server using server blocks. Each server block defines a virtual host with its own domain name and document root. This allows efficient resource utilization.
Imagine you have a website with a
public_html
directory containing your website's files. You can configure Nginx to serve content from this directory by creating a server block like this:Let's say you have two web applications running on different ports on your server. Nginx can act as a reverse proxy, directing traffic to the appropriate application based on the URL. Here's an example:
To enable HTTPS, you'll need an SSL certificate. Nginx can handle the encryption and decryption process. Here's a basic example (remember to replace the placeholders with your actual certificate and key files):
Nginx can restrict access to specific directories using a username and password. Here's an example configuration for a directory named
admin
:You'll need to create a separate file (
htpasswd.users
) containing usernames and encrypted passwords using thehtpasswd
tool.Nginx can rewrite URLs on the fly using regular expressions. For example, you can rewrite all requests for
.php
files to remove the extension:This will rewrite
/about.php
to/about
while maintaining the correct behavior.Imagine you have two web servers handling traffic for your website. Nginx can distribute incoming requests between them using a simple load balancing configuration:
Nginx can cache frequently accessed static content like images and CSS files. This reduces server load and improves website loading speed. Here's an example configuration:
Nginx can log access requests and errors for monitoring purposes. This allows you to analyze website traffic and identify potential issues. You can configure the level of detail logged in the
http
block of your Nginx configuration file.With additional modules, Nginx can identify a user's location based on their IP address. You can then potentially serve them region-specific content or language variations.
Nginx allows hosting multiple websites or applications on a single server using server blocks. Each server block defines a virtual host with its own domain name and document root. This allows efficient resource utilization.