Middle
How did you configure Nginx? What parameters did you use, such as allow, deny, redirects, conditions, and authentication?
sobes.tech AI
Answer from AI
Nginx was configured through configuration files, usually nginx.conf and files in the conf.d/ directory.
For access control, directives allow and deny were used:
// Allow access from all IP addresses
allow all;
// Deny access from a specific IP range
deny 192.168.1.0/24;
Redirects were configured using return:
// Permanent redirect
return 301 https://$host$request_uri;
Conditions were used with if:
// Redirect if the request is HTTP
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
Authorization was implemented through auth_basic and auth_basic_user_file:
// Enable basic authentication
auth_basic "Restricted Area";
// Specify the password file path
auth_basic_user_file /etc/nginx/.htpasswd;
The location directives were also actively used to define how to handle requests to different URLs:
location /static/ {
// Serve static files from this path
alias /var/www/app/static/;
}
location /api/ {
// Forward requests to the API backend
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
For performance optimization, caching was configured using proxy_cache_path and proxy_cache:
// Define cache zone
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
location /images/ {
// Use the defined cache zone
proxy_cache my_cache;
// Cache status logging
add_header X-Cache-Status $upstream_cache_status;
// Backend to fetch images from
proxy_pass http://image_server;
}