1.nginx安装和基本配置

作者

微信:tangy8080
电子邮箱:914661180@qq.com
更新时间:2019-07-10 20:56:10 星期三

欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程中的编写的文章
如您在阅读过程中发现文章错误,可添加我的微信 tangy8080 进行反馈.感谢您的支持。

文章主题

介绍如何在CentOs7上安装 nginx
在这里,我将nginx安装在k8s-180,k8s-181,k8s-182上.

前置条件

您已经准备好了三台Centos7的主机

正文

安装

安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel wget vim
安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能。

#下载 PCRE 安装包,下载地址: http://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz
cd /usr/local/src/
wget http://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz
#解压并删除压缩包
tar zxvf pcre-8.35.tar.gz
rm  pcre-8.35.tar.gz
#进入安装包目录
cd pcre-8.35
#编译安装 
./configure
make && make install
#查看pcre版本
pcre-config --version
下载vts虚拟主机流量状态模块[按需]

该模块可以采集到更多的Nginx指标参数,侧重流量采集.如有需要,您可以编译安装
github地址:https://github.com/vozlt/nginx-module-vts#compatibility

cd /usr/local/src
git clone git://github.com/vozlt/nginx-module-vts.git
安装Nginx
#下载 Nginx,下载地址:http://nginx.org/download/nginx-1.14.2.tar.gz
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.14.2.tar.gz
#解开安装包
tar zxvf nginx-1.14.2.tar.gz
rm nginx-1.14.2.tar.gz
#进入安装包目录
cd /usr/local/src/nginx-1.14.2
#编译安装,编译参数视使用场景而定,这里需要使用tcp负载均衡  所以编译了stream模块
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 --with-stream --add-module=/usr/local/src/nginx-module-vts
make
make install
#加入环境变量
vim /etc/profile
##在后面导入nginx的安装路径
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
##刷新配置
source /etc/profile
#查看nginx版本
##查看版本:
nginx -v
##查看详细信息:
nginx -V

  • --add-module:添加了vst模块,后期我们会使用该模块提供的数据导出metrics到promethues
  • --with-stream:支持TCP代理和负载均衡-stream模块
添加nginx用户组

说明:如不添加在启动时会提示 getpwnam("nginx") failed 检查配置文件也不会通过
这里创建的用户是你安装Nginx时,预编译时指定的Nginx用户(1.3 第四条的编译参数)

useradd nginx -s /sbin/nologin -M
测试配置文件的正确性
nginx -t

返回如下信息,表示配置文件语法正确

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

配置

编辑服务文件
cd /lib/systemd/system/


vim nginx.service
[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
常用命令
systemctl start nginx.service          启动nginx服务
systemctl stop nginx.service           停止服务
systemctl restart nginx.service        重新启动服务
systemctl list-units --type=service     查看所有已启动的服务
systemctl status nginx.service          查看服务当前状态
systemctl enable nginx.service          设置开机自启动
systemctl disable nginx.service         停止开机自启动
配置文件路径
/usr/local/nginx/conf/nginx.conf
VTS配置文件样例(为方便编辑,可用xftp下载下来配置好后传上去)
user  nginx;
worker_processes  8;

error_log  /usr/local/nginx/logs/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /usr/local/nginx/conf/mime.types;
    default_type  application/octet-stream;
    client_header_buffer_size 100k;
    proxy_buffer_size 64k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size 64k;

    #VTS的配置
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;   #开启此功能,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个上。

    #开启header的下划线支持
    underscores_in_headers on;
    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  /usr/local/nginx/logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    #VST server
    server {
        location /status {
            allow 192.168.161.0/24;
            allow 10.254.0.0/16;
            allow 172.30.0.0/16;
            deny  all;
            vhost_traffic_status off;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
    include /etc/nginx/conf.d/*.conf;
}
  • 上述配置描述了基本的VTS配置,如您没有VTS模块.可以删除
  • 当您启用VTS server时,如果处于公网环境,请注意安全.应该避免指标参数被公网任意访问
    这这里,配置了仅内网,k8s Pod网段,k8s svc网段可访问

测试您的安装

访问:http://192.168.161.180/,您应当看到Nginx欢迎页

访问:http://192.168.161.180/status,您应当看到Nginx Vhost Traffic Status页

访问:http://192.168.161.180//status/format/prometheus,您应当看到很多prothues的metrics指标
其中有些指标,在后期做监控时我们会用的到.比如我们可以统计某个server 在一定时间内是否返回了 5xx的代码.从而触发报警

常见问题

1.1 查看已经安装的模块
nginx -V
1.2 执行 nginx -t 是OK的,然而在执行 nginx -s reload 的时候报错

nginx: [error] invalid PID number "" in "/run/nginx.pid"
解决办法

nginx -c /usr/local/nginx/conf/nginx.conf
nginx -s reload
1.3 重新编译nginx
#重新编译时,需确认原始版本和现在的版本一致
cd /usr/local/src/nginx-1.14.2
#配置新的编译参数
./configure 新的参数
#开始重新编译
make
#覆盖原始文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp -rfp /usr/local/src/nginx-1.14.2/objs/nginx /usr/local/nginx/sbin/nginx
#查看是否是预期的编译参数
nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 --with-stream --add-module=/usr/local/src/nginx-module-vts

引用链接

https://github.com/vozlt/nginx-module-vts

请尽量按照自己期望的生活 email:18980489167@189.cn
原文地址:https://www.cnblogs.com/gytangyao/p/11406070.html