Nginx学习笔记

参考博客:https://zhuanlan.zhihu.com/p/83890573

什么是Nginx

静态资源服务器、Http代理服务器、反向代理服务器、电子邮件(IMAP/POP3)代理服务器。
官网:http://nginx.org/

为什么要使用Nginx

  1. HTTP服务器,部署静态资源,提高静态资源的访问效率
  2. 虚拟主机,可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
  3. 反向代理
  4. 负载均衡,当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
  5. Nginx的cpu、内存等资源消耗却非常低,运行非常稳定。

Nginx的使用

安装(Centos 7)

  1. 安装Nginx所需环境

    1. gcc
      yum install -y gcc-c++
      
    2. PERE
      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,所以需要在linux上安装zlib库。
      yum install -y zlib zlib-devel
      
    4. openssl
      OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
      nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
      yum install -y openssl openssl-devel
      
  2. 创建相关目录

    cd /
    ## 安装包保存目录
    mkdir installationPackage
    
    ## 安装目录
    mkdir apps
    
    ## 数据目录
    mkdir nginx-data
    
    ## 数据临时目录
    cd /apps/nginx-data
    mkdir temp
    cd temp
    mkdir client
    mkdir proxy
    mkdir fastcgi
    mkdir uwsgi
    mkdir scgi
    
  3. 下载Nginx安装包
    官网下载页面:http://nginx.org/en/download.html

    cd /installationPackage
    
    wget http://nginx.org/download/nginx-1.19.0.tar.gz
    
  4. 解压安装包

    cd /installationPackage
    
    tar zxvf nginx-1.19.0.tar.gz
    
  5. 配置

    cd /installationPackage/nginx-1.19.0
    
    ./configure --prefix=/apps/nginx --pid-path=/apps/nginx-data/nginx.pid --lock-path=/apps/nginx-data/nginx.lock --error-log-path=/apps/nginx-data/error.log --http-log-path=/apps/nginx-data/access.log --with-http_gzip_static_module --http-client-body-temp-path=/apps/nginx-data/temp/client --http-proxy-temp-path=/apps/nginx-data/temp/proxy --http-fastcgi-temp-path=/apps/nginx-data/temp/fastcgi --http-uwsgi-temp-path=/apps/nginx-data/temp/uwsgi --http-scgi-temp-path=/apps/nginx-data/temp/scgi --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
    
  6. 编译 安装

    cd /installationPackage/nginx-1.19.0
    
    make
    
    make install
    
  7. 修改配置文件

    cd /apps/nginx/conf
    
    vim nginx.conf
    
  8. 启动Nginx

    cd /apps/nginx/sbin
    
    ./nginx start
    

Nginx配置文件详解

  ```
  ########### 每个指令必须有分号结束。#################
  ## 全局块:配置影响nginx全局的指令,一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  
  # 配置用户或者组,默认为nobody nobody。
  #user  nobody;
  
  # 允许生成的进程数,默认为1
  worker_processes  1;
  
  # 指定日志路径,级别。这个设置可以放入全局块,http块,server块,级别依次为:debug|info|notice|warn|error|crit|alert|emerg
  #error_log  logs/error.log;
  #error_log  logs/error.log  notice;
  #error_log  logs/error.log  info;
  
  # 指定nginx进程运行文件存放地址
  #pid        logs/nginx.pid;
  
  ## events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  events {
        # 设置网路连接序列化,防止惊群现象发生,默认为on
        # accept_mutex on;
        
        # 设置一个进程是否同时接受多个网络连接,默认为off
        # multi_accept on;
        
        # 事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
        # use epoll;
        
        # 最大连接数,默认为512
        worker_connections  1024;
  }
  
  ## http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  http {
        # 文件扩展名与文件类型映射表
        include       mime.types;
        # 默认文件类型,默认为text/plain
        default_type  application/octet-stream;
        
        # 自定义日志格式
        #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 off;时取消服务日志
        # access_log  logs/access.log  main;
        
        # 允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
        sendfile        on;
        
        # 每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
        sendfile_max_chunk 100k;
        
        #tcp_nopush     on;
        
        # 连接超时时间,默认为75s,可以在http,server,location块。
        #keepalive_timeout  0;
        keepalive_timeout  65;
        
        #gzip  on;
        
        ## server块:配置虚拟主机的相关参数,一个http中可以有多个server。
        server {
              # 单连接请求上限次数。
              keepalive_requests 120; 
              
              # 监听端口号
              listen       80;

              # 监听地址
              server_name  localhost;
              
              #charset koi8-r;
              
              #access_log  logs/host.access.log  main;
              
              ## location块:配置请求的路由,以及各种页面的处理情况。
              # 请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
              location / {
                    # 根目录
                    root   html;
                    # 设置默认页
                    index  index.html index.htm;
                    # 请求转向mysvr 定义的服务器列表
                    proxy_pass  http://mysvr;
                    # 拒绝的ip
                    # deny 127.0.0.1;
                    # 允许的ip
                    # allow 172.18.5.54;
              }
              
              #error_page  404              /404.html;
              
              # redirect server error pages to the static page /50x.html
              
              # 错误页
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                    root   html;
              }
              
              # proxy the PHP scripts to Apache listening on 127.0.0.1:80
              #
              #location ~ .php$ {
              #    proxy_pass   http://127.0.0.1;
              #}
              
              # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
              #
              #location ~ .php$ {
              #    root           html;
              #    fastcgi_pass   127.0.0.1:9000;
              #    fastcgi_index  index.php;
              #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
              #    include        fastcgi_params;
              #}
              
              # deny access to .htaccess files, if Apache's document root
              # concurs with nginx's one
              #
              #location ~ /.ht {
              #    deny  all;
              #}
        }
        
        
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
        
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
        
        
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
        
        #    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 / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
        
  }
  ```

Nginx常用命令

  1. 启动

    cd /apps/nginx/sbin
    
    ./nginx
    
  2. 查看是否启动

    ps -aux | grep nginx
    
  3. 停止

    cd /apps/nginx/sbin
    
    ./nginx -s stop
    # 或
    ./nginx -s quit
    
  4. 刷新配置文件

    cd /apps/nginx/sbin
    
    ./nginx -s reload
    
  5. 重启
    先关闭后启动

原文地址:https://www.cnblogs.com/hutianyao/p/13196265.html