nginx在Linux上搭建
一.Nginx介绍和常用功能
1.Nginx介绍
Nginx是一个高性能的HTTP和反向代理web服务器(百度百科的介绍)
2.常见功能
1)Http代理,反向代理:
作为web服务器最常用的功能之一,尤其是反向代理(正向代理和反向代理不理解的可以看https://www.jianshu.com/p/ae76c223c6ef、https://blog.****.net/tsummerb/article/details/79248015)
2)负载均衡策略:
轮询:平均将请求分发给web服务器
weight:权重
ip_hash:使同一个ip客户端去访问同一个服务器
least_conn:把请求转发给连接数较少的后端服务器
二.安装Nginx
2.linux下安装
2.1 环境准备
1)安装gcc
说明:安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境
命令:yum install gcc-c++
2)安装pcre pcre-devel
说明:PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
命令:yum install -y pcre pcre-devel
3)安装zlib
说明:zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库
命令:yum install -y zlib zlib-devel
4) 安装OpenSSL
说明:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的**和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库
命令:yum install -y openssl openssl-devel
2.2 安装nginx
1)去官网下载安装包(https://nginx.org/en/download.html)
2)将安装包上传到Linux服务器,我放在/usr/local/nginx下(没有nginx自己建一个目录)
3)解压
cd到上一步的压缩包的位置解压文件
tar -zxvf nginx-1.16.1.tar.gz
4)配置、编译、安装
cd到ngnix-1.16.1的目录下配置
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module
make(编译)
make install(安装)
5)启动ngnix
cd 到/usr/local/ngnix/sbin
./nginx启动
在linux浏览器输入你自己linux的ip,如看到如下内容即启动成功
如需在外面访问需要关闭linux的防火墙
暂时关闭防火墙systemctl stop firewalld
永久关闭防火墙systemctl disable firewalld
快速关闭 ./nginx -s stop
安全关闭 ./nginx -s quit
重载配置文件 ./nginx -s reload
查看nginx进程 ps -ef|grep nginx
三 Nginx的配置(最重要)
nginx.conf大致格式如下
主要由6个部分(重点学习http、server、location)
- main:用于进行nginx全局信息的配置
- events:用于nginx工作模式的配置
- http:用于进行http协议信息的一些配置
- server:用于进行服务器访问信息的配置
- location:用于进行访问路由的配置
- upstream:用于进行负载均衡的配置
1)main模块
# user nobody nobody;
worker_processes 2;
# error_log logs/error.log
# error_log logs/error.log notice
# error_log logs/error.log info
# pid logs/nginx.pid
worker_rlimit_nofile 1024;
解释:
- user:用来指定nginx worker进程运行用户以及用户组,默认nobody账号运行
- worker_processes:指定nginx要开启的子进程数量,运行过程中监控每个进程消耗内存(一般几M~几十M不等)根据实际情况进行调整,通常数量是CPU内核数量的整数倍
- error_log:定义错误日志文件的位置及输出级别【debug / info / notice / warn / error / crit】
- pid:用来指定进程id的存储文件的位置
- worker_rlimit_nofile:用于指定一个进程可以打开最多文件数量的描述
2)event 模块
event {
worker_connections 1024;
multi_accept on;
use epoll;
}
解释:
- worker_connections:最大连接数
- multi_accept:设置一个进程是否同时接受多个网络连接,默认为off
- use:事件驱动模型select|poll|kqueue|epoll|resig|/dev/poll|
eventport
3) http模
包含server、localhost、upstream在456分别说
http {
include mime.types;
default_type application/octet-stream;
#access_log off;
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #
sendfile on;
sendfile_max_chunk 100k;
keepalive_timeout 65;
解释:
- include; #文件扩展名与文件类型映射表
- default_type; #默认文件类型,默认为text/plain
- access_log ; #取消服务日志
- log_format;#自定义格式
- access_log log/access.lo; #combined为日志格式的默认值
- sendfile;#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
- sendfile_max_chunk 100k;#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
- keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
- error_page 404 https://www.baidu.com; #错误页
4) server模块
server {
listen 80;#监听的端口
server_name localhost ;
root /nginx/www;
index index.php index.html index.html;
charset utf-8;
access_log logs/access.log;
error_log logs/error.log;
......
}
解释(主要是server和server_name)
- server:一个虚拟主机的配置,一个http中可以配置多个server
- server_name:用你指定ip地址或者域名,多个配置之间用空格分隔
- root:表示整个server虚拟主机内的根目录,所有当前主机中web项目的根目录
- index:用户访问web网站时的全局首页
- charset:用于设置www/路径中配置的网页的默认编码格式
- access_log:用于指定该虚拟主机服务器中的访问记录日志存放路径
- error_log:用于指定该虚拟主机服务器中访问错误日志的存放路径
注意:
1.启动nginx你服务的端口就是80,
如你设置为8081则访问ip:8081或者你配置的server_name
2.server_name 可以正则匹配如下:
server_name ~^www\d+\.testyx\.com$
server_name ~^(www\.)?(.+)$
这里正则表达式必须以“~”开头,而且使用时不要忘记设置"^" 和"$"
5)localhost模块
location / {
#root path;
#index vv.txt;
proxy_pass 192.168.8.109:8089;
#deny 127.0.0.1; #拒绝的ip
#allow 172.18.5.54; #允许的ip
}
解释
- location /:
-
- index :默认页
- proxy_pass:为你代理转发的服务器地址
注意:
root都是用来存放你静态代码的地方
server root , location root 区别
6)upstream模块
upstream tomcats{
server 192.168.8.110:8080;
server 192.168.8.110:8081;
server 192.168.8.110:8082;
}
主要是用来做负载均衡的(具体应用可看下一章节--负载均衡)
- down:表示当前的server暂时不参与负载均衡;
- backup:预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻;
- max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误;
- fail_timeout:在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。
四、反向代理
4.1在nginx.conf修改添加
server {
listen 80;
server_name localhost www.testyx.com;
location / {
proxy_pass http://192.168.8.108:8081;
index index.html;
}
}
4.2并在linux启动一个tomcat,其端口为8081
4.3测试访问测试
五、负载均衡
5.1在linux中安装3个tomcat服务端口分别为8080、8081、8082
具体安装见链接:
https://blog.****.net/qq_23832313/article/details/83584564
安装以后如下:
修改3个tomcat下的index.jsp
5.2配置ngnix的配置
ngnix.conf修改
upstream tomcats{
server 192.168.8.110:8080;
server 192.168.8.110:8081;
server 192.168.8.110:8082;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcats;
index index.html index.html;
}
5.3启动nginx测试
输入自己Linux的ip:80
5.4负载均衡其他策略
5.4.1热备
在本地启动2个tomcat端口分别为8080和8081,
访问localhost可以看到只会访问到8080
关闭8080的tomcat,在访问localhost可以看到会访问8081
配置如下:
upstream tomcats{
server 192.168.8.108:8080 ;
server 192.168.8.108:8081 backup;
}
server {
listen 80;
server_name localhost www.testyx.com;
location / {
proxy_pass http://tomcats;
index index.html;
}
}
5.4.2轮询
第五章节的第一个例子就是轮询
5.4.3加权轮询
upstream tomcats{
server 192.168.8.108:8080 weight=1 ;
server 192.168.8.108:8081 weight=2 ;
server 192.168.8.108:8082 weight=4 ;
}
5.4.4ip_hash
nginx会让相同的客户端ip请求相同的服务器
upstream tomcats{
server 192.168.8.108:8080 ;
server 192.168.8.108:8081 ;
server 192.168.8.108:8082 ;
ip_hash;
}