#user nobody;#worker_processes 1;worker_processes 8; #这个根据硬件有多少核CPU而定#开发环境#error_log logs/error.log debug;#生产环境error_log logs/error.log;pid logs/nginx.pid;events { use epoll; worker_connections 1024000;}http { default_type 'text/html'; charset utf-8; 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_main.log main; access_log off; #日志功能要关闭 #sendfile: 设置为on表示启动高效传输文件的模式。 # sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。 # 开启这个参数后可以让数据不用经过用户buffer。表示开启零复制,建议生产环境使用 sendfile off; #sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; gzip off; #gzip最小长度 一般设置成1K就行,小于1K的就不压缩了 不然会越压越大 gzip_min_length 1024; #gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)。 gzip_comp_level 1; #匹配MIME类型进行压缩 gzip_types text/plain application/json application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml font/woff; gzip_vary on; gzip_disable "MSIE [1-6]\."; #设置系统获取几个单位的缓存用于存储gzip的压缩结 果数据流。 例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。 #如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。 gzip_buffers 4 8k; #gzip默认最低支持1.1现在改成最低支持1.0 近代浏览器基本不设置 gzip_http_version 1.0; #Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。 #off - 关闭所有的代理结果数据的压缩 #expired - 启用压缩,如果header头中包含 "Expires" 头信息 #no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息 #no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息 #private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息 #no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息 #no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息 #auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息 #any - 无条件启用压缩 gzip_proxied expired no-cache no-store private auth; #指定缓存信息 lua_shared_dict ngx_cache 128m; #指定缓存信息 lua_shared_dict seckill_cache 128m; #保证只有一个线程去访问redis或是mysql-lock for cache lua_shared_dict cache_lock 100k; #lua扩展加载 # for linux # lua_package_path "./?.lua;/vagrant/LuaDemoProject/src/?.lua;/usr/local/ZeroBraneStudio-1.80/?/?.lua;/usr/local/ZeroBraneStudio-1.80/?.lua;;"; # lua_package_cpath "/usr/local/ZeroBraneStudio-1.80/bin/clibs/?.so;;"; lua_package_path "./?.lua;/vagrant/LuaDemoProject/src/?.lua;/vagrant/LuaDemoProject/vendor/template/?.lua;/vagrant/LuaDemoProject/src/?/?.lua;/usr/local/openresty/lualib/?/?.lua;/usr/local/openresty/lualib/?.lua;;"; lua_package_cpath "/usr/local/openresty/lualib/?/?.so;/usr/local/openresty/lualib/?.so;;"; # for windows # lua_package_path "./?.lua;C:/dev/refer/LuaDemoProject/src/vendor/jwt/?.lua;C:/dev/refer/LuaDemoProject/src/?.lua;E:/tool/ZeroBraneStudio-1.80/lualibs/?/?.lua;E:/tool/ZeroBraneStudio-1.80/lualibs/?.lua;E:/tool/openresty-1.13.6.2-win32/lualib/?.lua;;"; # lua_package_cpath "E:/tool/ZeroBraneStudio-1.80/bin/clibs/?.dll;E:/tool/openresty-1.13.6.2-win32/lualib/?.dll;;"; # 初始化项目 init_by_lua_file luaScript/initial/loading_config.lua; #调试模式(即关闭lua脚本缓存) #lua_code_cache off; lua_code_cache on; #内部网关的代理,内部网关带有 token 认证 upstream zuul { # idea 开发环境 # server 192.168.56.121:7799; # centos 自验证环境 server "cdh1:8888"; keepalive 1000; } underscores_in_headers on; limit_req_zone $arg_sku_id zone=skuzone:10m rate=6r/m; limit_req_zone $http_user_id zone=userzone:10m rate=6r/m; limit_req_zone $binary_remote_addr zone=perip:10m rate=6r/m; limit_req_zone $server_name zone=perserver:1m rate=10r/s; limit_req_zone $server_name zone=seckill_server:1m rate=20000r/s; server { listen 80; server_name admin.nginx.server; default_type 'text/html'; charset utf-8; limit_req zone=perip; limit_req zone=perserver; #管理控制台的web页面 location / { if ($request_method = 'OPTIONS') { add_header Access-Control-Max-Age 600000; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, token'; return 204; } # IDEA 管理控制台的web页面 proxy_pass http://192.168.56.121:8066/ ; # proxy_pass http://zuul; } } server { listen 80 default; server_name nginx.server *.nginx.server; default_type 'text/html'; charset utf-8; # 转发 zuul location / { if ($request_method = 'OPTIONS') { add_header Access-Control-Max-Age 600000; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, token'; return 204; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_pass http://zuul; } # 开发调试: 用户服务 location ^~ /uaa-provider/ { # idea 开发环境 # proxy_pass http://192.168.56.121:7702/; # centos 自验证环境 proxy_pass http://192.168.56.121:7702/uaa-provider/ ; } # 开发调试: 秒杀服务 location ^~ /seckill-provider/ { proxy_pass http://192.168.56.121:7701/seckill-provider/ ; } # 开发调试: 管理服务 location ^~ /backend-provider/ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header backend 'true'; # 给后台的请求加上自定义头,方便session区分 proxy_set_header X-Nginx-Proxy true; # 指向微服務 proxy_pass http://192.168.56.121:6600/backend-provider/ ; #指向gate way # proxy_pass http://zuul; } # 反向代理:秒杀web页面 location ^~ /seckill-web/ { proxy_pass http://192.168.56.121:6601/seckill-web/ ; } # Nginx+lua 秒杀:获取秒杀 token location = /seckill-provider/api/seckill/redis/token/v2 { default_type 'application/json'; charset utf-8; # 限流的 lua 脚本 access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua; # 获取秒杀token lua 脚本 content_by_lua_file luaScript/module/seckill/getToken.lua; } # ratelimit by sku id location = /ratelimit/sku { limit_req zone=skuzone; echo "正常的响应"; } # ratelimit by user id location = /ratelimit/demo { limit_req zone=userzone; echo "正常的响应"; } location = /50x.html{ echo "限流后的降级内容"; } error_page 502 503 =200 /50x.html; # 访问的路径 http://cdh1/ratelimit/demo2?seckillSkuId=3 # ratelimit by sku id location = /ratelimit/demo2 { # 限流的 lua 脚本 access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua; echo "正常的响应"; } } server { listen 8080 default; server_name nginx.server *.nginx.server ; #limit_req zone=seckill_server; #第一个lua脚本 hellword location /helloworld { default_type 'text/html'; charset utf-8; content_by_lua_file luaScript/module/demo/helloworld.lua; } location / { # 自动匹配到(htm|html)格式 ## 开发阶段,配置页面不缓存html和htm结尾的文件 add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate"; index index.html; root /vagrant/LuaDemoProject/src/www/static; #服务器路径 default_type 'text/html'; } location ~ .*\.(htm|html)$ { # 自动匹配到(htm|html)格式 ## 开发阶段,配置页面不缓存html和htm结尾的文件 add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate"; root /vagrant/LuaDemoProject/src/www/static; #服务器路径 default_type 'text/html'; } location ~ .*\.(js|script)$ { # 自动匹配到(jpg|gif|png)格式 add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate"; root /vagrant/LuaDemoProject/src/www/static; #服务器路径 default_type 'application/javascript'; } location ~ .*\.(css)$ { # 自动匹配到(css)格式 root /vagrant/LuaDemoProject/src/www/static; #服务器路径 default_type 'text/css'; } # 开发调试: 库存服务 location ^~ /stock-provider/ { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://zuul/stock-provider/ ; } # 开发调试: 秒杀服务 location ^~ /seckill-provider/ { # proxy_pass http://localhost:7701/seckill-provider/ ; #指向gate way proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://zuul/seckill-provider/; } # Nginx+lua :获取 商品信息 location = /stock-lua/gooddetail { default_type 'application/json'; charset utf-8; # 限流的 lua 脚本 #access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua; # 获取秒杀token lua 脚本 content_by_lua_file luaScript/module/seckill/good_detail.lua; } # Nginx+lua 秒杀:获取秒杀 token location ~ /seckill-lua/(.*)/getToken/v3 { default_type 'application/json'; charset utf-8; set $skuId $1; limit_req zone=userzone; limit_req zone=seckill_server; # 限流的 lua 脚本 # access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua; # 获取秒杀token lua 脚本 content_by_lua_file luaScript/module/seckill/getToken_v3.lua; } location = /echo { echo "echo"; } location = /50x.html{ echo "限流后的降级内容"; } error_page 502 503 =200 /50x.html; }}