Nginx 配置模板
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
client_max_body_size 100m;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# http://culture.example.com
server {
listen 80;
server_name culture.example.com;
location / {
proxy_pass http://192.168.1.111:8080;
}
}
# http://game.example.com
server {
listen 80;
server_name game.example.com;
location / {
proxy_pass http://192.168.1.111:8081;
}
}
# http://rank.example.com
server {
listen 80;
server_name rank.example.com;
location / {
proxy_pass http://192.168.1.111:8082;
}
}
# http://console.example.com 代理前端页面
server {
listen 80;
server_name console.example.com;
location / {
proxy_pass http://console.example.com:9527;
}
}
# http://html.example.com
server {
listen 80;
server_name html.example.com;
location / {
try_files $uri $uri/ /index.html;
root /home/example/html;
index index.html#/login;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
# 负载配置 weight 请求分配的权重
upstream test.example.com {
server 192.168.1.111:8088 weight=2;
server 192.168.1.112:8088 weight=3;
server hello.example.com:8088 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://test.example.com;
}
}
# http://console.example.com 后台管理前端
server {
listen 9527;
server_name console.example.com;
client_max_body_size 1000M;
# 解决跨域问题
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
#root D:/software/nginx-1.14.0/dist;
#charset koi8-r;
#access_log logs/host.access.log main;
if ($request_method = 'OPTIONS') {
return 204;
}
location / {
# root html;
root /home/admin/front/dist;
try_files $uri $uri/ @router;
index index.html index.htm;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 解决页面刷新问题
location @router {
rewrite ^.*$ /index.html last;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
upstream模块
server指令:配置处理请求的服务器IP或域名,端口可选,不配置默认使用80端口。通过上面的配置,Nginx默认将请求依次分配给111,112,113来处理,可以通过修改下面这些参数来改变默认的分配策略:
- weight
默认为1,将请求平均分配给每台server
upstream test.example.com {
server 192.168.1.111:8888 weight=2; # 2/6次
server 192.168.1.112:8888 weight=3; # 3/6次
server 192.168.1.113:8888 weight=1; # 1/6次
}
上例配置,表示6次请求中,111分配2次,112分配3次,113分配1次
- max_fails
默认为1。某台Server允许请求失败的次数,超过最大次数后,在fail_timeout时间内,新的请求将不会分配给这台机器。如果设置为0,Nginx会将这台Server置为永久无效状态,然后将请求发给定义了proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream指令来处理这次错误的请求。 - fail_timeout
默认为10秒。某台Server达到max_fails次失败请求后,在fail_timeout期间内,nginx会认为这台Server暂时不可用,不会将请求分配给它
upstream test.example.com {
server 192.168.1.111:8888 weight=2 max_fails=3 fail_timeout=15;
server 192.168.1.112:8888 weight=3;
server 192.168.1.113:8888 weight=1;
}
192.168.1.111这台机器,如果有3次请求失败,nginx在15秒内,不会将新的请求分配给它。
- backup
备份机,所有服务器挂了之后才会生效
upstream test.example.com {
server 192.168.1.111:8888 weight=2 max_fails=3 fail_timeout=15;
server 192.168.1.112:8888 weight=3;
server 192.168.1.113:8888 backup;
}
在111和112都挂了之前,113为不可用状态,不会将请求分配给它。只有当111和112都挂了,113才会被启用。
- down
标识某一台server不可用。可能能通过某些参数动态的激活它吧,要不真没啥用。
upstream test.example.com {
server 192.168.1.111:8888 weight=2 max_fails=3 fail_timeout=15;
server 192.168.1.112:8888 down;
server 192.168.1.113:8888 backup;
}
表示112这台Server为无效状态,不会将请求分配给它。
- max_conns
限制分配给某台Server处理的最大连接数量,超过这个数量,将不会分配新的连接给它。默认为0,表示不限制。注意:1.5.9之后的版本才有这个配置
upstream test.example.com {
server 192.168.1.111:8888 max_conns=1110;
}
表示最多给111这台Server分配1110个请求,如果这台Server正在处理1110个请求,nginx将不会分配新的请求给到它。假如有一个请求处理完了,还剩下999个请求在处理,这时nginx也会将新的请求分配给它。
- resolve
将server指令配置的域名,指定域名解析服务器。需要在http模块下配置resolver指令,指定域名解析服务
http {
resolver 192.168.1.2;
upstream u {
zone ...;
...
server hello.example.com resolve;
}
}
上面配置表示hello.example.com域名,由192.168.1.2服务器来负责解析。(私有域名服务器可自行部署,不然也可以使用阿里云或者华为云服务提供的域名解析服务)
upstream模块server指令的其它参数和详细配置说明,请参考官方文档。