CentOS7 源码安装Nginx及Nginx基本管理设置

  • A+
所属分类:linux技术
摘要

CentOS7 安装 参考文档 CentOS7最小安装后初始化安装工具   1:yum install net-tools 参考文档

CentOS7 安装 参考文档

CentOS7最小安装后初始化安装工具

  1:yum install net-tools 参考文档

     2:源码安装wget 参考文档 或者执行 yum -y install wget

 

CentOS7 源码安装包步骤

  一:Nginx 安装步骤

    1-1:源码下载地址 http://nginx.org/en/download.html

      执行命令:wget wget http://nginx.org/download/nginx-1.21.3.tar.gz

    1-2:解压下载的文件 

      执行命令:tar  -zxvf   {压缩文件名}.tar.gz

    1-3:配置configure 检测安装及依赖信息

      查看命令文档执行:./configure --help

      CentOS7 源码安装Nginx及Nginx基本管理设置

 

       先安装编译器

      执行:sudo yum -y install gcc

      再次执行:./configure 进行配置 如下图:

      CentOS7 源码安装Nginx及Nginx基本管理设置

 

       检测到有依赖包没有装,根据提示去安装 pcre

      执行命令:sudo yum -y install pcre-devel

      再次执行:./configure  如下图:

      CentOS7 源码安装Nginx及Nginx基本管理设置

 

      根据提示继续安装依赖包:

     执行命令:sudo yum -y install zlib-devel

     一直重复执行./configure 直到检测到没有依赖项就说明检测通过了,如下图:

     CentOS7 源码安装Nginx及Nginx基本管理设置

 

      检测并指定安装路径命令:./configure --prefix=/usr/local/nginx

、    CentOS7 源码安装Nginx及Nginx基本管理设置

 

     1-4:执行make指令:源码生成可执行文件,中途不能出现error

      CentOS7 源码安装Nginx及Nginx基本管理设置

 

 

    1-5:安装 make install

      执行安装命令:sudo make install

    CentOS7 源码安装Nginx及Nginx基本管理设置

   测试:

     1:进入:/usr/local/nginx 执行:./sbin/nginx

     2:在浏览器中输入:http://localhost

     3:安装一个文本浏览器测试 yum -y install elinks

      CentOS7 源码安装Nginx及Nginx基本管理设置

   

  Nginx相关目录文件

    nginx path prefix:/usr/local/nginx (Nginx安装目录)

    nginx binary file:/usr/local/nginx/sbin/nginx(Nginx启动目录)

    nginx moduless path:/usr/local/nginx/modules(Nginx相关模块目录)

    nginx configuration prefix:/usr/local/nginx/conf(Nginx配置文件目录) 

    nginx configuration file:/usr/local/nginx/conf/nginx.con(nginx启动配置文件)

    nginx pid file:/usr/local/nginx/logs/nginx.pid(nginx进程号)

    nginx error log file:/usr/local/nginx/logs/error.log:(nginx错误日志文件)

    nginx http access log file:/usr/local/nginx/logs/access.log(访问日志文件)

   

  先关闭防火墙 (测试需要在局域网中访问)

    setenforce 0
    systemctl stop firewalld
    systemctl disable firewalld

 

     Nginx添加到环境变量

    使用软连接将nginx链接到/usr/local/sbin

    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin || /usr/local/sbin/ | grep "nginx" 

               CentOS7 源码安装Nginx及Nginx基本管理设置

    显示当前环境变量PATH

      echo $PATH

    编辑.bash_profile文件

      vim ~/.bash_profile

    在.bash_profile文件末尾加入以下内容

      export PATH=$PATH:/usr/local/nginx/sbin

    CentOS7 源码安装Nginx及Nginx基本管理设置

    引用.bash_profile文件

      source ~/.bash_profile

    使用nginx命令

    # 启动nginx

      nginx

    # 停止nginx

      nginx -s quit

  

nginx部分配置

CentOS7 源码安装Nginx及Nginx基本管理设置CentOS7 源码安装Nginx及Nginx基本管理设置

# 启动该程序的默认程序 #user  nobody; # 一个主进程和多个工作进程。这里定义的是主进程数量 worker_processes  4;  # 全局错误日志的位置及日志格式 #error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info;  #pid        logs/nginx.pid;   events {     # 每个工作进程最大并发数     worker_connections  1024; }  # http 服务设置 http {     include       mime.types;     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  logs/access.log  main; # 全局日志路径     # $remote_addr与$http_x_forwarded_for用以记录客户端ip地址     # $remote_user:记录客户端名称     # $time_local:记录访问时间与时区     # $request:记录访问状态 200成功     # $body_bytes_sent:记录发送给客户端文件主体内容大小     # $http_referer:记录从哪个页面链接访问过来的     # $http_user_agent:记录客户端浏览器相关信息      # sendfie指令指定nginx是否调用sendfile函数(zero copy 方式)来输出文件     sendfile        on;     # 允许或禁止使用socke的TCP_CORK的选项仅在使用sendfile的时候使用     #tcp_nopush     on;     # 长连接超时时间     #keepalive_timeout  0;     keepalive_timeout  65;     # 开启压缩     #gzip  on;     # 默认网站  配置虚拟主机     server {     # 虚拟主机使用的端口         listen       80;     # 主机域名         server_name  localhost;     # 支持的字符集         charset utf-8;         #访问日志路径         #access_log  logs/host.access.log  main;     # 定义web根路径         location / {         # 根目录路径             root   html;         # 索引页面             index  index.html index.htm;         }      # 访问控制目录     location /a {         # 允许访问         allow  127.0.0.0;         # 不允许         deny  all;         # return 404;         return  http://www.baidu.com;     }             #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;         }      } }

View Code

  

  如果修改了配置文件先进入nginx目录中:cd /usr/local/nginx ,在执行:./sbin/nginx -g ./conf/nginx.conf 检测配置文件是否正确

  修改配置重启nginx 方式一:

    先执行:killall nginx

    在执行:cd /usr/local/nginx 执行:./sbin/nginx

  修改配置重启nginx 方式一:

    killall -s HUP nginx (重新加载配置文件)

  nginx访问目录配置

    注意:访问控制目录中的location /a{} 这里的location值得就是 web根路径中的root路径

    # 定义web根路径         location / {         # 根目录路径             root   html;         # 索引页面             index  index.html index.htm;         }      # 访问控制目录     location /a {         # 允许访问         allow  127.0.0.0;         # 不允许         deny  all;         # return 404;         return  http://www.baidu.com;     }    

 Nginx日志管理

   Nginx访问日志主要有两个参数控制

    1:log_format 用来定义日志格式,可以定义多种日志格式,取不同名字即可

    2:access_log 用来指定日志文件路径及使用何种日志格式记录日志

   

  log_format格式变量:

    # access_log logs/access.log main; # 全局日志路径

    # $remote_addr与$http_x_forwarded_for用以记录客户端ip地址

  `   # $remote_user:记录客户端名称
    # $time_local:记录访问时间与时区
    # $request:记录访问状态 200成功
    # $body_bytes_sent:记录发送给客户端文件主体内容大小
    # $http_referer:记录从哪个页面链接访问过来的
    # $http_user_agent:记录客户端浏览器相关信息

   CentOS7 源码安装Nginx及Nginx基本管理设置

 

   自定义格式为json格式:  

   # 自定义日志格式     # log_format default_fmt '[$time_local] $remote_addr "$request"'     # 自定义json日志格式     log_format default_fmt_json '{"@timestamp":"$time_local",'                                 '"client_ip":"$remote_addr",'                                 '"request":"$request",'                                 '"status":"$status",'                                 '"bytes":"$body_bytes_sent",'                                 '"x_forwarded":"$http_x_forwarded_for",'                                 '"referer":"$http_referer"'                                 '}';

  两种格式测试效果如下:

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

Nginx图片防盗链

   

        # 防盗链(只针对b目录)         # location /b {         # 项目中以下文件后缀         location ~* .(png|gif|bmp|jpg|jpeg)$ {         valid_referers none blocked *.ticp.net;         if ($invalid_referer){                 return 403;         }         }

 

Nginx虚拟主机

    一个web服务器软件默认情况下只能发布一个web,因为一个web分享出去需要三个条件(IP、Port、Domain name)

    虚拟主机就是把一台物理服务器划分成多个虚拟服务器,每个虚拟主机都可以有独立的域名和独立的目录。

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

 

  1:基于IP的虚拟主机

    准备:

      1:在nginx根目录下准备两个网站如:

        /usr/local/nginx/html/web1/index.html

        /usr/local/nginx/html/web1/index.html

      2:添加一个子IP,使用逻辑网卡添加一个 自网卡的方式

        添加网卡:ifconfig ens33:1 192.168.0.131/24 up

        删除网卡:ifconfig ens33:1 down

      CentOS7 源码安装Nginx及Nginx基本管理设置

 

   基于IP虚拟主机nginx配置信息 (/usr/local/nginx/conf/nginx.conf)

    

user centos; worker_processes  4; events {     worker_connections  1024; } http {     include       mime.types;     default_type  application/octet-stream;          log_format default_fmt_json '{"@timestamp":"$time_local",'                     '"client_ip":"$remote_addr",'                 '"request":"$request",'                 '"status":"$status",'                 '"bytes":"$body_bytes_sent",'                 '"x_forwarded":"$http_x_forwarded_for",'                 '"referer":"$http_referer"'                 '}';         sendfile        on;     keepalive_timeout  65;     server {         listen       192.168.0.130:80;         server_name  localhost;         charset utf-8;         access_log  logs/host.access.log  default_fmt_json;         location / {             root   html/web1;             index  index.html index.htm;         }     }     server {         listen       192.168.0.131:80;         server_name  localhost;         charset utf-8;         access_log  logs/host.access.log  default_fmt_json;         location / {             root   html/web2;             index  index.html index.htm;         }     } }

  重启nginx 测试 (先:killall nginx 在:../sbin/nginx )

   CentOS7 源码安装Nginx及Nginx基本管理设置

 

 

  CentOS7 源码安装Nginx及Nginx基本管理设置

 

   2:基于端口的虚拟主机(只需要修改配置即可)

CentOS7 源码安装Nginx及Nginx基本管理设置CentOS7 源码安装Nginx及Nginx基本管理设置

user centos; worker_processes  4; events {     worker_connections  1024; } http {     include       mime.types;     default_type  application/octet-stream;          log_format default_fmt_json '{"@timestamp":"$time_local",'                     '"client_ip":"$remote_addr",'                 '"request":"$request",'                 '"status":"$status",'                 '"bytes":"$body_bytes_sent",'                 '"x_forwarded":"$http_x_forwarded_for",'                 '"referer":"$http_referer"'                 '}';         sendfile        on;     keepalive_timeout  65;     server {         listen       80;         #server_name  localhost;         charset utf-8;         access_log  logs/host.access.log  default_fmt_json;         location / {             root   html/web1;             index  index.html index.htm;         }     }     server {         listen       8080;         #server_name  localhost;         charset utf-8;         access_log  logs/host.access.log  default_fmt_json;         location / {             root   html/web2;             index  index.html index.htm;         }     } }

View Code

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

     测试

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

   3:基于域名的虚拟主机配置文件

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

Nginx反向代理

    代理服务器,客户在发送请求的时候,不会直接发送给目的主机,而是先发送给代理服务器,代理服务器接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器硬盘中,在发送给客户机

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

 

    应用场景:

        堡垒机场景

        内网服务器发布场景

        缓存场景

        CentOS7 源码安装Nginx及Nginx基本管理设置

 

 

        

    

    代理服务器原理:

           CentOS7 源码安装Nginx及Nginx基本管理设置

 

     代理配置文件  

CentOS7 源码安装Nginx及Nginx基本管理设置CentOS7 源码安装Nginx及Nginx基本管理设置

user centos; worker_processes  4; events {     worker_connections  1024; } http {     include       mime.types;     default_type  application/octet-stream;          log_format default_fmt_json '{"@timestamp":"$time_local",'                     '"client_ip":"$remote_addr",'                 '"request":"$request",'                 '"status":"$status",'                 '"bytes":"$body_bytes_sent",'                 '"x_forwarded":"$http_x_forwarded_for",'                 '"referer":"$http_referer"'                 '}';         sendfile        on;     keepalive_timeout  65;     server {         listen       80;         #server_name  localhost;         charset utf-8;         access_log  logs/host.access.log  default_fmt_json;         location / {             proxy_pass http://wendj.ticp.net;         }     } }

View Code

    CentOS7 源码安装Nginx及Nginx基本管理设置

Nginx限速

      Nginx官方版本限制IP的链接和并发分别有两个模块

        limit_req_zone:用来限制单位时间内的请求数,即速率限制。

        limit_req_conn:用来限制同一时间连接数,即并发限制。

         CentOS7 源码安装Nginx及Nginx基本管理设置

 

   Nginx中URL重写

      rewrite模块(ngx_http_rewrite_module)

      Rewrite功能是Nginx服务器提供的一个重要功能。几乎所有的web产品必备技能,用于实现URL重写。URL重写是非常常用的功能,比如它可以在我们改变网站结构后,不需要客户端修改原来的书签,也不需要其它网站吸怪对我网站的友情链接,还可以在一定程度上提高网站安全性。

      Nginx服务器Rewrite功能是依赖于PCRE(PerlCompatibleRegularExpression)

      

Nginx优化

    Nginx是主进程+工作进程模型

    worker_processes 1:工作进程数量,按CPU的总核心调整

    worker_cpu_affinity 0001 0100 1000:CPU的亲和力

    worker_connections 1024:一个工作进程的并发数

  查看CPU核数指令:cat /proc/cpuinfo |grep "flags"|wc -l

    CentOS7 源码安装Nginx及Nginx基本管理设置

 

   统计nginx连接数:netstat -antpl|grep nginx|grep ESTABLISHED|wc -l

   

  Nginx长连接

     http协议属于TCP协议

     优化目标:减少三次握手和四次挥手次数

     keepalive_timeout 5:长连接时间

     keepalive_requests 8192:每个长连接接受最大请求数

   Nginx数据压缩

     gzip on;      # 开启压缩

     gzip_proxied any;  # 任何时候都压缩

     gzip_min_length 1k;  # 启用压缩最小文件,小于设置的不会被压缩

        gzip_buffers 4 8k;  # 压缩的缓存区大小

     gzip_comp_level 6;  # 压缩级别1-9数字越大压缩的越好,也越占CPU时间

     gzip_types text/plain text/css application/x-javascript application/javascript application/xml;  # 压缩文件类型

   Nginx客户端缓存

     location ~* .(png|gif|jpg|jpeg)${

     expires 1h;

     }

  

  Nginx参考文章