nginx 配置文件解读
参考:链接
在微服务的体系之下,Nginx正在被越来越多的项目采用作为网关来使用,配合 Lua 做限流、熔断等控制
——源自 nginx
Lua #
脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。
参考:链接
nginx 和 Lua 如何做限流、熔断控制?#
参考: 链接
Lua 是一个脚本文件,里面写入限流程序即可
熔断是什么?#
参考:链接
服务熔断:
当下游的服务因为某种原因突然变得不可用或响应过慢,上游服务为了保证自己整体服务的可用性,不再继续调用目标服务,直接返回,快速释放资源。如果目标服务情况好转则恢复调用。
指软件系统中,由于某些原因使得服务出现了过载现象,为防止造成整个系统故障,从而采用的一种保护措施,所以很多地方把熔断亦称为过载保护
nginx 原理#
优点1:
- 传统的小型网站并发量小,用户使用的少,所以在低并发的情况下,用户可以直接访问tomcat服务器,然后tomcat服务器返回消息给用户。当然,为了解决并发,可以使用负载均衡,也就是多增加几个tomcat服务器,当用户访问的时候,请求可以提交到空闲的tomcat服务器上。
- 但是这种情况下可能会出现一种问题:假设把图片上传到了tomcat1上了,当要访问这个图片的时候,tomcat1正好在工作,所以访问的请求就交给其他的tomcat操作,而tomcat之间的数据没有进行同步,所以就发生了我们要请求的图片找不到。
- 为了解决这种情况,我们专门建立一个图片服务器,用来存储图片。这样当都把图片上传的时候,不管是哪个服务器接收到图片,都把图片上传到图片服务器。而图片服务器上需要安装一个http服务,可以使用tomcat、apache、nginx。
优点2:
- nginx常用做静态内容服务和代理服务器,直面外来请求转发给后面的应用服务(tomcat,django什么的),tomcat更多用来做做一个应用容器,让java web app跑在里面的东西。
优点3:
- nginx作反向代理:
- 反向代理就是后端服务不直接对外暴露,请求首先发送到nginx,然后nginx将请求转发到后端服务器,比如tomcat等.如果后端服务只有一台服务器,nginx在这里只有一个作用就是起到了代理后端服务接收请求的作用.称之为反向代理.
优点4:
- nginx作负载均衡:
- 在现实的应用场景中,一台后端服务器出现单点故障的概率很大或者单台机器的吞吐量有限,无法承担过多请求.这时候就需要在nginx后端配置多台服务器,利用nginx内置的规则讲请求转发到后端不同的机器上.这时候就起到了负载均衡的作用.
配置文件nginx.conf#
参考:链接
版本:nginx/1.14.1
文件一般在:/etc/nginx/nginx.conf
主要分为六个区域#
- main(全局设置),main部分设置的指令将影响其它所有部分的设置;
- events(nginx工作模式)
- http(http设置)
- server(主机设置),server部分的指令主要用于指定虚拟主机域名、IP和端口;
- upstream(上游服务器设置,主要为反向代理、负载均衡相关配置),upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;
- location(URL匹配特定位置后的设置),location部分用于匹配网页位置(比如,根目录“/”,“/images”,等等);
它们之间的关系#
- server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。
配置文件讲解#
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ ## 全局设置 user nginx; worker_processes auto; # worker进程的数量 error_log / var /log/nginx/error.log; #报错日志的位置 pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; #加载 该目录下所有.conf的文件进来 ## 工作模式设置 events { worker_connections 1024; #每个worker进程支持的最大连接数 } ## http设置 http { 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 / var /log/nginx/access.log main; sendfile on ; # 开启高效传输模式 tcp_nopush on ; tcp_nodelay on ; keepalive_timeout 65; #连接超时 types_hash_max_size 2048; include /etc/nginx/mime.types; #nginx支持的媒体类型库文件 default_type application/octet-stream; #默认媒体类型 # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; <br> ### server设置 # 表示一个独立的虚拟主机站点 server { listen 80 default_server; #提供服务的端口,默认80 listen [::]:80 default_server; server_name _; #提供服务的域名主机名 root /usr/share/nginx/html; # 站点的根目录 # Load configuration files for the default server block. include /etc/nginx/ default .d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; #出现对应的http状态码时,使用50x.html回应客户 location = /50x.html { #location区块开始,访问50x.html } } # Settings for a TLS enabled server. # # server { # listen 443 ssl http2 default_server; # listen [::]:443 ssl http2 default_server; # server_name _; # root /usr/share/nginx/html; # # ssl_certificate "/etc/pki/nginx/server.crt"; # ssl_certificate_key "/etc/pki/nginx/private/server.key"; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers PROFILE=SYSTEM; # ssl_prefer_server_ciphers on; # # # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; # # location / { # } # # error_page 404 /404.html; # location = /40x.html { # } # # error_page 500 502 503 504 /50x.html; # location = /50x.html { # } # } } |
官方文档#
参考:链接
配置介绍:一个示例站点配置,它将除图像和以“ / download /”开头的请求之外的所有请求传递到后端
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | user www www; worker_processes 2; pid / var /run/nginx.pid; # [ debug | info | notice | warn | error | crit ] error_log / var /log/nginx.error_log info; events { worker_connections 2000; # use [ kqueue | epoll | /dev/poll | select | poll ]; use kqueue; } http { include conf/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$gzip_ratio"' ; log_format download '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$http_range" "$sent_http_content_range"' ; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; gzip on ; gzip_min_length 1100; gzip_buffers 4 8k; gzip_types text/plain; output_buffers 1 32k; postpone_output 1460; sendfile on ; tcp_nopush on ; tcp_nodelay on ; send_lowat 12000; keepalive_timeout 75 20; #lingering_time 30; #lingering_timeout 10; #reset_timedout_connection on; server { listen one.example.com; server_name one.example.com www.one.example.com; access_log / var /log/nginx.access_log main; location / { proxy_pass http: //127.0.0.1/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; client_body_temp_path / var /nginx/client_body_temp; proxy_connect_timeout 70; proxy_send_timeout 90; proxy_read_timeout 90; proxy_send_lowat 12000; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_temp_path / var /nginx/proxy_temp; charset koi8-r; } error_page 404 /404.html; location = /404.html { root /spool/www; } location /old_stuff/ { rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent; } location /download/ { valid_referers none blocked server_names *.example.com; if ($invalid_referer) { #rewrite ^/ http://www.example.com/; return 403; } #rewrite_log on; # rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3 rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break ; root /spool/www; #autoindex on; access_log / var /log/nginx-download.access_log download; } location ~* \.(jpg|jpeg|gif)$ { root /spool/www; access_log off; expires 30d; } } } |
作者:Hang Shao
出处:https://www.cnblogs.com/pam-sh/p/13704620.html
版权:本作品采用「知识共享」许可协议进行许可。
声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)