乐闻世界logo
搜索文章和话题

How does Nginx handle dynamic content? What are the configuration methods?

2月21日 16:58

How does Nginx handle dynamic content? What are the configuration methods?

Nginx doesn't process dynamic content itself, but forwards requests to backend application servers through protocols like FastCGI, uWSGI, SCGI, etc.

FastCGI Configuration (PHP):

nginx
server { listen 80; server_name example.com; root /var/www/html; index index.php index.html; # PHP file handling location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_index index.php; # FastCGI parameters fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; include fastcgi_params; # Timeout settings fastcgi_connect_timeout 60s; fastcgi_send_timeout 60s; fastcgi_read_timeout 60s; # Buffer settings fastcgi_buffer_size 4k; fastcgi_buffers 8 4k; } location / { try_files $uri $uri/ /index.php?$query_string; } }

uWSGI Configuration (Python):

nginx
upstream django_backend { server unix:/var/run/uwsgi/app.sock; server 127.0.0.1:8000; } server { listen 80; server_name example.com; root /var/www/html; location / { uwsgi_pass django_backend; include uwsgi_params; # uWSGI parameters uwsgi_param UWSGI_SCHEME $scheme; uwsgi_param SERVER_NAME $server_name; uwsgi_param REMOTE_ADDR $remote_addr; # Timeout settings uwsgi_connect_timeout 60s; uwsgi_send_timeout 60s; uwsgi_read_timeout 60s; # Buffer settings uwsgi_buffering on; uwsgi_buffer_size 4k; uwsgi_buffers 8 4k; } # Static files location /static/ { alias /var/www/html/static/; expires 1y; add_header Cache-Control "public, immutable"; } }

SCGI Configuration (Ruby):

nginx
upstream rails_backend { server unix:/var/run/scgi/rails.sock; server 127.0.0.1:9000; } server { listen 80; server_name example.com; root /var/www/html; location / { scgi_pass rails_backend; include scgi_params; # SCGI parameters scgi_param SCGI 1; scgi_param SERVER_SOFTWARE nginx; # Timeout settings scgi_connect_timeout 60s; scgi_send_timeout 60s; scgi_read_timeout 60s; } }

HTTP Proxy Configuration:

nginx
upstream nodejs_backend { server 127.0.0.1:3000; server 127.0.0.1:3001; } server { listen 80; server_name example.com; location / { proxy_pass http://nodejs_backend; # Proxy header settings proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # Timeout settings proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; # Buffer settings proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; # WebSocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }

FastCGI Caching:

nginx
# Define FastCGI cache fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fastcgi_cache:10m max_size=1g inactive=60m; server { listen 80; server_name example.com; root /var/www/html; location ~ \.php$ { try_files $uri =404; # Enable FastCGI cache fastcgi_cache fastcgi_cache; fastcgi_cache_valid 200 60m; fastcgi_cache_valid 404 1m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; # Cache bypass conditions fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # Add cache status header add_header X-Cache-Status $upstream_cache_status; } }

Dynamic Content Load Balancing:

nginx
upstream php_backend { least_conn; server 192.168.1.100:9000 weight=3; server 192.168.1.101:9000 weight=2; server 192.168.1.102:9000 weight=1; keepalive 32; } server { listen 80; server_name example.com; root /var/www/html; location ~ \.php$ { fastcgi_pass php_backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

Dynamic Content Compression:

nginx
http { # Enable Gzip compression gzip on; gzip_vary on; gzip_min_length 1024; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml application/rss+xml; server { listen 80; server_name example.com; root /var/www/html; location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }

Dynamic Content Security:

nginx
server { listen 80; server_name example.com; root /var/www/html; # Deny access to sensitive files location ~* \.(htaccess|htpasswd|ini|log|sh|sql|bak|old|swp|tmp)$ { deny all; access_log off; } # PHP file handling location ~ \.php$ { # Prevent PHP file upload execution if ($request_filename ~* \.(jpg|jpeg|png|gif|ico)$) { return 403; } fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # Limit request size client_max_body_size 10m; } location / { try_files $uri $uri/ /index.php?$query_string; } }

Dynamic Content Monitoring:

nginx
# Custom log format log_format dynamic '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'rt=$request_time uct="$upstream_connect_time" ' 'uht="$upstream_header_time" urt="$upstream_response_time" ' 'cache=$upstream_cache_status'; server { listen 80; server_name example.com; root /var/www/html; access_log /var/log/nginx/dynamic.log dynamic; location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # Add performance headers add_header X-Response-Time $request_time; add_header X-Upstream-Time $upstream_response_time; } }

Complete Dynamic Content Configuration Example:

nginx
user nginx; worker_processes auto; http { # FastCGI cache fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fastcgi_cache:10m max_size=1g inactive=60m; # Gzip compression gzip on; gzip_vary on; gzip_min_length 1024; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml application/rss+xml; # Log format log_format dynamic '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'rt=$request_time uct="$upstream_connect_time" ' 'uht="$upstream_header_time" urt="$upstream_response_time" ' 'cache=$upstream_cache_status'; # PHP backend upstream php_backend { least_conn; server 192.168.1.100:9000; server 192.168.1.101:9000; keepalive 32; } server { listen 80; server_name example.com; root /var/www/html; index index.php index.html; access_log /var/log/nginx/example.com.access.log dynamic; error_log /var/log/nginx/example.com.error.log warn; # Deny access to sensitive files location ~* \.(htaccess|htpasswd|ini|log|sh|sql|bak|old|swp|tmp)$ { deny all; access_log off; } # Static resources location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2)$ { expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # PHP file handling location ~ \.php$ { try_files $uri =404; # Enable cache fastcgi_cache fastcgi_cache; fastcgi_cache_valid 200 60m; fastcgi_cache_valid 404 1m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_pass php_backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; # Timeout settings fastcgi_connect_timeout 60s; fastcgi_send_timeout 60s; fastcgi_read_timeout 60s; # Buffer settings fastcgi_buffer_size 4k; fastcgi_buffers 8 4k; # Add cache status header add_header X-Cache-Status $upstream_cache_status; add_header X-Response-Time $request_time; add_header X-Upstream-Time $upstream_response_time; } # Main routing location / { try_files $uri $uri/ /index.php?$query_string; } } }

Dynamic Content Handling Best Practices:

  1. Use caching: Enable FastCGI cache to reduce backend load
  2. Load balancing: Use upstream for load balancing
  3. Reasonable timeouts: Set timeout based on business requirements
  4. Buffer optimization: Adjust buffer size for better performance
  5. Compress responses: Enable Gzip compression to reduce data transfer
  6. Security configuration: Prevent file upload execution and sensitive file access
  7. Monitor performance: Log response time and cache hit rate
  8. Static separation: Separate static resources to independent paths
  9. Connection keepalive: Use keepalive to reduce connection overhead
  10. Error handling: Configure friendly error pages
标签:Nginx