口碑好网站建设价格低黄山seo
Nginx
- Nginx
- Nginx可以从事的用途
- Nginx安装
- Nginx自带常用命令
- Nginx启动
- Nginx停止
- Nginx重启
- Nginx配置概要
- 第一部分:全局块
- 第二部分:events 块:
- 第三部分:http块:
Nginx
Nginx是一个高性能的http和反向代理服务器,其特点是占用内存小,并发能力强。Nginx专为性能优化而开发,性能是其最重要的考量,能经受高负载的考验,有报告表明能支持高达50000个并发连接数。
Nginx可以从事的用途
web服务器:提供Web信息浏览服务。它只需支持HTTP协议、HTML文档格式及URL。
反向代理
正向代理:在浏览器中配置代理服务器,通过代理服务器进行互联网访问。
反向代理:将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,再返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴漏的是代理服务器地址。
负载均衡
如果请求数过大,单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器的情况改为请求分发到多个服务器上,就是负载均衡。
Nginx安装
CentOS
yum install -y nginx
Ubuntu
apt-get install -y nginx
当终端显示出Complete!字样时,则代表Nginx已经安装成功了。
Docker 方式安装(linux通用,需要提前安装docker):
docker container run \--rm \--name mynginx \--volume "$PWD/html":/usr/share/nginx/html \ --volume "$PWD/conf":/etc/nginx \-p 80:80 \-d \nginx
以上参数含义:
-d:在后台运行
-p :容器的80端口映射到本地80端口,格式为本地端口:容器端口
--rm:容器停止运行后,自动删除容器文件
--name:容器的名字为mynginx
--volume "$PWD/conf":/etc/nginx 表示把容器的配置目录/etc/nginx,映射到本地的conf子目录。
Nginx自带常用命令
nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。
nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。
nginx -s reopen 重新打开日志文件。
nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。
nginx -t 不运行,仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开
配置文件中所引用到的文件。
nginx -v 显示 nginx 的版本。
nginx -V 显示 nginx 的版本,编译器版本和配置参数。
Nginx启动
查看Nginx服务状态
systemctl status nginx
执行该命令后,可以看到,初始状态,Nginx是未启动的
在centos7+ 启动Nginx服务
systemctl start nginx
centos6+ 上启动Nginx服务
service nginx start
或者是直接输入命令
nginx
Nginx服务开启后,默认使用的是80端口,如需要修改,可在配置文件中自定义配置
Nginx停止
在centos7+ 停止Nginx服务
systemctl stop nginx
centos6+ 上停止Nginx服务
service nginx stop
使用Nginx命令停止服务
nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。
Nginx重启
修改了Nginx的某些配置,为了使配置生效,往往需要重启Nginx
在centos7+ 重启Nginx服务
systemctl restart nginx
centos6+ 上重启Nginx服务
service nginx restart
使用Nginx命令重启服务
nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。
具体使用nginx原生命令操作还是linux提供的systemctl ,这个主要看个人喜好,实际两者的功能是差不多的,并没有什么明显的不同
Nginx配置概要
nginx本身作为一个完成度非常高的负载均衡框架,和很多成熟的开源框架一样,大多数功能都可以通过修改配置文件来完成,使用者只需要简单修改一下nginx配置文件,便可以非常轻松的实现比如反向代理,负载均衡这些常用的功能,同样的,和其他开源框架比如tomcat一样,nginx配置文件也遵循着相应的格式规范,并不能一顿乱配,在了解如何使用nginx实现反向代理,负载均衡等这些功能的配置前,需要先了解一下nginx配置文件的结构
既然要了解nginx的配置文件,需知道nginx配置文件所在位置,nginx配置文件默认都放在nginx安装路径下的conf目录,而主配置文件nginx.conf自然也在这里面
如果不知道nginx装哪了,可以运行nginx -t
命令,下面除了给出nginx配置文件是否OK外,同时也包括了配置文件的路径。
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
配置文件位置:
/etc/nginx/nginx.conf # 主配置文件
/etc/nginx/conf.d/*.conf # 次配置文件
/etc/nginx/default.d/*.conf # 次配置文件
nginx配置文件不一定只有主配置文件一个,还可能在/etc/nginx/conf.d/
和/etc/nginx/default.d
目录下会有其他的次配置文件
使用 cat 命令或 vim 命令查看主配置文件:cat /etc/nginx/nginx.conf
,不同版本的配置文件可能稍有不同
user nginx;
worker_processes auto;
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;events {worker_connections 1024;
}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 4096;include /etc/nginx/mime.types;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;server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
从include /usr/share/nginx/modules/*.conf
、 include /etc/nginx/conf.d/*.conf
和 include /etc/nginx/default.d/*.conf
这几行带include
的代码中可以得知一些次配置文件所在的位置。可以发现,这几块次配置文件所处的作用域不同
按照功能划分,我们通常将nginx配置文件分为三大块,全局块,events块,http块。
# 全局块
...
# events块
events { ...
}
# http块
http
{# http全局块... # 虚拟主机server块server { # server全局块... # location块location [PATTERN] {...}location [PATTERN] {...}}server{...}# http全局块...
}
第一部分:全局块
首先映入眼帘的这一堆:
user nginx; # 用户(组)
worker_processes auto; # 工作的进程以及允许生成的 worker process 数
error_log /var/log/nginx/error.log; # 错误日志存放的路径
pid /run/nginx.pid; # 进程 PID# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; # 配置文件的引入
称之为全局块,这里主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、工作的进程以及允许生成的 worker process 数,进程 PID 、存放路径、日志存放路径和类型以及配置文件的引入等。
比如 worker_processes auto
; 这一行,worker_processes 值越大,nginx可支持的并发数量就越多,很多人想这不就爽了吗,我设置成正无穷,无限并发flag达成,秒杀问题轻松解决,这个,受自己服务器硬件限制的,不能乱来。设置auto表示会自动根据当前cpu的核心数来分配worker,规则是1个核心1个worker.
第二部分:events 块:
events {worker_connections 1024;
}
这一堆,就是我们配置文件的第二部分,events 块
events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 work process 可以同时支持的最大连接数等
第三部分: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 4096; # hash字节块的最大值# 引入的多媒体类型include /etc/nginx/mime.types;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;# server块server {# listen监听指令listen 80;listen [::]:80;server_name _;# 静态文件所在的根目录root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
http是一个大块,里面也可以包括很多小块,比如http全局块,server块等。
http全局块 配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
而http块中的server块则相当于一个虚拟主机,一个http块可以拥有多个server块。
server块 又包括全局server块,和location块。
全局server块主要包括了本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置
location块则用来对虚拟主机名称之外的字符串进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。比如对/usl
相关的请求交给8080来处理,/admin
则较给8081处理。