Introduction to Nginx
Main Functions: Reverse Proxy, Load Balancing, Static/Dynamic Separation
- Installlation and startup of Nginx
- Common commands for Nginx, navigate to /usr/local/nginx/sbin
./nginx -- start Nginx
./nginx -s stop -- stop Nginx
./nginx -s quit -- graceful shut down Nginx
./nginx -s reload -- reload configuration, useful after config file changes
ps -ef|grep nginx -- check running Nginx processes
- Overview of Nginx Features
Reverse Proxy: Handles requests directed to backend servers
Forward Proxy: Manages client-side requests
Different business needs can be addressed through customized routing rules
- Load Balancing Strategies
Typical methods include: Round Robin (common), Weighted Round Robin (common), IP Hash
- Configuration File Structure: nginx.conf
... # Global context
events { # Events context
...
}
http # HTTP context
{
... # HTTP global context
server # Server context
{
... # Server global context
location [PATTERN] # Location context
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... # HTTP global context
}
- Global Context: Directives that affect the entire Nginx instance. Includes settings like user/group, PID file path, log directory, inclusion of config files, and number of worker processes.
- Events Context: Controls network connections between Nginx and clients. Configures maximum connections per process, event handling model (e.g., epoll), and concurrent connection acceptance.
- HTTP Context: Contains most configurasions for HTTP functionalities and third-party modules. Includes file inclusions, MIME types, custom logging, sendfile usage, timeout values, and request limits per connection.
- Server Context: Defines virtual host parameters. Multiple servers can exist within one HTTP block.
- Location Context: Specifies how to handle different URL paths and related page responses.
Sample Configuration:
########### Each directive must end with a semicolon #########
#user administrator administrators; # User/group settings, default is nobody nobody.
#worker_processes 2; # Number of worker processes, default is 1
#pid /nginx/pid/nginx.pid; # Path to the PID file
error_log log/error.log debug; # Log path and level. Can be set globally, in http, or server blocks. Levels: debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; # Serialize network connections to prevent thundering herd, default is on
multi_accept on; # Allow a process to accept multiple connections, default is off
#use epoll; # Event model selection: select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; # Maximum connections per worker, default is 512
}
http {
include mime.types; # File extension to MIME type mapping
default_type application/octet-stream; # Default MIME type, default is text/plain
#access_log off; # Disable access logs
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; # Custom log format
access_log log/access.log myFormat; # 'combined' is the default format
sendfile on; # Enable sendfile transfer, default is off. Can be used in http, server, or location blocks.
sendfile_max_chunk 100k; # Max chunk size per transfer, default is 0 (no limit).
keepalive_timeout 65; # Connection timeout, default is 75s, configurable in http, server, or location blocks.
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; # Hot standby
}
error_page 404 https://www.baidu.com; # Error page redirection
server {
keepalive_requests 120; # Max requests per connection.
listen 4545; # Listening port
server_name 127.0.0.1; # Listening address
location ~*^.+$ { # URL filtering via regex, ~ case-sensitive, ~* case-insensitive.
#root path; # Root directory
#index vv.txt; # Default page
proxy_pass http://mysvr; # Forward request to defined server group
deny 127.0.0.1; # Deny specific IPs
allow 172.18.5.54; # Allow specific IPs
}
}
}