- Installing Nginx
yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data
wget http://nginx.org/download/nginx-1.22.0.tar.gz
useradd -M -s /sbin/nologin nginx
mkdir -p /opt/nginx
chown -R nginx:nginx /opt/nginx
tar xf nginx-1.22.0.tar.gz
cd nginx-1.22.0/
./configure --user=nginx --group=nginx --prefix=/opt/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module
make
make install
ln -s /opt/nginx-1.9.12/ /opt/nginx
- Setting Up Reverse Proxy
# cat nginx.conf
upstream backend_servers {
ip_hash; # IP session persistence
server 10.0.1.105:8080 weight=1 max_fails=3 fail_timeout=30s; # After 3 failures
server 10.0.1.106:8080 weight=2 max_fails=3 fail_timeout=30s; # No requests for 30s
}
location / {
root html;
index index.html index.htm;
proxy_pass http://backend_servers;
}
- TCP Proxy Configuration
Example 1:
# cat nginx.conf
stream {
upstream tcp_proxy_pool {
hash $remote_addr consistent; # Consistent hash algorithm for remote addresses
server 192.168.0:11:22;
}
server {
listen 2222;
proxy_connect_timeout 1s; # Backend connection timeout
proxy_timeout 10s; # Backend response timeout
proxy_pass tcp_proxy_pool;
}
}
Example 2:
# cat nginx.conf
server {
listen 3306 so_keepalive=on; # Enable TCP long connections
proxy_pass 172.17.0.3:3306;
proxy_connect_timeout 5s; # Connection establishment time
proxy_timeout 3600s; # Connection maintenance time
}
###so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]
###so_keepalive=30m::10;
###keepidle: Wait time, keepintvl: Sending interval, keepcnt: Number of sends
Other settings:
proxy_timeout; # Connection maintenance time without data, default 75s
proxy_connect_timeout; # Connection establishment timeout, not exceeding 75s
proxy_send_timeout; # Send timeout, default 60s
proxy_read_timeout; # Receive timeout, default 60s
# sysctl -a |grep tcp_keepalive
net.ipv4.tcp_keepalive_time = 1200 # Time after which TCP starts probing without data transmission
net.ipv4.tcp_keepalive_probes = 9 # Number of probes
net.ipv4.tcp_keepalive_intvl = 75 # Probe interval
- Performance Testing and Optimization
1. Static content testing:
# ab -n 2000 -c 2 http://www.example.com/test.html
ab # n(total requests), c(concurrency), k(enable keepalive)
Server Software: nginx/1.12.2 # Software name
Server Hostname: www.example.com # Domain name
Server Port: 80 # Port
Document Path: /test.html # Accessed page
Document Length: 228 bytes # Content length
Concurrency Level: 2 # Concurrency level
Time taken for tests: 1.777 seconds # Test duration
Complete requests: 2000 # Number of completed requests
Failed requests: 0 # Number of failed requests
Write errors: 0 # Number of write errors
Total transferred: 708000 bytes # Total data transferred (content and headers)
HTML transferred: 22800 bytes # HTML file data (test.html)
Requests per second: 1125.37 [#/sec] (mean) # Requests per second
Time per request: 1.777 [ms] (mean) # Time per request
Time per request: 0.889 [ms] (mean,all requests) # Time for server to complete one request
Transfer rate: 389.05 [Kbytes/sec] received # Transfer speed
2. Dynamic content testing:
# ab -n 20 -c 2 http://www.example.com/test.jsp
Document Path: /test.jsp # Accessed page
Document Length: 669 bytes # Content length
Concurrency Level: 2 # Concurrency level
Time taken for tests: 55.03 seconds # Test duration
Complete requests: 20 # Number of completed requests
Total transferred: 99600 bytes # Total data transferred (content and headers)
HTML transferred: 66900 bytes # HTML file data (test.html)
Requests per second: 1.36 [#/sec] (mean) # Requests per second
Time per request: 553.3 [ms] (mean) # Time per request
Time per request: 271.6 [ms] (mean,all requests) # Time for server to complete one request
Transfer rate: 1.1 [Kbytes/sec] received # Transfer speed
3. Optimization
# cat /etc/security/limits.conf
* hard nofile 1000000
* soft nofile 1000000
#ulimit -a
# cat nginx.conf
worker_processes 4; # Number of processes
worker_cpu_affinity auto; # Automatic CPU binding
worker_rlimit_nofile 655350; # Nginx file descriptor limit
worker_connections 10240; # Maximum connections per process
sendfile on; # Enable efficient transfer mode
keepalive_timeout 65; # Keepalive timeout
gzip on; # Enable gzip compression
# ps -eo pid,args,psr |grep [n]ginx # Check process CPU binding
- Reocvering Deleted Nginx Logs
Prerequisite: Nginx has not been restarted
1. Backup and delete the nginx error log
# cp error.log error.log-bak
# rm -f error.log
2. Find the nginx log directory
# ps -elf | grep nginx # Check PID
# cd /proc/[PID]/fd
# ls -la # View nginx log directory
3. Recover
# cat [file_descriptor] > error.log # Redirect to file, replace [file_descriptor] with actual number
- Web Content Types Explained
1. Static Web Pages
Common extensions:
Plain text: htm, html, xml, shtml, js, css
Images or data documents: jpg, gif, png, bmp, txt, doc, ppt
Video formats: mp4, swf, avi, wmv, flv
Characteristics:
1) Client-side parsing, higher efficiency
2) No backend database, cost-effective
3) URL addresses without special characters like && ? etc.
4) Better for search engine indexing
5) Easier for CDN caching
2. Dynamic Web Pages
Common extensions:
.asp, .aspx, .php, .jsp, .do, .cgi
Characteristics
1) Server-side parsing, slower
2) URL addresses contain special characters, such as && ? etc.
3) Backend database, content comes from database
4) Dynamic program files exist on disk, no physical files for content
5) Based on database technology, reduces website maintenance workload
6) Uses dynamic technology to implement user interaction, users can register, login, shop, post blogs, etc.
3. Pseudo-Static Web Pages
Using rewrite technology to disguise dynamic pages as static pages (URL rewriting)
Characteristics:
1) Facilitates search engine indexing, improves user experience and website traffic
2) Higher resource consumption, performance degradation
4. Summary:
Static pages are more efficient, parsed on client side
Dynamic pages consume more resources, parsed on server side
Performance: Static is better
Functionality: Dynamic is better
Maintenance: Dynamic is better
5. Website Metrics
IP: Number of unique IP addresses, public IP of a computer
PV: Number of times a website page is accessed, click count
UV: Number of times a client device accesses the website, unique visitors
Concurrency: Maximum number of connections a server can handle per unit time
- Nginx Variables and Parameters
$args # Parameter values in the request
$query_string # Same as $args
$arg_NAME # Value of NAME in GET request
$is_args # "?" if request has parameters, empty string otherwise
$uri # Current URI in the request (without parameters, which are in $args), can differ from the $request_uri passed by the browser, it can be modified through internal redirects or using the index directive, $uri does not include the hostname, e.g., "/foo/bar.html"
$document_uri # Same as $uri
$document_root # Document root or alias for the current request
$host # Priority: Hostname in HTTP request line > "HOST" request header field > Server name matching the request. Host header field in the request, if the host header in the request is unavailable, then the server name that handles the request
$hostname # Hostname
$https # "on" if SSL security mode is enabled, otherwise empty string
$binary_remote_addr # Binary form of client address, fixed length of 4 bytes
$body_bytes_sent # Number of bytes transferred to the client, response headers not included; this variable is compatible with the "%B" parameter in Apache's mod_log_config module
$bytes_sent # Number of bytes transferred to the client
$connection # TCP connection sequence number
$connection_requests # Number of requests on the current TCP connection
$content_length # "Content-Length" request header field
$content_type # "Content-Type" request header field
$cookie_name # Cookie name
$limit_rate # Used to set response speed limit
$msec # Current Unix timestamp
$nginx_version # Nginx version
$pid # PID of worker process
$pipe # "p" if the request comes from pipeline communication, otherwise "."
$proxy_protocol_addr # Get client address accessing the proxy server, empty string if accessed directly
$realpath_root # Real path of the current request's document root or alias, converts all symbolic links to real paths
$remote_addr # Client address
$remote_port # Client port
$remote_user # Username for HTTP basic authentication service
$request # Client request address
$request_body # Client request body: this variable can be used in location to pass the request body to the next-level proxy server via proxy_pass, fastcgi_pass, uwsgi_pass, and scgi_pass
$request_body_file # Save the client request body in a temporary file. This file needs to be deleted after file processing. If you need to enable this feature, you need to set client_body_in_file_only. If you want to pass this file to the backend proxy server, you need to disable request body, i.e., set proxy_pass_request_body off, fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off
$request_completion # "OK" if the request is successful, empty if the request is not completed or not the last part of a range request
$request_filename # File path of the current request, generated by the root or alias directive with the URI request
$request_length # Length of the request (including request address, http headers and request body)
$request_method # HTTP request method, usually "GET" or "POST"
$request_time # Time spent processing the client request, in seconds, with millisecond precision; from reading the first byte from the client to writing the last character to the client before logging.
$request_uri # Original URI containing some client request parameters, cannot be modified, see $uri to change or rewrite URI, does not include hostname, e.g., "/test.php?arg=value"
$scheme # Web protocol used in the request, "http" or "https"
$server_addr # Server address, note: to avoid accessing the Linux kernel, the IP address should be set in the configuration file in advance
$server_name # Server name
$server_port # Server port
$server_protocol # HTTP version of the server, usually "HTTP/1.0" or "HTTP/1.1"
$status # HTTP response code
$time_iso8601 # Server time in ISO 8610 format
$time_local # Server time (LOG Format)
$cookie_NAME # Cookie variable in the client request Header, prefix "$cookie_" plus cookie name variable, the value of this variable is the value of the cookie name
$http_NAME # Matches any request header field; the second half NAME in the variable name can be replaced with any request header field, such as in the configuration file to get http request header: "Accept-Language", $http_accept_language can be used
$http_cookie # All cookies in the request
$http_host # Request address, i.e., the address you type in the browser (IP or domain)
$http_referer # Referring URL, used to record which page linked to the current page
$http_user_agent # User terminal browser information
$http_x_forwarded_for # Client IP and proxy server IP, separated by commas; can be forged
$http_x_forwarded_proto # Protocol of the request
$sent_http_NAME # Can set any http response header field; the second half NAME in the variable name can be replaced with any response header field, such as to set the response header Content-length, $sent_http_content_length can be used
$sent_http_cache_control
$sent_http_connection
$sent_http_content_type
$sent_http_keep_alive
$sent_http_last_modified
$sent_http_location
- Nginx Path Matching Configuration
Method 1:
location ~ ^/(.+)/(.+)/(.+\.js)$ {
alias /web/jar/mcms/template/1/pc/$3;
}
Method 2:
location ~ ^/.+/.+/.+\.png$ {
root /web/jar/mcms/template/1/pc/;
rewrite ^/(.+/)(.+\.png) /images/$2 break;
}
Method 3:
location ~ ^/a/.+/.+\.png$ {
root /web/jar/mcms/template/1/pc/;
if($http_user_agent ~* (iphone|android)) {
root /web/jar/mcms/html/phone/;
}
rewrite ^/(.+/)(.+\.png) /images/$2 break ;
}
Method 4:
location /c/ {
alias /web/jar/mcms/template/1/pc/images/;
}
Method 5:
location /b/ {
root /web/jar/mcms/template/1/pc/;
location /b/ {
alias /web/jar/mcms/template/1/pc/;
}
}
Method 6:
location ~* \.png$ {
root /web/jar/mcms/template/1/pc/;
}