Nginx随记

Nginx随记

一、nignx 常用功能模块

1、静态资源部署

2、Rewrite地址重写(正则表达式)

3、反向代理

4、负载均衡(轮询、加权轮询、ip_hash、url_hash、fair)

5、web缓存

nginx核心组成

  • 二进制可执行文件
  • 配置文件(nginx.conf)
  • 错误日志(error.log)
  • 访问日志(access.log)

二、安装

源码安装

1、安装环境依赖

需要的依赖:gcc、pcre、zlib、openssl

yum -y install wget
yum -y install gcc gcc-c++
yum -y install pcre pcre-devel
yum -y install openssl openssl-devel
yum -y install zlib zlib-devel
2、下载指定版本的 nginx 安装包
cd /opt
wget http://nginx.org/download/nginx-1.18.0.tar.gz
3、解压
tar -zxvf nginx-1.18.0.tar.gz
4、编译并安装
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
make
make install
5、环境变量修改
export PATH=/usr/local/nginx/sbin:$PATH 

source /etc/profile
6、检查环境变量是否生效
export	#查看是否有刚才的配置路径
nginx -t #查看是否有ok和successful信息输出

进入nginx的sbin文件夹下启动,查看是否可以访问Nginx欢迎页面

yum 安装

1、安装依赖
yum install -y yum-utils
2、创建nignx源配置信息
vi /etc/yum.repos.d/nginx.repo
  • 文件内容如下
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
3、安装
sudo yum install nginx
  • yum 源默认使用的是stable,需要切换为mainline执行以下命令
sudo yum-config-manager --enable nginx-mainline

三、配置

1、nginx配置文件结构

# 全局配置

events{	#events配置
		...
}

http{		#http 配置
	server{
			location[0]{
			...
			}
			location[1]{
			...
			}
	}
	server...
}

全局配置:整个nginx的全局指令,一般配置的 用户组信息、进程pid、日志存放路径、外部引入的配置文件路径以及允许生成的worker process的数量信息

events配置:配置整个服务器与用户的网络连接,包含每个进程的最大连接数,事件驱动模型等

http配置:里面可以嵌套多个server,并配置代理、缓存、日志等绝大多数功能

server配置:配置主机相关参数

location:配置请求的路由,以及各种页面的处理情况

2、详细配置说明

文件名称:nginx.conf

文件位置:安装目录下的conf文件夹下,一般在/usr/local/nginx/conf

文件内容:

  • worker_processes

设置 worker的数量,Nginx的进程模型采用的是master、worker模式,一个master负责协调多个worker负责与客户端交互。设置为 auto 就可以。

  • events

设置使用的模型和每个 worker 的连接数

模型有:epoll、kqueue、icop

Linux操作系统中模型建议使用 epoll。

worker 的连接数通常设置为 10240 即可,也可以根据实际情况适当修改。

events{
	use epoll;
}
  • include

包含另一个文件的内容将另一个文件的内容放在标记处。

多个文件可以写多个 include,也可以使用通配符匹配多个文件。

  • sendfile

设置文件高效传输是否开启,默认开启。

  • tcp_nopush

必须是 sendfile 值为 on 时才有效,当 tcp_nopush 设置为 on 代表当数据包累计到一定大小时再发送,有助于提升文件传输效率。

  • keepalive_timeout

客户端连接服务器的超时时间,在连接断开前,客户端再次与服务器进行交互,可复用此连接,不需要重新建立一个连接,减少了资源的开销。保持默认值即可

  • gzip

设置为 on,代表会先将数据压缩再进行传输,这样做会增加传输的效率,节省带宽,但会影响服务器CPU的性能。

开启此配置,还需要配置一些额外的属性。

此处可以权衡是节省带宽还是提高CPU性能,建议开启,根据实际情况配置。

gzip on;

gzip_min_length 512;   # 限制最小压缩,单位字节,小于该值则不会压缩

gzip_comp_level 5;     # 压缩的级别,值为1到9,级别越高压缩的比例越大,cpu消耗越多

gzip_types text/plain application/javascript text/css image/jpeg image/gif image/png application/json; # 需要压缩的文件类型

  • server

一个server块就是一个虚拟的服务。

在server块中可以指定虚拟服务的 端口、服务名、路由规则等信息。

server可以有多个。

一个server下,location也可以有多个。

例:

server {
        listen       90;           # 端口
        server_name  localhost;    # 服务名,可以是IP地址或者域名,当端口相同时,会根据服务名选择走哪个路由规则

        location / {               # 根路径路由规则
            root   html;           # 对应nginx安装目标下的html文件夹,也可以设置为绝对路径,例如:root /home/html;
            index  hello.html;     # 指定默认首页为 hello.html
        }
     location /hello {        root /home/hello;        # index 省略,代表没有默认页     }      
        error_page   500 502 503 504  /50x.html;  # 指定这些状态码跳转的错误页
        location = /50x.html {
            root   html;
        }
    }
  • location

server块中包含了location块,location块在一个server下可以有多个,主要是配置请求的路由规则。

nginx根据请求的资源路径,与location块进行匹配,然后根据location的配置对路由进行转发。

location支撑多种匹配规则:

精确匹配

location / {            # 根路径路由规则
            root   html;       # 对应nginx安装目标下的html文件夹,也可以设置为绝对路径,例如:root /home/html;
            index  hello.html; # 指定默认首页为 hello.html
        }
     location /hello {
       root /home/hello;
       # index 省略,代表没有默认页
    }

正则表达式匹配

location ~* \.(GIF|PNG|jpg|bmp|jpeg) {  # *代表不区分大小写
   root /home/img;
}

匹配以某个路径开头的请求

    location ^~ /server/page/ {
        root /home/page;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值