一:nginx简介
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server.
Nginx (engine x) 是一款轻量级的Web服务器 、反向代理服务器、电子邮件(IMAP/POP3)代理服务器、通用TCP/UDP代理服务器、也可以作为静态服务器,提供静态内容服务(静态网站)。
Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性,处理高性能和高效率的并发一直是部署nginx的主要优势。
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.
作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器)。
应用场景
反向代理
负载均衡
HTTP服务器
动静分离
正向代理
nginx 架构图
nginx有一个主进程(master process)和几个工作进程(worker process)。 主进程的主要目的是读取和评估配置,并维护工作进程。 工作进程对请求进行实际处理。
nginx采用基于事件的模型和依赖于操作系统的机制来有效地在工作进程之间分配请求。 工作进程的数量可在配置文件中定义,并且可以针对给定的配置进行修改,或者自动调整到可用CPU内核的数量
二:MacOS安装nginx
1. 查看nginx是否已经安装
brew update// 查询nginx是否已经安装过brew search nginx// 查看nginx的信息brew info nginx
注意:看一下Dependencies,Required的openssl和pcre必须打对号,如果是错号就先把这两个安装上
安装nginx前需要先安装openssl
2. 安装openssl
brew install opensslbrew upgrade opensslbrew link openssl --force
安装成功后再运行brew info nginx,查看openssl为对号
3. 安装pcre
安装:https://blog.csdn.net/tuibian001/article/details/6359200
4. 更新nginx
安装成功后再运行brew info nginx,查看pcre还是错号,不知道为什么
这时候直接安装nginx, 会报错已经存在,然后直接更新nginx,运行brew upgrade nginx 发现
在更新的过程中也会安装pcre, 更新成功后再查看nginx信息brew info nginx,结果发现必要的依赖pcre后面也是对号了。
// 安装nginxbrew install nginx// 一般mac本身都会安装nginx,此时直接更新就可以了,更新时如果没brew upgrade nginx
5. nginx信息
通过brew info nginx 可以得知一下内容
当前版本号
nginx的简介 HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server (主要三个功能,http(s)服务/反向代理/邮件代理服务)
nginx的安装目录
文档根目录Docroot /usr/local/var/www
配置文件位置 /usr/local/etc/nginx/nginx.conf
启动nginx命令,分后台启动brew services start nginx 前台启动nginx两种方式,自己玩建议使用前台方式,不要装成服务
三:nginx命令
启动nginx
# 方式一:直接使用默认参数启动 # 如果没有权限启动则使用sudo nginx 来启动 nginx # 方式二:指定一些配置参数,如果配置文件位置 nginx -c /usr/local/etc/nginx/nginx.conf # 方式三: 使用brew brew services start nginx
停止nginx:
nginx -s stop :快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。 nginx -s quit :平稳关闭Nginx,保存相关信息,有安排的结束web服务。 其他的停止nginx 方式: ps -ef | grep nginx kill -QUIT 主进程号 :从容停止Nginx kill -TERM 主进程号 :快速停止Nginx kill -HUP 主进程号 :平滑关闭nginx pkill -9 nginx :强制停止Nginx
其它
nginx -s reload :重新加载配置文件,nginx支持热启动,即修改了配置文件运行该命令既可是该配置文件生效,而无需重启nginx nginx -s reopen :重新打开日志文件 nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确 nginx -v :显示 nginx 的版本。 nginx -V :显示 nginx 的版本,编译器版本和配置参数。
启动成功后访问localhost,既可以看到nginx的欢迎页面
四:配置文件简介
nginx中有一个nginx.conf文件用于配置nginx,可以通过brew info nginx查看配置文件的位置:/usr/local/etc/nginx/nginx.conf
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { 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"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include servers/*; }
配置文件的结构
nginx由配置文件中指定的指令控制的模块组成。 指令分为简单指令和块指令。 配置文件使用#号作为注释
简单指令: 由空格分隔的名称和参数组成,并以分号(;)结尾,如:
listen 80;
块指令: 具有与简单指令相同的结构,但不是以分号结尾,而是以大括号({和})包围的一组附加指令结束。 如果块指令可以在大括号内部有其他指令,则称为上下文(例如:events,http,server和location)。
http { server { listen 80; server_name localhost; }}
几个顶级指令(称为上下文)将适用于不同流量类型的指令组合在一起:
events : 一般连接处理
http : HTTP协议流量
mail : Mail协议流量
stream : TCP协议流量