Linux安装、配置nginx
nginx的介绍
Nginx是俄罗斯人 Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的一个十分轻量级的HTTP服务器。
它是一个高性能的HTTP和反向代理服务器,
同时也可以作为IMAP/POP3/SMTP的代理服务器。
Nginx使用的是BSD许可。
Nginx 以事件驱动的方式编写,
所以有非常好的性能,
同时也是一个非常高效的反向代理、负载平衡。
Nginx 因为它的稳定性、丰富的模块库、灵活的配置和低系统资源的消耗而闻名。
Nginx适合用来做mongrel clusters 的前端 HTTP 响应。
nginx的特点
- 核心特点:高并发请求的同时保持高效的服务
- 热部署
- 低内存消耗
- 处理响应请求很快
- 具有很高的可靠性
- 同时,nginx也可以实现高效的反向代理、负载均衡。
前端可以用nginx做些什么?
- 搭建静态资源服务器
- 反向代理分发后端服务和跨域问题
- 根据User Agent来重定向站点
- 开发环境或测试环境切换(切换host)
- url重写,使用rewrie规则本地映射
- 资源内容篡改
- 获取cookie做分流
- 资源合并
- gzip压缩
- 压缩图片
- sourceMap调试
本文主要分享在配置项目服务实战中对nginx的使用
除了配置以外还注明了一些报错的解决和避免采坑的提示等
服务器厂商:阿里云
操作系统: CentOS 7.6
操作终端: Mac bash
安装nginx之前
可以一键安装下面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
1. gcc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。
2. pcre pcre-devel
Nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法。
这里需要安装两个安装包pcre和pcre-devel。
第一个安装包提供编译版本的库,而第二个提供开发阶段的头文件和编译项目的源代码。
3. zlib zlib-devel
zlib库提供了开发人员的压缩算法,在Nginx的各种模块中需要使用gzip压缩。
4. openssl openssl-devel
nginx不仅支持 http协议,还支持 https(即在 ssl 协议上传输 http),如果使用了 https,需要安装 OpenSSL 库。
我这里采用按需安装依赖
先按正常程序走
走不动了报错了再根据实际情况安装相应依赖
这样搞自己走的每一步就会比较明白
1.下载并解压安装包
//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
//解压 tar包
tar -xvf nginx-1.13.7.tar.gz
可在 nginx 官网查找所有版本: http://nginx.org/download/
官网也对 最新稳定版 进行了分类: http://nginx.org/en/download.html
2.安装nginx
//进入nginx目录
cd /usr/local/nginx
//执行命令
./configure
//执行make命令 它从Makefile中读取指令,然后编译
make
//执行make install命令 从Makefile中读取指令,安装到指定的位置
make install
configure 会在你的系统上 测试 存在的特性(或者bug!)。为了加速随后进行的配置,测试的结果会存储在一个cache file(缓存文件)里。当configure一个每个子树里都有’configure’脚本的复杂的源码树时,一个很好的cache file的存在会有很大帮助。
执行 ./configure 命令一般时会报错,原因时缺少依赖,可根据报错内容安装相应依赖
关于configure更详细的说明可参考: ./configure的配置和用法
报错1
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决方案: 安装 PCRE 依赖
yum -y install pcre-devel
报错2
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决方案: 安装 zlib 依赖
yum -y install zlib zlib-devel
3.配置config文件
# 打开配置文件
vim /usr/local/nginx/conf/nginx.conf
nginx.conf
#user nobody;
# 工作进程数 默认1 如果CPU是双核4线程 可以设置为4
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# worker_connections 单个工作进程可以允许同时建立外部连接的数量
events {
worker_connections 1024;
}
http {
# 文件扩展名与文件类型映射表
include mime.types;
# 默认文件类型
default_type application/octet-stream;
# 通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了
# 通过$remote_add拿到的IP地址是反向代理服务器的iP地址。
# 反向代理服务器在转发请求的http头信息中
# 可以增加x_forwarded_for信息
# 用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
#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超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# upstream 对应 proxy_pass
# 负载均衡配置 多个server
# weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况
# max_conns 可以根据服务的好坏来设置最大连接数,防止挂掉,比如1000,我们可以设置800
# 也可根据接口服务器个数配置多个 upstream
upstream shimh.api1.com {
server 39.107.228.66 weight=4 max_conns=800;
server 39.107.229.66 weight=2;
}
upstream shimh.api2.com {
server 39.108.118.66:8080;
server 39.108.118.66:8090;
}
server {
listen 80;
# 这里可以配置自定义域名 前端需要配置host
# 要不然只能localhost打开,远程服务器只能使用你购买的域名打开
# 阿里云服务器中如果自定义的域名不进行备案,过不了多长时间就不能用了,本地服务没问题
server_name www.shimh01.com;
location ^~/dev/ {
proxy_pass http://shimh.api1.com;
}
location / {
#表示服务器文件根目录 项目放在此目录下面
root html/project1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.shimh02.com;
location ^~/dev/ {
proxy_pass http://shimh.api2.com;
}
location / {
root html/project2;
index index.html index.htm;
}
}
}
切记: 配置的每条语句结尾一定要加分号,否则执行时将会报错
关于 负载均衡 的详细的说明: 负载均衡演示之 upstream、 location 参数
关于 工作进程 和 连接 的详细说明: worker_processes、worker_connections设置
以下命令应当在nginx安装目录sbin下输入
// 例如在 sbin 下执行 重启nginx服务
// 注意 nginx 命令需要指定到路径
// 如下 写成 ./nginx
./nginx -s reload
3.启动
nginx
4.重启
nginx -s reload
5.退出
nginx -s quit
6.关闭(停止)
nginx -s stop
7.测试nginx配置文件是否正确
nginx -t -c /usr/local/etc/nginx/nginx.conf
报错1
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
解决办法: 执行以下命令即可修复
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
作用:
使用nginx -c的参数指定nginx.conf文件的位置
报错2
nginx: [emerg] still could not bind()
原因
定义的端口或域名被占用
解决办法
调整端口域名后就不报错了
我实际遇到一个问题就是这个时候不报错了也打不开网页
原因是我之前在服务器中配置了pm2
如果你不手动关掉它自己时永远不会关的
执行: pm2 stop all 关闭所有 pm2配置的服务
再次reload就能成功打开网址了
mac报错
以上报错是权限问题
使用mac操作会出现这个错误
命令前面要加 sudo
最后输入开机密码就可以了
这里推荐一个比较详细的config文件中文详解: Nginx配置文件nginx.conf中文详解