This article mainly share with you the Nginx scene application technology hope to help everyone.
Nginx as a static resource Web service
Configuration context: HTTP, server, or location.
May involve, resource compression, cross-domain access, anti-theft chain and other scenarios.
Context for Resource compression configuration: HTTP, server, or location
Context for cross-domain configuration: HTTP, server, or location
Context of the anti-theft chain configuration: Server, location
Configuration reference
Vi/etc/nginx/conf.d/default.conf
server {
...
# Turn on Sendfile to improve network packet transmission efficiency
Sendfile on;
# Configure Image resource storage path and compression mode
Location ~. *\. (jpg|gif|png) $ {
gzip on;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# Image Anti-theft chain configuration
# do not specify Referer protocol incorrect release specified IP release SEO optimization
Valid_referers none blocked 39.104.116.91 ~/google\./;
if ($invalid _referer) {
return 403;
}
Root/opt/app/code/images;
}
# Configure the Txt|xml resource storage path and compression method
Location ~. *\. (Txt|xml) $ {
gzip on;
Gzip_http_version 1.1;
Gzip_comp_level 1;
Gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
Root/opt/app/code/doc;
}
# Cache and cross-domain scene configuration
Location ~. *\. (html|htm) $ {
#给返回报文添加Cache-control, Expires header, control cache
#expires 24h;
#允许跨域到指定域
Add_header Access-control-allow-origin http://somehost.com;
Add_header Access-control-allow-methods get,post,put,delete,options;
Root/opt/app/code;
}
}
Nginx as a proxy service
Forward Proxy
The forward proxy is the client agent. The client is required to access the destination address by setting up a proxy server.
Nginx can act as this proxy server
Configuration reference
Vi/etc/nginx/conf.d/default.conf
server {
...
# may involve DNS domain name resolution
Resolver 8.8.8.8;
Location/{
# Make yourself forwarding the client's request intact
Proxy_pass http://$http _host$request_uri;
}
}
Reverse Proxy
The
reverse proxy is the service-side proxy. Hides the real server address from the client.
Configuration reference
Vi/etc/nginx/conf.d/default.conf
server {
Location/{
#Real server Address
Proxy_pass http://127.0.0.1:8080;
Include Proxy_params;
}
}
# Other Agent configuration independent, easy to reuse
Vi/etc/nginx/proxy_param
# default is all you can do. Unless you return a 301 scenario, you may need to overwrite
Proxy_redirect Defalut;
#配置header信息, let real server know the actual client information
Proxy_set_header Host $http _host;
Proxy_set_header X-real-ip $remote _addr;
#一些代理超时设置
Proxy_connect_timeout 30;
Proxy_send_timeout 60;
Proxy_read_timeout 60;
#代理缓冲区设置
Proxy_buffer_size 32k;
Proxy_buffering on;
Proxy_buffers 4 128k;
Proxy_busy_buffers_size 256k;
Proxy_max_temp_file_size 256k;
Nginx as Load Balancer Service
Reference configuration
Vi/etc/nginx/conf.d/default.conf
Upstream Backend {
Server 192.168.1.101:8001;
Server 192.168.1.102:8002;
Server 192.168.1.102:8003 down;
Server 192.168.1.103:8004 backup;
}
server {
...
Location/{
# Proxy to upstream group
Proxy_pass Http://backend;
Include Proxy_params;
}
}
Server Extra Parameters
After the server of the upstream group, the following parameters are supported:
Parameters |
Description |
Down |
Do not participate in load balancing |
Backup |
A reserved backup server. It provides services when no other node provides services |
Max_fails |
Number of allowable requests failed |
Fail_timeout |
The time that the service was paused after Max_fails failed |
Max_conns |
Limit the maximum number of connections received |
Load Balancing scheduling algorithm
Way |
Description |
Polling |
Assign each to a different back-end server sequentially |
Weighted polling |
The larger the weight value, the greater the probability of being allocated |
Ip_hash |
Same IP fixed access to the same back-end server |
Least_conn |
Which machine is sent with a few connections? |
Url_hash |
Assign by hash result of URL parameter |
Hash key value |
Hash-Custom Key |
# Weighted Polling
Upstream Backend {
Server 192.168.1.101:8001 weight=5;
Server 192.168.1.102:8002;
Server 192.168.1.102:8003 down;
Server 192.168.1.103:8004 backup;
}
# IP HASH
Upstream Backend {
Ip_hash;
Server 192.168.1.101:8001;
Server 192.168.1.102:8002;
Server 192.168.1.102:8003 down;
Server 192.168.1.103:8004 backup;
}
# Minimum number of connections
Upstream Backend {
Least_conn;
Server 192.168.1.101:8001;
Server 192.168.1.102:8002;
Server 192.168.1.102:8003 down;
Server 192.168.1.103:8004 backup;
}
# URL HASH
Upstream Backend {
Url_hash;
Server 192.168.1.101:8001;
Server 192.168.1.102:8002;
Server 192.168.1.102:8003 down;
Server 192.168.1.103:8004 backup;
Related recommendations:
The connection limit of Nginx limit is detailed
Nginx configuration React static Page instance tutorial
PHP using nginx How to implement reverse proxy