nginx 这一篇就够了

您所在的位置:网站首页 nginx文件目录 nginx 这一篇就够了

nginx 这一篇就够了

2024-01-16 12:33| 来源: 网络整理| 查看: 265

nginx 安装 安装依赖 下载 编译安装 编译时将 ssl 模块静态编译 nginx 服务架构 模块化结构 模块化开发 nginx 的模块化结构 nginx 的模块清单 nginx 的 web 请求处理机制 nginx 配置文件实例 nginx 服务器基础配置指令 nginx.conf 文件的结构 nginx 运行相关的 Global 部分 配置运行 nginx 服务器用户 配置允许生成的 worker process 数 配置 nginx 进程 PID 存放路径 配置错误日志的存放路径 配置文件的引入 与用户的网络连接相关的 events 设置网络连接的序列化 设置是否允许同时接收多个网络连接 事件驱动模型的选择 配置最大连接数 http http Global 代理 - 缓存 - 日志 - 第三方模块配置 定义 MIME-Type 自定义服务日志 配置允许 sendfile 方式传输文件 配置连接超时时间 单连接请求数上限 server 配置网络监听 基于名称的虚拟主机配置 配置 https 证书 基于 IP 的虚拟主机配置 配置 location 块 [root] 配置请求的根目录 [alias] 更改 location 的 URI 设置网站的默认首页 设置网站的错误页面 基于 IP 配置 nginx 的访问权限 基于密码配置 nginx 的访问权限 应用 架设简单文件服务器 nginx 正向代理 nginx 服务器基础配置实例 测试 myServer1 的访问 测试 myServer2 的访问 使用缓存 使用 location 反向代理到已有网站 其他 ngx_http_sub_module 替换响应中内容 配置 http 强制跳转 https 安装 安装依赖

安装 nginx 之前,确保系统已经安装 gcc、openssl-devel、pcre-devel 和 zlib-devel 软件库

gcc 可以通过光盘直接选择安装 openssl-devel、zlib-devel 可以通过光盘直接选择安装,https 时使用 pcre-devel 安装 pcre 库是为了使 nginx 支持 HTTP Rewrite 模块 下载

nginx 下载

编译安装

通过上面的下载页下载最新的稳定版

#wget http://nginx.org/download/nginx-1.8.0.tar.gz #tar xzvf nginx-1.8.0.tar.gz #cd nginx-1.8.0 #./configure --prefix=/opt/X_nginx/nginx --with-http_ssl_module #make && sudo make install --prefix=/opt/X_nginx/nginx 安装目录 --with-http_ssl_module 添加 https 支持 编译时将 ssl 模块静态编译 ./configure --prefix=/opt/X_nginx/nginx \ --with-openssl=../openssl-1.0.2l \ --with-zlib=../zlib-1.2.11 \ --with-pcre=../pcre-8.41 \ --with-http_ssl_module nginx 服务架构 模块化结构

nginx 服务器的开发完全遵循模块化设计思想

模块化开发 单一职责原则,一个模块只负责一个功能 将程序分解,自顶向下,逐步求精 高内聚,低耦合 nginx 的模块化结构 核心模块:nginx 最基本最核心的服务,如进程管理、权限控制、日志记录; 标准 HTTP 模块:nginx 服务器的标准 HTTP 功能; 可选 HTTP 模块:处理特殊的 HTTP 请求 邮件服务模块:邮件服务 第三方模块:作为扩展,完成特殊功能 nginx 的模块清单

核心模块

ngx_core ngx_errlog ngx_conf ngx_events ngx_event_core ngx_epll ngx_regex

标准 HTTP 模块

ngx_http ngx_http_core #配置端口,URI 分析,服务器相应错误处理,别名控制 (alias) 等 ngx_http_log #自定义 access 日志 ngx_http_upstream #定义一组服务器,可以接受来自 proxy, Fastcgi,Memcache 的重定向;主要用作负载均衡 ngx_http_static ngx_http_autoindex #自动生成目录列表 ngx_http_index #处理以/结尾的请求,如果没有找到 index 页,则看是否开启了random_index;如开启,则用之,否则用 autoindex ngx_http_auth_basic #基于 http 的身份认证 (auth_basic) ngx_http_access #基于 IP 地址的访问控制 (deny,allow) ngx_http_limit_conn #限制来自客户端的连接的响应和处理速率 ngx_http_limit_req #限制来自客户端的请求的响应和处理速率 ngx_http_geo ngx_http_map #创建任意的键值对变量 ngx_http_split_clients ngx_http_referer #过滤 HTTP 头中 Referer 为空的对象 ngx_http_rewrite #通过正则表达式重定向请求 ngx_http_proxy ngx_http_fastcgi #支持 fastcgi ngx_http_uwsgi ngx_http_scgi ngx_http_memcached ngx_http_empty_gif #从内存创建一个 1×1 的透明 gif 图片,可以快速调用 ngx_http_browser #解析 http 请求头部的 User-Agent 值 ngx_http_charset #指定网页编码 ngx_http_upstream_ip_hash ngx_http_upstream_least_conn ngx_http_upstream_keepalive ngx_http_write_filter ngx_http_header_filter ngx_http_chunked_filter ngx_http_range_header ngx_http_gzip_filter ngx_http_postpone_filter ngx_http_ssi_filter ngx_http_charset_filter ngx_http_userid_filter ngx_http_headers_filter #设置 http 响应头 ngx_http_copy_filter ngx_http_range_body_filter ngx_http_not_modified_filter

可选 HTTP 模块

ngx_http_addition #在响应请求的页面开始或者结尾添加文本信息 ngx_http_degradation #在低内存的情况下允许服务器返回 444 或者 204 错误 ngx_http_perl ngx_http_flv #支持将 Flash 多媒体信息按照流文件传输,可以根据客户端指定的开始位置返回 Flash ngx_http_geoip #支持解析基于 GeoIP 数据库的客户端请求 ngx_google_perftools ngx_http_gzip #gzip 压缩请求的响应 ngx_http_gzip_static #搜索并使用预压缩的以.gz 为后缀的文件代替一般文件响应客户端请求 ngx_http_image_filter #支持改变 png,jpeg,gif 图片的尺寸和旋转方向 ngx_http_mp4 #支持.mp4,.m4v,.m4a 等多媒体信息按照流文件传输,常与 ngx_http_flv 一起使用 ngx_http_random_index #当收到 / 结尾的请求时,在指定目录下随机选择一个文件作为 index ngx_http_secure_link #支持对请求链接的有效性检查 ngx_http_ssl #支持 https ngx_http_stub_status ngx_http_sub_module #使用指定的字符串替换响应中的信息 ngx_http_dav #支持 HTTP 和 WebDAV 协议中的 PUT/DELETE/MKCOL/COPY/MOVE 方法 ngx_http_xslt #将 XML 响应信息使用 XSLT 进行转换

邮件服务模块

ngx_mail_core ngx_mail_pop3 ngx_mail_imap ngx_mail_smtp ngx_mail_auth_http ngx_mail_proxy ngx_mail_ssl

第三方模块

echo-nginx-module #支持在 nginx 配置文件中使用 echo/sleep/time/exec 等类 Shell 命令 memc-nginx-module rds-json-nginx-module #使 nginx 支持 json 数据的处理 lua-nginx-module nginx 的 web 请求处理机制

作为服务器软件,必须具备并行处理多个客户端的请求的能力, 工作方式主要以下 3 种:

多进程 (Apache) 优点:设计和实现简单;子进程独立 缺点:生成一个子进程要内存复制,在资源和时间上造成额外开销 多线程 (IIS) 优点:开销小 缺点:开发者自己要对内存进行管理;线程之间会相互影响 异步方式 (nginx)

经常说道异步非阻塞这个概念, 包含两层含义:

通信模式: + 同步:发送方发送完请求后,等待并接受对方的回应后,再发送下个请求 + 异步:发送方发送完请求后,不必等待,直接发送下个请求

nginx 配置文件实例 #定义 nginx 运行的用户和用户组 user www www; #nginx 进程数,建议设置为等于 CPU 总核心数。 worker_processes 8; #nginx 默认没有开启利用多核 CPU, 通过增加 worker_cpu_affinity 配置参数来充分利用多核 CPU 以下是 8 核的配置参数 worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ] error_log /var/log/nginx/error.log info; #进程文件 pid /var/run/nginx.pid; #一个 nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值 ulimit -n)与 nginx 进程数相除,但是 nginx 分配请求并不均匀,所以建议与 ulimit -n 的值保持一致。 worker_rlimit_nofile 65535; #工作模式与连接数上限 events { #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll 模型是 Linux 2.6 以上版本内核中的高性能网络 I/O 模型,如果跑在 FreeBSD 上面,就用 kqueue 模型。 #epoll 是多路复用 IO(I/O Multiplexing) 中的一种方式,但是仅用于 linux2.6 以上内核,可以大大提高 nginx 的性能 use epoll; ############################################################################ #单个后台 worker process 进程的最大并发链接数 #事件模块指令,定义 nginx 每个进程最大连接数,默认 1024。最大客户连接数由 worker_processes 和 worker_connections 决定 #即 max_client=worker_processes*worker_connections, 在作为反向代理时:max_client=worker_processes*worker_connections / 4 worker_connections 65535; ############################################################################ } #设定 http 服务器 http { include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream; #默认文件类型 #charset utf-8; #默认编码 server_names_hash_bucket_size 128; #服务器名字的 hash 表大小 client_header_buffer_size 32k; #上传文件大小限制 large_client_header_buffers 4 64k; #设定请求缓 client_max_body_size 8m; #设定请求缓 sendfile on; #开启高效文件传输模式,sendfile 指令指定 nginx 是否调用 sendfile 函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成 off。 autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。 tcp_nopush on; #防止网络阻塞 tcp_nodelay on; #防止网络阻塞 ##连接客户端超时时间各种参数设置## keepalive_timeout 120; #单位是秒,客户端连接时时间,超时之后服务器端自动关闭该连接 如果 nginx 守护进程在这个等待的时间里,一直没有收到浏览发过来 http 请求,则关闭这个 http 连接 client_header_timeout 10; #客户端请求头的超时时间 client_body_timeout 10; #客户端请求主体超时时间 reset_timedout_connection on; #告诉 nginx 关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间 send_timeout 10; #客户端响应超时时间,在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx 就会关闭连接 ################################ #FastCGI 相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; ###作为代理缓存服务器设置####### ###先写到 temp 再移动到 cache #proxy_cache_path /var/tmp/nginx/proxy_cache levels=1:2 keys_zone=cache_one:512m inactive=10m max_size=64m; ###以上 proxy_temp 和 proxy_cache 需要在同一个分区中 ###levels=1:2 表示缓存级别,表示缓存目录的第一级目录是 1 个字符,第二级目录是 2 个字符 keys_zone=cache_one:128m 缓存空间起名为 cache_one 大小为 512m ###max_size=64m 表示单个文件超过 128m 就不缓存了 inactive=10m 表示缓存的数据,10 分钟内没有被访问过就删除 ########


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3