Nginx初识
-
master:配置文件分析和加载、管理worker、平滑升级;
-
worker:处理用户请求;
-
cache loader, cache manager:缓存加载和缓存管理
-
nginx的其它的二次发行版:tengine、OpenResty
-
Nginx模块:
- 核心模块:core module
- 标准模块:
- HTTP modules:
- Standard HTTP modules
- Optional HTTP modules
- Mail modules
- Stream modules:传输层代理
- HTTP modules:
- 第三方模块
-
负载均衡比较:
- 传输层:lvs、nginx、haproxy
- 应用层:nginx(http, https, smtp, pop, imap), haproxy(http), httpd(http/https), ats, perlbal, pound, …
-
Nginx作用:
- 静态的web资源服务器;(图片服务器,或js/css/html/txt等静态资源服务器)
- 结合FastCGI/uwSGI/SCGI等协议反代动态资源请求;
- http/https协议的反向代理;
- IMAP4/pop3协议的反向代理;
- tcp/udp协议的请求转发;
-
安装:
- yum安装:http://nginx.org/en/linux_packages.html#RHEL-CentOS
- 编译安装:
-
前提:开发环境,包括nginx编译要启用的功能依赖到的开发库;
# yum groupinstall "Development Tools" "Server Platform Development" # yum -y pcre-devel openssl-devel
-
编译过程:
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-threads --with-file-aio # make && make install
-
-
Nginx配置文件:
# 用户 user www www; # 启动进程数 worker_processes auto; # 错误日志 error_log logs/error.log; # pid文件 pid logs/nginx.pid; # 进程绑定CPU worker_cpu_affinity auto; # 设置优先级 worker_priority -5; # 允许打开的最大文件数 worker_rlimit_nofile 65535; # 最大链接数 events { worker_connections 1024; } http { # 各个server共享的配置 # mime信息,包含哪些mime协议 include mime.types; # 默认把文件识别成八进制数据流 default_type application/octet-stream; # 日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # 配置文件目录 include /etc/nginx/conf.d/*.conf; # 日志路径 access_log /var/log/nginx/logs/access.log; error_log /var/log/nginx/logs/error.log; # 控制网络连接参数: # 发送文件 sendfile on; #tcp_nopush on; # 保持连接超时65s keepalive_timeout 65; #gzip on; upstream helpdesk { server unix:///data/helpdesk-master/desk_demo.sock; } # 虚拟主机 server { listen 80; # 默认虚拟主机,当没有匹配项是使用 # listen 80 default_server; # 主机名 server_name 10.126.3.193; # 默认网页跟路径 root /usr/share/nginx/html; #charset koi8-r; charset utf-8; # max upload size client_max_body_size 75M; # Django media location /media { alias /data/helpdesk-master/media; # 指向django的media目录 } location /static { alias /data/helpdesk-master/static; # 指向django的static目录 } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass helpdesk; include uwsgi_params; # the uwsgi_params file you installed } # 自定义错误页 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
- 详细配置说明:
-
每项配置以分号;结尾
-
main配置段常见的配置指令:
分类:
1. 正常运行必备的配置
1) user user [group];
2) pid /PATH/TO/PID_FILE; # 指定存储nginx主进程号码的文件路径
3) include file | mask; # 指明包含进来的其他配置文件片段
4) load_module file; # 指明要装载的动态模块
2. 优化性能相关的配置
1) worker_processes number | auto;
###### number:worker进程的数量,通常应该 <= 当前主机的CPU的物理核心数;auto:当前主机物理CPU核心数;
2) worker_cpu_affinity auto | cpumask;
###### nginx进程与CPU绑定,如果不绑定进程使用的CPU会随着访问进行跳转。auto:自动;cpumask:CPU位掩码(00000001表示0号CPU,00000010表示1号CPU)
###### 查看nginx运行在第几个CPU:ps axo comm,pid,psr | grep nginx
3) worker_prilrity number; # 指定worker进程的nice值,设定worker进程优先级;[-20,20]
4) worker_rlimit_nofile number; # worker进程所能够打开的文件数量上限
3
-
- 详细配置说明: