Nginx
下载安装
官方网站:https://nginx.org/en/download.html
windows安装
下载后随便拷贝到一个目录解压 解压的目录不能有中文
windows启动
1) 直接双击该目录下的"nginx.exe",即可启动nginx服务器;
2) 命令行进入该文件夹,执行start nginx命令,也会直接启动nginx服务器。
检验是否成功
浏览器访问 http://localhost 弹出一个有黑色大字的页面就是成功了
Nginx Windows基本操作指
启动服务:start nginx
退出服务:nginx -s quit
强制关闭服务:nginx -s stop
重载服务:nginx -s reload (重载服务配置文件,类似于重启,服务不会中止)
验证配置文件:nginx -t
使用配置文件:nginx -c "配置文件路径"
使用帮助:nginx -h
Linux下安装
Nginx依赖包
模块依赖性Nginx需要依赖下面3个包
- ssl功能需要 openssl 库 ( http://www.openssl.org/ )
- gzip模块需要 zlib 库 ( http://www.zlib.net/ )
- rewrite模块需要 pcre 库 ( http://www.pcre.org/ )
依赖包安装顺序依次为:openssl、zlib、pcre, 最后安装Nginx包。
安装教程
- 下载所需包
openssl-fips-2.0.2.tar.gz
zlib-1.2.7.tar.gz
pcre-8.21.tar.gz
nginx-1.12.2.tar.gz
- 安装OpenSSL
[root@localhost wcw]# tar -zxvf openssl-fips-2.0.2.tar.gz
[root@localhost wcw]# cd openssl-fips-2.0.2
[root@localhost openssl-fips-2.0.2]# ./config
[root@localhost openssl-fips-2.0.2]# make
[root@localhost openssl-fips-2.0.2]# make install
- 安装zlib
[root@localhost wcw]# tar -zxvf zlib-1.2.7.tar.gz
[root@localhost wcw]# cd zlib-1.2.7
[root@localhost zlib-1.2.7]# ./configure
[root@localhost zlib-1.2.7]# make
[root@localhost zlib-1.2.7]# make install
- 安装pcre
[root@localhost wcw]# tar -zxvf pcre-8.21.tar.gz
[root@localhost wcw]# cd pcre-8.21
[root@localhost pcre-8.21]# ./configure
[root@localhost pcre-8.21]# make
[root@localhost pcre-8.21]# make install
- 安装Nginx
[root@localhost wcw]# tar -zxvf nginx-1.12.2.tar.gz
[root@localhost wcw]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/install/nginx --with-pcre=../pcre-8.21 --with-zlib=../zlib-1.2.7 --with-openssl=../openssl-fips-2.0.2
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
请注意:"–with-xxx="的值是解压目录,而不是安装目录!
Nginx Linux基本操作指令
启动服务:nginx
退出服务:nginx -s quit
强制关闭服务:nginx -s stop
重载服务:nginx -s reload (重载服务配置文件,类似于重启,但服务不会中止)
验证配置文件:nginx -t
使用配置文件:nginx -c "配置文件路径"
使用帮助:nginx -h
此时可以为Nginx添加环境变量
修改 /etc/profile 文件,在文件末尾加上如下代码:
export PATH=$PATH:路径
最后,执行命令 source /etc/profile 使其修改生效,执行完可通过echo $PATH命令查看是否添加成功。
检测是否安装成功:
[root@localhost wcw]# nginx -t
Nginx Linux基本操作指令
启动服务:nginx
安全退出服务:nginx -s quit
强制关闭服务:nginx -s stop
重载服务:nginx -s reload (重载服务配置文件,类似于重启,但服务不会中止)
验证配置文件:nginx -t
使用配置文件:nginx -c "配置文件路径"
查看nginx进程:ps aux|grep nginx
使用帮助:nginx -h
Nginx的作用
Http代理:
反向代理和正向代理
负载均衡:
nginx提供的负载均衡策略有两种:内置策略和扩展策略,内置策略为轮询,加权轮询,ip,hash。
扩展策略,就天马行空
动静分离:
在软件开发中,有些请求是需要后台处理的,有些请求是不需要经过后台处理的(如:css,html,jpg,js等文件),这些不需要经过后台处理的文件称之为静态文件。让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,提高响应速度
Nginx配置语法
Nginx 的主配置文件是 安装目录/nginx/nginx.conf,你可以使用
cat -n nginx.conf` 来查看配置。
nginx.conf
结构图可以这样概括:
main # 全局配置,对全局生效
├── events # 配置影响 Nginx 服务器或与用户的网络连接
├── http # 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
│ ├── upstream # 配置后端服务器具体地址,负载均衡配置不可或缺的部分
│ ├── server # 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块
│ ├── server
│ │ ├── location # server 块可以包含多个 location 块,location 指令用于匹配 uri
│ │ ├── location
│ │ └── ...
│ └── ...
└── ...
一个 Nginx 配置文件的结构就像 nginx.conf 显示的那样,配置文件的语法规则:
- 配置文件由指令与指令块构成;
- 每条指令以 ; 分号结尾,指令与参数间以空格符号分隔;
- 指令块以 {} 大括号将多条指令组织在一起;
- include 语句允许组合多个配置文件以提升可维护性;
- 使用 # 符号添加注释,提高可读性;
- 使用 $ 符号使用变量;
- 部分指令的参数支持正则表达式;
典型配置
user nginx; # 运行用户,默认即是nginx,可以不进行设置
worker_processes 1; # Nginx 进程数,一般设置为和 CPU 核数一样
error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
events {
use epoll; # 使用epoll的I/O模型(如果你不知道Nginx该使用哪种轮询方法,会自动选择一个最适合你操作系统的)
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; # Nginx访问日志存放位置
sendfile on; # 开启高效传输模式
tcp_nopush on; # 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; # 保持连接的时间,也叫超时时间,单位秒
types_hash_max_size 2048;
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
include /etc/nginx/conf.d/*.conf; # 加载子配置项
server {
listen 80; # 配置监听的端口
server_name localhost; # 配置的域名
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认首页文件
deny 172.168.22.11; # 禁止访问的ip地址,可以为all
allow 172.168.33.44;# 允许访问的ip地址,可以为all
}
error_page 500 502 503 504 /50x.html; # 默认50x对应的访问页面
error_page 400 404 error.html; # 同上
}
}