1. #user nobody;
    2. #worker_processes 1;
    3. worker_processes 8; #这个根据硬件有多少核CPU而定
    4. #开发环境
    5. #error_log logs/error.log debug;
    6. #生产环境
    7. error_log logs/error.log;
    8. pid logs/nginx.pid;
    9. events {
    10. use epoll;
    11. worker_connections 1024000;
    12. }
    13. http {
    14. default_type 'text/html';
    15. charset utf-8;
    16. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    17. '$status $body_bytes_sent "$http_referer" '
    18. '"$http_user_agent" "$http_x_forwarded_for"';
    19. #access_log logs/access_main.log main;
    20. access_log off; #日志功能要关闭
    21. #sendfile: 设置为on表示启动高效传输文件的模式。
    22. # sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。
    23. # 开启这个参数后可以让数据不用经过用户buffer。表示开启零复制,建议生产环境使用
    24. sendfile off;
    25. #sendfile on;
    26. #tcp_nopush on;
    27. #keepalive_timeout 0;
    28. keepalive_timeout 65;
    29. #gzip on;
    30. gzip off;
    31. #gzip最小长度 一般设置成1K就行,小于1K的就不压缩了 不然会越压越大
    32. gzip_min_length 1024;
    33. #gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)。
    34. gzip_comp_level 1;
    35. #匹配MIME类型进行压缩
    36. 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;
    37. gzip_vary on;
    38. gzip_disable "MSIE [1-6]\.";
    39. #设置系统获取几个单位的缓存用于存储gzip的压缩结 果数据流。 例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。
    40. #如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。
    41. gzip_buffers 4 8k;
    42. #gzip默认最低支持1.1现在改成最低支持1.0 近代浏览器基本不设置
    43. gzip_http_version 1.0;
    44. #Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。
    45. #off - 关闭所有的代理结果数据的压缩
    46. #expired - 启用压缩,如果header头中包含 "Expires" 头信息
    47. #no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
    48. #no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
    49. #private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
    50. #no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
    51. #no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
    52. #auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
    53. #any - 无条件启用压缩
    54. gzip_proxied expired no-cache no-store private auth;
    55. #指定缓存信息
    56. lua_shared_dict ngx_cache 128m;
    57. #指定缓存信息
    58. lua_shared_dict seckill_cache 128m;
    59. #保证只有一个线程去访问redis或是mysql-lock for cache
    60. lua_shared_dict cache_lock 100k;
    61. #lua扩展加载
    62. # for linux
    63. # lua_package_path "./?.lua;/vagrant/LuaDemoProject/src/?.lua;/usr/local/ZeroBraneStudio-1.80/?/?.lua;/usr/local/ZeroBraneStudio-1.80/?.lua;;";
    64. # lua_package_cpath "/usr/local/ZeroBraneStudio-1.80/bin/clibs/?.so;;";
    65. 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;;";
    66. lua_package_cpath "/usr/local/openresty/lualib/?/?.so;/usr/local/openresty/lualib/?.so;;";
    67. # for windows
    68. # 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;;";
    69. # lua_package_cpath "E:/tool/ZeroBraneStudio-1.80/bin/clibs/?.dll;E:/tool/openresty-1.13.6.2-win32/lualib/?.dll;;";
    70. # 初始化项目
    71. init_by_lua_file luaScript/initial/loading_config.lua;
    72. #调试模式(即关闭lua脚本缓存)
    73. #lua_code_cache off;
    74. lua_code_cache on;
    75. #内部网关的代理,内部网关带有 token 认证
    76. upstream zuul {
    77. # idea 开发环境
    78. # server 192.168.56.121:7799;
    79. # centos 自验证环境
    80. server "cdh1:8888";
    81. keepalive 1000;
    82. }
    83. underscores_in_headers on;
    84. limit_req_zone $arg_sku_id zone=skuzone:10m rate=6r/m;
    85. limit_req_zone $http_user_id zone=userzone:10m rate=6r/m;
    86. limit_req_zone $binary_remote_addr zone=perip:10m rate=6r/m;
    87. limit_req_zone $server_name zone=perserver:1m rate=10r/s;
    88. limit_req_zone $server_name zone=seckill_server:1m rate=20000r/s;
    89. server {
    90. listen 80;
    91. server_name admin.nginx.server;
    92. default_type 'text/html';
    93. charset utf-8;
    94. limit_req zone=perip;
    95. limit_req zone=perserver;
    96. #管理控制台的web页面
    97. location / {
    98. if ($request_method = 'OPTIONS') {
    99. add_header Access-Control-Max-Age 600000;
    100. add_header Access-Control-Allow-Origin *;
    101. add_header Access-Control-Allow-Credentials true;
    102. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    103. 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';
    104. return 204;
    105. }
    106. # IDEA 管理控制台的web页面
    107. proxy_pass http://192.168.56.121:8066/ ;
    108. # proxy_pass http://zuul;
    109. }
    110. }
    111. server {
    112. listen 80 default;
    113. server_name nginx.server *.nginx.server;
    114. default_type 'text/html';
    115. charset utf-8;
    116. # 转发 zuul
    117. location / {
    118. if ($request_method = 'OPTIONS') {
    119. add_header Access-Control-Max-Age 600000;
    120. add_header Access-Control-Allow-Origin *;
    121. add_header Access-Control-Allow-Credentials true;
    122. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    123. 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';
    124. return 204;
    125. }
    126. proxy_set_header X-Real-IP $remote_addr;
    127. proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    128. proxy_set_header Host $http_host;
    129. proxy_set_header X-Nginx-Proxy true;
    130. proxy_pass http://zuul;
    131. }
    132. # 开发调试: 用户服务
    133. location ^~ /uaa-provider/ {
    134. # idea 开发环境
    135. # proxy_pass http://192.168.56.121:7702/;
    136. # centos 自验证环境
    137. proxy_pass http://192.168.56.121:7702/uaa-provider/ ;
    138. }
    139. # 开发调试: 秒杀服务
    140. location ^~ /seckill-provider/ {
    141. proxy_pass http://192.168.56.121:7701/seckill-provider/ ;
    142. }
    143. # 开发调试: 管理服务
    144. location ^~ /backend-provider/ {
    145. proxy_set_header X-Real-IP $remote_addr;
    146. proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    147. proxy_set_header Host $http_host;
    148. proxy_set_header backend 'true'; # 给后台的请求加上自定义头,方便session区分
    149. proxy_set_header X-Nginx-Proxy true;
    150. # 指向微服務
    151. proxy_pass http://192.168.56.121:6600/backend-provider/ ;
    152. #指向gate way
    153. # proxy_pass http://zuul;
    154. }
    155. # 反向代理:秒杀web页面
    156. location ^~ /seckill-web/ {
    157. proxy_pass http://192.168.56.121:6601/seckill-web/ ;
    158. }
    159. # Nginx+lua 秒杀:获取秒杀 token
    160. location = /seckill-provider/api/seckill/redis/token/v2 {
    161. default_type 'application/json';
    162. charset utf-8;
    163. # 限流的 lua 脚本
    164. access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua;
    165. # 获取秒杀token lua 脚本
    166. content_by_lua_file luaScript/module/seckill/getToken.lua;
    167. }
    168. # ratelimit by sku id
    169. location = /ratelimit/sku {
    170. limit_req zone=skuzone;
    171. echo "正常的响应";
    172. }
    173. # ratelimit by user id
    174. location = /ratelimit/demo {
    175. limit_req zone=userzone;
    176. echo "正常的响应";
    177. }
    178. location = /50x.html{
    179. echo "限流后的降级内容";
    180. }
    181. error_page 502 503 =200 /50x.html;
    182. # 访问的路径 http://cdh1/ratelimit/demo2?seckillSkuId=3
    183. # ratelimit by sku id
    184. location = /ratelimit/demo2 {
    185. # 限流的 lua 脚本
    186. access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua;
    187. echo "正常的响应";
    188. }
    189. }
    190. server {
    191. listen 8080 default;
    192. server_name nginx.server *.nginx.server ;
    193. #limit_req zone=seckill_server;
    194. #第一个lua脚本 hellword
    195. location /helloworld {
    196. default_type 'text/html';
    197. charset utf-8;
    198. content_by_lua_file luaScript/module/demo/helloworld.lua;
    199. }
    200. location / { # 自动匹配到(htm|html)格式
    201. ## 开发阶段,配置页面不缓存html和htm结尾的文件
    202. add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    203. index index.html;
    204. root /vagrant/LuaDemoProject/src/www/static; #服务器路径
    205. default_type 'text/html';
    206. }
    207. location ~ .*\.(htm|html)$ { # 自动匹配到(htm|html)格式
    208. ## 开发阶段,配置页面不缓存html和htm结尾的文件
    209. add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    210. root /vagrant/LuaDemoProject/src/www/static; #服务器路径
    211. default_type 'text/html';
    212. }
    213. location ~ .*\.(js|script)$ { # 自动匹配到(jpg|gif|png)格式
    214. add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    215. root /vagrant/LuaDemoProject/src/www/static; #服务器路径
    216. default_type 'application/javascript';
    217. }
    218. location ~ .*\.(css)$ { # 自动匹配到(css)格式
    219. root /vagrant/LuaDemoProject/src/www/static; #服务器路径
    220. default_type 'text/css';
    221. }
    222. # 开发调试: 库存服务
    223. location ^~ /stock-provider/ {
    224. proxy_set_header Host $host:$server_port;
    225. proxy_set_header X-Real-IP $remote_addr;
    226. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    227. proxy_pass http://zuul/stock-provider/ ;
    228. }
    229. # 开发调试: 秒杀服务
    230. location ^~ /seckill-provider/ {
    231. # proxy_pass http://localhost:7701/seckill-provider/ ;
    232. #指向gate way
    233. proxy_set_header Host $host:$server_port;
    234. proxy_set_header X-Real-IP $remote_addr;
    235. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    236. proxy_pass http://zuul/seckill-provider/;
    237. }
    238. # Nginx+lua :获取 商品信息
    239. location = /stock-lua/gooddetail {
    240. default_type 'application/json';
    241. charset utf-8;
    242. # 限流的 lua 脚本
    243. #access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua;
    244. # 获取秒杀token lua 脚本
    245. content_by_lua_file luaScript/module/seckill/good_detail.lua;
    246. }
    247. # Nginx+lua 秒杀:获取秒杀 token
    248. location ~ /seckill-lua/(.*)/getToken/v3 {
    249. default_type 'application/json';
    250. charset utf-8;
    251. set $skuId $1;
    252. limit_req zone=userzone;
    253. limit_req zone=seckill_server;
    254. # 限流的 lua 脚本
    255. # access_by_lua_file luaScript/module/seckill/getToken_access_limit.lua;
    256. # 获取秒杀token lua 脚本
    257. content_by_lua_file luaScript/module/seckill/getToken_v3.lua;
    258. }
    259. location = /echo {
    260. echo "echo";
    261. }
    262. location = /50x.html{
    263. echo "限流后的降级内容";
    264. }
    265. error_page 502 503 =200 /50x.html;
    266. }
    267. }