Nginx 基础

什么是nginx

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。

由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,

并且cpu、内存等资源消耗却非常低,运行非常稳定。

应用场景

版本区别

常用版本分为四大阵营

Nginx开源版 (常用)

http://nginx.org/

Nginx plus 商业版

https://www.nginx.com

openresty

http://openresty.org/cn/

Tengine

http://tengine.taobao.org

安装

编译安装

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
yum install -y pcre pcre-devel

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

安装nginx

下载 Nginx,下载地址:https://nginx.org/en/download.html

放在你想放在的地方,无所谓。

./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_stub_status_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module
--prefix=/usr/local/nginx  表示软件安装到/usr/local/nginx下面。nginx的各种配置也就在里面了。
这个make install 的时候就不用在指定安装路径。
执行完成后查看目录里面已经多了一个Makefile文件

注意,有朋友说想要http3的模块,看一下构建参数 https://nginx.org/en/docs/configure.html

make
make install

安装成功后会看到以下目录,在/usr/local/nginx

conf
html
sbin

容器安装

docker直接启动、docker-compose启动,k8s启动。

我们一般不会用docker直接启动,感兴趣可以看这里 https://hub.docker.com/_/nginx

docker-compose启动

version: '3'
services:
  nginx:
    restart: always
    container_name: nginx
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - /usr/local/nginx/html:/usr/share/nginx/html
      - /usr/local/nginx/www:/var/www
      - /usr/local/nginx/logs:/var/log/nginx
      # 有可能会出现不能挂载,这个时候用手动拷贝配置文件就行
      - /usr/local/nginx/nginx.conf/:/etc/nginx/nginx.conf
      - /usr/local/nginx/etc/cert:/etc/nginx/cert
      - /usr/local/nginx/conf.d:/etc/nginx/conf.d
    environment:
      - NGINX_PORT=80
      - TZ=Asia/Shanghai
    privileged: true

container_name: 容器名称

image: 镜像

ports: 端口

volumes: 路径映射

启动

docker-compose up -d

docker-compose常用命令

k8s启动

使用volume共享主机目录

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
  # 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
        # 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
  # NodePort类型可以对外暴露端口
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    nodeSelector:
        app: nginx
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
      - image: nginx:1.22.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
        volumeMounts:
        - name: nginx-config
          mountPath: "/etc/nginx/conf.d/"
      volumes:
      - name: nginx-config
        hostPath:
          # directory location on host
          path: /etc/nginx/conf.d/
          type: Directory

注意这种方式要给主机打label,不然会漂移

Configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
  namespace: hello-world
data:
  default.conf: |
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

目录结构

进入Nginx的主目录我们可以看到这些文件夹

client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp

其中这几个文件夹在刚安装后是没有的,主要用来存放运行过程中的临时文件

client_body_temp fastcgi_temp proxy_temp scgi_temp

一般我们要用关注下面这几个目录

conf 用来存放配置文件相关

html 用来存放静态文件的默认目录 html、css等

sbin nginx的主程序

logs 日志文件

最小配置

worker_processes

worker_processes 1; 默认为1,表示开启一个业务进程

worker_connections

worker_connections 1024; 单个业务进程可接受连接数

include mime.types;

include mime.types; 引入http mime类型 (多媒体文件如视频、图片之类的)

default_type application/octet-stream;

default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。

sendfile on;

使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。

Nginx 文档建议使用它以提高效率,但大文件除外。可能发生的情况是,大文件传输可能会导致连接过长阻塞。解决方案可以在与上面相同的链接中找到,它可以使用sendfile_max_chunk.

location /mp3 {
    sendfile           on;
    sendfile_max_chunk 1m; # Limits chunks to 1 Megabytes
}

关于sendfile的可以看官方文档

keepalive_timeout 65;

指定每个TCP 连接最多可以保持多长时间。