Ngix安装
官网地址,下载为源码,需要编译安装
http://nginx.org/
环境
1、需要安装gcc的环境。
yum install gcc-c++
2、第三方的开发包。
- PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
- zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
- openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
安装
第一步:把nginx的源码包上传到linux系统
第二步:解压缩
[root@localhost ~]# tar zxf nginx-x.x.x.tar.gz
第三步:使用configure命令创建一makeFile文件。
./configure
--prefix=/usr/local/nginx
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--with-http_gzip_static_module
--http-client-body-temp-path=/var/temp/nginx/client
--http-proxy-temp-path=/var/temp/nginx/proxy
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi
--http-scgi-temp-path=/var/temp/nginx/scgi
--with-http_ssl_module
注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
[root@localhost sbin]# mkdir /var/temp/nginx/client -p
第四步:make
第五步:make install #装在--prefix=/usr/local/nginx中
启动
进入sbin目录
启动命令
[root@localhost sbin]# ./sbin/nginx
关闭nginx:
[root@localhost sbin]# ./nginx -s stop
推荐使用:
[root@localhost sbin]# ./nginx -s quit
重启nginx:
1、先关闭后启动。
2、刷新配置文件:
[root@localhost sbin]# ./nginx -s reload
配置虚拟主机
就是在一台服务器启动多个网站。
如何区分不同的网站:
1、端口不同
2、域名不同
通过端口区分不同虚拟机
Nginx的配置文件
/usr/local/nginx/conf/nginx.conf
可以配置多个server,配置了多个虚拟主机。
重新加载配置文件
[root@localhost nginx]# sbin/nginx -s reload
通过域名区分虚拟主机
本地测试可以修改hosts文件。
修改window的hosts文件:(C:WindowsSystem32driversetc)
可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。
反向代理
反向代理服务器决定哪台服务器提供服务。
Nginx实现反向代理
两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。
两个域名是www.sian.com.cn和www.sohu.com
第一步:安装两个tomcat,分别运行在8080和8081端口。
第二步:启动两个tomcat。
第三步:反向代理服务器的配置
upstream tomcat1 { server 192.168.182.3:8080; #被代理的服务器节点 } server { listen 80; server_name www.sina.com.cn; location / { proxy_pass http://tomcat1; index index.html index.htm; } } upstream tomcat2 { server 192.168.182.3:8081; #被代理的服务器节点 } server { listen 80; server_name www.sohu.com; location / { proxy_pass http://tomcat2; index index.html index.htm; } }
负载均衡
如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。
upstream tomcat2 { server 192.168.182.3:8080; server 192.168.182.4:8080; }
可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1
upstream tomcat2 { server 192.168.182.3:8080; server 192.168.182.3:8080 weight=2; }
Nginx的高可用
keepalived+nginx实现主备 (七层)
F5 (四层,超过5w并发用)
lvs免费,实现F6百分之六十的性能
ps:Ultraedit->主页->FTP->从FTP打开,配置后点击连接可以直接编译服务器上的文件。
service管理nginx
RHEL6下面
在/etc/init.d下创建文件nginx
vi /etc/init.d/nginx
输入配置
nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。
保存后设置文件的执行权限
chmod a+x /etc/init.d/nginx
至此就可以通过下面指令控制启动停止
/etc/init.d/nginx start /etc/init.d/nginx stop
上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便。
先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx
加完这个之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx start service nginx stop service nginx restart
最后设置开机自动启动
chkconfig nginx on
使用systemctl管理nginx
RHEL7下面(CentOS7用这个)
在centos7上,源码安装之后的nginx无法使用systemctl管理,需要写配置文件。
cd /lib/systemd/system
vi nginx.service
在目录下新建nginx.service文件,内容为:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
加入开机自启动
# systemctl enable nginx
如果不想开机自启动了,可以使用下面的命令取消开机自启动
# systemctl disable nginx
服务的启动/停止/刷新配置文件/查看状态
# systemctl start nginx.service 启动nginx服务 # systemctl stop nginx.service 停止服务 # systemctl restart nginx.service 重新启动服务 # systemctl list-units --type=service 查看所有已启动的服务 # systemctl status nginx.service 查看服务当前状态 # systemctl enable nginx.service 设置开机自启动 # systemctl disable nginx.service 停止开机自启动
文件说明:
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
出现问题
nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
解决方案:
方案一:创建默认目录 /var/run/nginx/,但是每次重启都会删除这个文件夹,开机自启也会因此失败。推荐方案二
方案二:
vi /usr/local/nginx/conf/nginx.conf
pid的根下修改为
pid /usr/local/nginx/logs/nginx.pid;
保存后,创建新文件夹
mkdir /usr/local/nginx/logs
Nginx配置SSL证书
首先生成证书
https://www.cnblogs.com/aeolian/p/12375120.html
然后配置证书
vi /usr/local/nginx/conf/nginx.conf
server { listen 443 ssl; ssl on; ssl_certificate /etc/crt/autumn.crt; ssl_certificate_key /etc/crt/autumn.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; server_name aeolian.xyz; location /ray/ { # 与 V2服务端 配置中的 path 保持一致 proxy_redirect off; proxy_pass http://127.0.0.1:10000;#这个端口和服务端保持一致 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $http_host; # Show realip in v2 access.log proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
重启nginx,提示requires ngx_http_ssl_module,说明缺少模块。
/usr/local/nginx/sbin/nginx -V
查看安装了哪些模块
查看configure arguments:后边的值,如果有,就复制下来。
进入到安装包,然后执行
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --with-http_ssl_module
make
这里就不能再重新make install了,不然会覆盖安装。
关闭nginx
/usr/local/nginx/sbin/nginx -s stop
把编译好的nginx覆盖掉原有的nginx。
cp ./objs/nginx /usr/local/nginx/sbin/
然后重新启动,发现依然有,没办法,只能make install覆盖安装,好在nginx.conf没有覆盖。
这次成功了,有个warn,意思是不推荐使用ssl。
浏览器测试成功。