官方:http://nginx.org/ 参考:https://blog.csdn.net/qq_29677867/article/details/90112120
1. 介绍
Nginx (engine x) 是一个高性能、轻量级的反向代理web服务器,同时也提供了电子邮件IMAP/POP3/SMTP服务。它具备内存占用少,并发能力强的特点,在BSD-like 协议下发行。Nginx是由伊戈尔·赛索耶夫开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx可以提供的服务:
- web 服务
- 负载均衡 (反向代理)
- web cache(web 缓存)
Nginx 的优点:
- 高并发,适合小静态文件
- 占用资源少,2万并发、10个线程,内存消耗几百M。
- 支持epoll模型,使得nginx可以支持高并发
- nginx 配合动态服务和Apache有区别(FASTCGI 接口)
- 利用nginx可以对IP限速,可以限制连接数
- 配置简单,更灵活
Nginx 的应用场合:
- 静态服务器。(图片,视频服务)另一个 lighttpd。并发几万,html,js,css,flv,jpg,gif等
- 动态服务,nginx——FASTCGI 的方式运行PHP,jsp。(PHP并发在500-1500,MySQL 并发在300-1500)
- 反向代理,负载均衡。日pv2000W以下,都可以直接用nginx做代理
- 缓存服务。类似 SQUID,VARNISH
2. 安装和配置
2.1 安装
windows
ubuntu
#### 1 安装包sudo apt-get install nginx#### 2 查看nginx -v#### 3 启动服务service nginx start#------------------------------#### 卸载 nginxapt-get --purge autoremove nginx
服务启动之后,使用浏览器访问本地链接 localhost 查看欢迎界面!
2.2 配置
核心配置文件路径:nginx/conf/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.htmlerror_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;# }#}}
nginx.conf 文件主要分为三个部分: 全局块、events 块 和 http 块 参考链接
2.2.1 全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。
worker_processes 1;
这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。
2.2.1 events 块
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
events {worker_connections 1024;}
上述例子就表示每个 work process 支持的最大连接数为 1024.这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
2.2.1 http 块
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http 块也可以包括 http 全局块、server 块。**
2.2.1.1 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;
http 全局块配置的指令包括:
- 文件引入
- MIME-TYPE 定义
- 日志自定义
- 连接超时时间
- 单链接请求数上限等
2.2.1.1 server 块
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;}}
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。详情如下:
- 全局 server 块中最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置;
- 一个 server 块可以配置多个 location 块,location 块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
3. 反向代理
正向代理是指一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。
反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率。
简单来说:正向代理是客户端的代理,代替客户端访问服务,因此对于服务端来说,不知道真正的访问者是谁,应用场景有 翻墙/VPN 等。而反向代理是服务端的代理,代替服务端向客户端“提供”服务,因此客户端并不知道真正访问的是哪台服务器,对于客户端来说,反向代理服务器是透明的,因此反向代理常常和负载均衡共存。
https://www.yuque.com/huluwa-txtkw/rw1mgt/wrbzgy
