#定义Nginx运行的用户和用户组
user www www;
#nginx进程数,通常设置成和cpu的数量相等,node可以通过os的cpus获取,可以做高并发
worker_processes 4;
#全局错误日志定义类型,一般不用改 [debug | info | notice | warn | error | crit]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
events {
# 最大并发数 = worker_processes * worker_connections
worker_connections 1024;
}
http {
# mime类型映射文件,注释的话就会走下面的默认类型,如果返回的文件类型不对要检查这个配置项
include mime.types;
#默认文件类型 default_type application/octet-stream;
#nginx 特性 处理静态文件并且大的静态文件,效率很高,因为它会通过线程池去进行分布式加载
sendfile on;
#超时时间
keepalive_timeout 65
# 压缩
gzip on;
# 负载均衡, 默认是轮询, weiht权重分配,可以根据服务器的性能来分配权重
# 容灾技术 backup
upstream fuzaijuheng {
server 127.0.0.1:9001 weiht=3;
server 127.0.0.1:9002 weiht=3;
server 127.0.0.1:9003 backup;
}
#限速技术 $binary_remote_addr nginx 内置变量都是$开头 rate_limit是名称可以自定义, 5r/m 一分钟5个请求
limit_req_zone $binary_remote_addr zone=rate_limit:10m rate=5r/m
#缓存技术
proxy_cache_path [路径] lavels=[目录结构]
server {
listen 80;
#域名可以有多个,用空格隔开
server_name localhost;
#代理 /代理路径
location / {
root html;
index index.html index.htm; #对应的文件
}
#报错码在500 502 503 504 就返回这个50x页面
error_page 500 502 503 504 /50x.hmtl
#反向代理,替换api
location /api {
# burst短时间内可以请求10 nodelay 无延时
limt_req zone=rate_limit burst=10 nodelay
proxy_pass http://fuzaijuheng;
rewrite ^/api/(.*) /$1 break;
}
#防盗链 ~*大小写不敏感 .*匹配所有
location ~*.*\.(jpg|jpeg|gif|png|ico)$ {
root html/static;
#none 允许 referer空
#blocked 允许referer没有
#localhost 允许来源是localhost
#$invalid_referer 如果验证不过就会有值
valid_referers none blocked localhost;
if ($invalid_referer) {
return 403;
}
}
}
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem; # 对应的文件路径
# ssl_certificate_key cert.key; #对应的文件路径
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
}
https配置, 需要下载openssl
- 生成私钥
openssl genpkey -algorithm RSA -out private.key -pkeyopt rea_keygen_bits:2028
- 生成证书请求文件 CSR
openssl req -new -key private.key -out csr.csr
- 通过csr生成证书文件
openssl x509 -req -in csr.csr -signkey private.key -out certificate.crt