前言
最近工作中经常用到nginx,用起来很顺手,想着就在家搭建一下,用于日常学习和研究,现在我就把整个过程写出来给大家参考。
一、准备工作
- windows系统:多说无益;我这里是64位。
- Linux系统:没有Linux的同学可以下载,地址:http://mirrors.gigenet.com/centos/7/isos/x86_64/
这下面有很多版本,具体区别可以百度, 下载CentOS-7-x86_64-DVD-2003.iso,网速快的同学最好下载CentOS-7-x86_64-Everything-2003.iso,DVD版本还是缺不少package,不方便。这些都是无界面的,为了学习,还是先熟悉命令行模式。 - nginx:http://nginx.org/en/download.html
我这里选择了1.16.1版本,前面是linux版本,后面是Windows版本
- 虚拟机VMware:安装CentOS
https://my.vmware.com/cn/web/vmware/downloads
- Xshell 6、Xftp6:虚拟器操作确实不方便,还是本地ssh->CentOS、ftp->CentOS香
https://www.netsarang.com/zh/xshell-download/
填写姓名、邮箱获得评估版本下载地址(当然可以下载其他软件)
二、Linux(CentOS)下搭建
- 安装WMware,双击运行即可;
- 新建虚拟机
一直下一步即可,开始安装…
完成安装后,录入root密码,为了好记,密码也为root
注意:在VMware检查网络适配器是否为“NAT”,安装时默认是这个。
- 检查CentOS是已安装ftpserver(vsftpd和vsftpd)
[root@192 ~]# yum list *vsftp*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.ustc.edu.cn
Available Packages
vsftpd.x86_64 3.0.2-27.el7 base
vsftpd-sysvinit.x86_64 3.0.2-27.el7 base
[root@192 ~]#
如果未安装,可以使用yum安装:至于yum是什么,可以百度rpm、yum
[root@192 ~]# yum isntall vsftpd
[root@192 ~]# yum install vsftpd-sysvinit
若yum安装错误如下图:先确认VMware虚拟机网络适配器是否为NAT模式,若否,改为NAT,继续安装,若无法解决,可以登录CentOS网络配置查看原因。
命令模式进入CentOS网络配置
[root@192 ~]# nmtui
回车为确定
TAB为选择
上下键也可以选择
默认选中的是Edit a connection,回车
继续回车
Automatically connet:默认是空,我们TAB到【】内,按 空格 即可选中,然后TAB到OK,回车即可。
再次执行:
[root@192 ~]# yum isntall vsftpd
[root@192 ~]# yum install vsftpd-sysvinit
连上网络后,我们就可以使用yum install xx安装任何东西,当然本帖子不再其他packeage
执行命令,获取CentOS ip地址:CentOS 7以后命令改为了 ip addr
3. 安装Xshell、Xftp,一直下一步即可。
Xshell连接:
文件->新建
录入用户名密码:root/root
登录成功:
Xftp连接:
文件->新建
录入ip、用户名、密码
连接成功:
- nginx配置
Xftp 上传nginx版本 至 /usr/local/nginx(新建目录)
解压当前文件夹
安装nginx
设置安装路径
[root@192 nginx]# ls
nginx-1.16.1 nginx-1.16.1.tar.gz
[root@192 nginx]# cd nginx-1.16.1
[root@192 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 3.10.0-1127.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
[root@192 nginx-1.16.1]#
报错,没有编译器,我们安装一个gcc编译器
[root@192 nginx-1.16.1]# yum -y install gcc gcc-c++ autoconf automake make
再次执行
[root@192 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
若报错如下,是因为nginx涉及https ssl安全,需要openssl支持,安装openssl解决
[root@192 nginx-1.16.1]# yum -y install openssl openssl-devel
再次执行
[root@192 nginx-1.16.1]#
[root@192 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
成功了
编译
[root@192 nginx-1.16.1]# make
安装
[root@192 nginx-1.16.1]# make install
make -f objs/Makefile install
make[1]: Entering directory `/usr/local/nginx/nginx-1.16.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
|| mv '/usr/local/nginx/sbin/nginx' \
'/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
|| mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
|| cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
'/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
'/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/local/nginx/nginx-1.16.1'
[root@192 nginx-1.16.1]#
大功告成,看一下完整目录:其中nginx-1.16.1.tar.gz是原始文件;nginx-1.16.1是我们操作的文件夹,最好不要删除,后续使用stock配置还需要再次使用。
现在nginx安装已经完成了
启动nginx
[root@192 nginx]# cd sbin
[root@192 sbin]# ./nginx
[root@192 sbin]# ps -ef | grep nginx
root 14908 1 0 09:27 ? 00:00:00 nginx: master process ./nginx
nobody 14909 14908 0 09:27 ? 00:00:00 nginx: worker process
root 14911 14807 0 09:27 pts/0 00:00:00 grep --color=auto nginx
[root@192 sbin]#
停止nginx
[root@192 nginx]# pkill -9 nginx
[root@192 nginx]# ps -ef | grep nginx
root 14906 14807 0 09:26 pts/0 00:00:00 grep --color=auto nginx
编辑配置文件
配置文件在cong/nginx.conf,我把学习的配置贴上来,里面有很多注释,比较易懂。
#user nobody; 工作时的进程数,设置值小于cpu的内核数
worker_processes 1;
error_log logs/error.log;
error_log logs/notice.log notice;
error_log logs/info.log info;
pid logs/nginx.pid;
# 最大连接数
events {
worker_connections 1024;
}
# stock通讯代理
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log logs/stock-access.log proxy ;
# 负载均衡
upstream test1 {
server 192.168.134.1:9992 weight=2;
# 9993优先;权重5>2
server 192.168.134.1:9993 weight=5;
# 不存在的ip地址,所以还是优先发到9993
server 192.168.134.100:9993 weight=9;
}
# 测试负载test1
server {
listen 2222;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass test1;
}
# 普通代理
server {
listen 1111;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass 127.0.0.1:9991;
#access_log logs/1111_access.log;
}
}
# http代理
http {
#设定mime类型(邮件支持类型),类型由mime.types文件定义
include mime.types;
default_type application/octet-stream;
# $remote_addr 表示客户端地址
# $remote_user http客户端请求nginx认证用户名
# $time_local Nginx的时间;通用日志格式下的本地时间。
# $request Request请求行,GET等方法http协议版本
# $status respoence返回状态码
# $body_bytes_sent 从服务端响应给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容
# $http_referer 记录从哪个页面链接访问过来的;http上一级页面分析,防盗链、用户行为分析
# $http_user_agent http头部信息,客户端访问设备
# $http_x_forwarded_for http请求携带的http信息
# $bytes_sent 发送给客户端的总字节数。
# $connection 连接的序列号。
# $connection_requests 当前通过一个连接获得的请求数量。
# $msec 日志写入时间。单位为秒,精度是毫秒。
# $pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
# $request_length 请求的长度(包括请求行,请求头和请求正文)。
# $request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
# $time_iso8601 ISO8601标准格式下的本地时间。
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/http_access.log main;
rewrite_log on;
# sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
# 必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
# 连接超时时间
keepalive_timeout 65;
tcp_nodelay on;
# 对网络传输的数据内容进行压缩,开关;
#gzip on;
# 负载均衡 服务器的集群
# weight-设定该服务的访问权重,权重越大被访问的几率越大,相应的压力就越大;
# down- 表示此server不参与负载;
# max_fails -允许请求失败的最大次数;当超过后返回proxy_next_upstream中定义的的错误;
# fail_timeout:请求失败后暂停访问的时限;
# backup:备用server;当其他机器全忙或宕机时,才会使用。
# 反向代理 负载均衡
upstream myServer {
server localhost:8080;
#server localhost:2222 weight=2;
server localhost:3333 down;
server localhost:4444 backup;
}
# 反向代理
server {
listen 80;
server_name localhost;
#server_name www.daniu.com;
#access_log logs/80_access.log;
#error_log logs/80_error.log;
#首页
#index index.html
# 设置url编码格式,解决参数中文乱码问题
charset utf-8;
#代理配置参数
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr;
location / {
proxy_pass http://myServer;
}
}
# HTTPS server
#server {
# listen 82 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 / {
# proxy_pass https://www.baidu.com;
# }
#}
}
检查配置是否合规
[root@192 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@192 sbin]#
如果报错:nginx unknown directive "stream"之类的错误,是因为linux nginx默认安装的时候没有加载stream模块,可以参考这位大神的帖子:
原文链接:https://blog.csdn.net/buzenmedi/article/details/104904727
使配置生效
[root@192 sbin]# ./nginx -s reload
[root@192 sbin]#
配置示例说明
访问linux本地的2222端口,自动代理至192.168.134.1(我的windows机器)的9992、9993或者192.168.134.100的9993端口,根据优先级应该优先代理至100的9993端口,但100这个ip请求不了,故代理至1的9993端口。
# 负载均衡 服务器的集群
# weight-设定该服务的访问权重,权重越大被访问的几率越大,相应的压力就越大;
# down- 表示此server不参与负载;
# max_fails -允许请求失败的最大次数;当超过后返回proxy_next_upstream中定义的的错误;
# fail_timeout:请求失败后暂停访问的时限;
# backup:备用server;当其他机器全忙或宕机时,才会使用。
upstream test1 {
server 192.168.134.1:9992 weight=2;
# 9993优先;权重5>2
server 192.168.134.1:9993 weight=5;
# 不存在的ip地址,所以还是优先发到9993
server 192.168.134.100:9993 weight=9;
}
# 测试负载test1
server {
listen 2222;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass test1;
}
三、Windows下搭建
1. nginx-1.16.1.zip 直接解压任意目录即可
2. 配置conf/ngxin.conf
windows和linux版本可以通用nginx配置文件,讲刚刚linux贴出的配置拷贝进来即可。
3. 启动
第一种:1、双击nginx.exe、
第二种:win+r,打开dos窗口,进入nginx目录
F:\nginx\nginx-1.16.1>start nginx
F:\nginx\nginx-1.16.1>tasklist | findstr nginx
nginx.exe 2556 Console 1 7,548 K
nginx.exe 6952 Console 1 7,908 K
F:\nginx\nginx-1.16.1>
4. 检查配置是否合规
windows版本已是完整版本,无须再按照stream模块
F:\nginx\nginx-1.16.1>nginx -t
nginx: the configuration file F:\nginx\nginx-1.16.1/conf/nginx.conf syntax is ok
nginx: configuration file F:\nginx\nginx-1.16.1/conf/nginx.conf test is successful
F:\nginx\nginx-1.16.1>
5. 使配置生效
F:\nginx\nginx-1.16.1>nginx -s reload
F:\nginx\nginx-1.16.1>
6. 配置示例说明
下面摘出来这段配置,是访问本机80端口,自动代理至本机8080端口,本机8080是我的vue项目登录画面。
原登录地址
使用80代理的效果,是一样的
# 负载均衡 服务器的集群
# weight-设定该服务的访问权重,权重越大被访问的几率越大,相应的压力就越大;
# down- 表示此server不参与负载;
# max_fails -允许请求失败的最大次数;当超过后返回proxy_next_upstream中定义的的错误;
# fail_timeout:请求失败后暂停访问的时限;
# backup:备用server;当其他机器全忙或宕机时,才会使用。
# 反向代理 负载均衡
upstream myServer {
server localhost:8080;
#server localhost:2222 weight=2;
server localhost:3333 down;
server localhost:4444 backup;
}
# 反向代理
server {
listen 80;
server_name localhost;
#server_name www.daniu.com;
#access_log logs/80_access.log;
#error_log logs/80_error.log;
#首页
#index index.html
# 设置url编码格式,解决参数中文乱码问题
charset utf-8;
#代理配置参数
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr;
location / {
proxy_pass http://myServer;
}
}
<完>