1. 常用配置
1. 标准root页面配置
1 2 3 4 5 6 7 8 9 10 11 12
| server { listen 80; server_name 127.0.0.1;
client_max_body_size 100m; location / { root /app/xxx; index index.html index.htm; try_files $uri $uri/ /index.html; } }
|
2. 根据路径访问不同前端静态页面项目
1 2 3 4 5
| location /app-h5 { try_files $uri $uri/ /app-h5/index.html; alias /usr/share/nginx/html/h5/mobile/app-h5; index index.html index.htm; }
|
3. 接口反向代理
1 2 3 4 5
| location /app-api { proxy_pass http://b.domain.com:9000; }
|
4. upstream负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13
| upstream appServer { server 10.0.0.80:5000; server 10.0.0.81:5000; } server { listen 80; server_name 127.0.0.1;
location /app-api { proxy_pass http://appServer; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| upstream appServer { server 10.0.0.80:5000 weight=10; server 10.0.0.81:5000 weight=20; }
upstream appServer { ip_hash; server 10.0.0.80:5000; server 10.0.0.81:5000; }
upstream appServer { server 10.0.0.80:5000; server 10.0.0.81:5000; hash $request_uri; }
upstream appServer { server 10.0.0.80:5000; server 10.0.0.81:5000; fair; }
upstream appServer { least_conn; server 10.0.0.80:5000; server 10.0.0.81:5000; }
|
| 调度算法 |
概述 |
| 轮询 |
逐一轮询,默认算法 |
| weight轮询 |
加权轮询,weight越大,轮询到的几率越大 |
| fair |
根据后端服务器的响应时间来分配,响应时间短的优先分配 |
| ip_hash |
根据ip的hash结果进行分配,来自同一客户端的请求可以打在同一台机器上, 能解决后台session不共享的问题 |
| url_hash |
根据访问的url进行hash分配 |
| least_conn |
最小连接数,哪个服务器连接数少分配给哪个服务器 |
| hash关键数值 |
hash自定义key |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| upstream appServer { server 10.0.0.80:5000 weight=10; server 10.0.0.81:5000 weight=20; }
upstream appServer { server 10.0.0.80:5000 weight=10 max_fails=3 fail_timeout=15; server 10.0.0.81:5000 weight=20; }
upstream appServer { server 10.0.0.80:5000 weight=10; server 10.0.0.81:5000 weight=20; server 10.0.0.82:5000 backup; }
upstream appServer { server 10.0.0.80:5000 weight=10; server 10.0.0.81:5000 weight=20; server 10.0.0.81:5000 down; }
upstream appServer { server 10.0.0.80:5000 weight=10; server 10.0.0.81:5000 max_conn=1000; }
|
| 状态 |
概述 |
| down |
当前的server暂不参与负载均衡 |
| backup |
预留的备份服务器,当其他服务器都挂了的时候启用 |
| max_fails |
允许请求失败的次数 ,如果请求失败次数超过限制,则进过fail_timeout 时间后从虚拟服务池中kill掉该服务器 |
| fail_timeout |
经过max_fails失败后,服务暂停时间,max_fails设置后,必须设置fail_timeout 值 |
| max_conn |
限制最大的连接数,用于服务器硬件配置不同的情况下 |
5. 允许跨域访问
可将下面配置添加到指定location中或者server中。
1 2
| add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Credentials true;
|
6. iframe同源策略限制
限制iframe插入,可设置为非同源不插入,指定origin插入,默认为不限制
1 2 3 4 5 6
| add_header X-Frame-Options SAMEORIGIN
add_header X-Frame-Options "ALLOW-FROM http://xxx.com:8000"; add_header Content-Security-Policy "frame-ancestors http://xxx.com:8000";
|
7. gzip压缩
1 2 3 4 5 6 7 8 9 10 11 12 13
| http { include mime.conf; default_type application/octet-stream;
gzip on; gzip_min_length 1k; gzip_comp_level 5; gzip_types text/plain text/css application/x-javascript application/javascript text/javascript;
server { }
|
部分参数含义:
| 参数 |
含义 |
| gzip_min_length |
正整数,单位为字节,也可用k表示千字节,比如写成1024与1k都可以,效果是一样的,表示当资源大于1k时才进行压缩,资源大小取响应头中的Content-Length进行比较,经测试如果响应头不存在Content_length信息,该限制参数对于这个响应包是不起作用的;另外此处参数值不建议设的太小,因为设的太小,一些本来很小的文件经过压缩后反而变大了,官网没有给出建议值,在此建议1k起,因为小于1k的也没必要压缩,并根据实际情况来调整设定。
|
| gzip_comp_level |
1-9的正整数,1最低,压缩时间短,9最高,压缩时间长,吃cpu,但是压缩效果好,实质是用cpu换流量,视情况而用。 |
| gzip_types |
响应报文数据格式,或者说类型,对应http响应头中的Content-type字段,如常见的有text/html、text/css、application/json、application/javaScript等。用于指定要压缩的响应报文类型。”*”表示压缩所有格式的响应报文,多种格式用空格隔开。如text/html text/css。 ————————————————
|
| gzip_http_version |
默认为1.1,此处只能是1.0,或者1.1。设置压缩响应所需的最小http协议版本。 |
8. 配置https
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server { listen 80; server_name 127.0.0.1; ssl_certificate /etc/nginx/ssl_certs/draw.lyan.me.pem; ssl_certificate_key /etc/nginx/ssl_certs/draw.lyan.me.key; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; client_max_body_size 100m; location / { root /app/xxx; index index.html index.htm; try_files $uri $uri/ /index.html; } }
|
9. 配置长连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
http { keepalive_timeout 90s 90s; keepalive_requests 10000; server { listen 80; server_name www.xxx.com;
client_max_body_size 100m; upstream BACKEND { server 192.168.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.0.2:8080 weight=1 max_fails=2 fail_timeout=30s; keepalive 300; // 这个很重要! } location / { root /app/xxx; index index.html index.htm; try_files $uri $uri/ /index.html; } location /mqtt { proxy_pass http://BACKEND; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 90; proxy_send_timeout 90; proxy_connect_timeout 90; } } }
|
10. 限制ip访问
1 2 3 4 5 6 7
| location / { deny 192.168.0.2; allow 192.168.0.0/24; allow 192.168.1.1; deny all; }
|
11. 配置移动端访问
1 2 3 4
| if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { rewrite ^(.*) http://www.abc.com$1 permanent; }
|
12. 配置文件服务器
所谓文件服务器就是通过浏览器可以直接打开服务器的文件,点击可以直接下载,这样就不用专门写下载的页面,简单明了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| user root root;
location /download { alias /home/download/; autoindex on; autoindex_exact_size off; autoindex_localtime on; charset utf-8,gbk; }
location ~ ^/home/download(.*)$ { add_header Content-Disposition "attachment; filename=$1"; }
|
2. 具体参数含义
1. location路径匹配
| 开头 |
功能 |
| 以 = 开头 |
表示精确匹配;如只匹配根目录结尾的请求,后面不能带任何字符串 |
| 以 ~ 开头 |
表示区分大小写的正则匹配 |
| 以 ~* 开头 |
表示不区分大小写的正则匹配 |
| 以 ^~ 开头 |
表示uri以某个常规字符串开头,不是正则匹配 |
| 以 / 开头 |
通用匹配, 如果没有其它匹配,任何请求都会匹配到 |