Nginx

一、安装Nginx

1,安装nginx

nginx官网地址:http://nginx.org/en/download.html

 yum install pcre pcre-devel -y 安装依赖包

 yum install -y openssl openssl-devel 安装依赖包

 wget http://nginx.org/download/nginx-1.18.0.tar.gz 

 tar zxvf nginx-1.18.0.tar.gz 

 cd nginx-1.18.0/ 

 ./configure --prefix=/usr/local/nginx 

 make 

 make install 

2,nginx语法支持

 mkdir ~/.vim 创建目录

 cp -r contrib/vim/* ~/.vim 

二、安装OpenResty

1,安装

 wget https://openresty.org/download/openresty-1.19.3.1.tar.gz 

 tar zxvf openresty-1.19.3.1.tar.gz 

 cd openresty-1.19.3.1/ 

 ./configure --prefix=/usr/local/openresty 

 gmake 

 gmake install 

2,启动

 ./nginx -c /usr/local/openresty/nginx/conf/nginx.conf 启动失败查看下端口是否被占用

3,配置上游服务

 vim /usr/local/openresty/nginx/conf/nginx.conf 

...
 upstream local{
        server 127.0.0.1:8080;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://local;
        }

...

 4,配置返回真实的访问ip

...

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

...

三、基本命令

1,基础命令

 [root@localhost sbin]#./nginx -c /usr/local/nginx/conf/nginx.conf   启动nginx

 [root@localhost sbin]# ./nginx -t   测试配置文件是否有语法错误

 [root@localhost sbin]# ./nginx -s reload   重载配置文件

 [root@localhost sbin]# ./nginx -s stop   立刻停止服务

 [root@localhost sbin]# ./nginx -s quit   优雅的停止服务

 [root@localhost sbin]# ./nginx -s reopen   重新开始记录日志

2, 热部署

热更新nginx程序,一般只需要替换掉sbin/nginx二进制文件,不需要更换其他文件

 cp nginx nginx.old 备份nginx二进制文件

 ps -ef | grep nginx 查看nginx进程号

 kill -USR2 10105 向nginx的老的master进程发送USR2信号,会新启动nginx进程,现在新老nginx进程并存,老的进程不再监听端口

 kill -WINCH 10105 向nginx的老的master进程发送WINCH信号,请优雅的关闭掉所有的worker进程

 kill -QUIT 10105 优雅的关闭老的master进程

 kill -HUP 10105 版本回退操作,重启老的nginx的worker进程

3,日志切割

如果日志文件过大,可以把日志文件清空(先把日志文件备份)

 nginx -s reopen 或者 kill -USR1 10105 

四、配置

1,gzip压缩

①配置 vim /usr/local/nginx/conf/nginx.conf 

    gzip  on; #on开启gzip off关闭
    gzip_min_length 1;#小于多少字节的文件不进行压缩,测试可以配置小点看效果
    gzip_comp_level 2;#压缩级别
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;#针对某些类型进行gzip压缩

    server {
        listen       80;
        server_name  localhost;

②重载配置

 ./nginx -s reload 

2,限制访问速度

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            alias  html/gentelella-master/production/;
            index  index.html index.htm;
            set $limit_rate 1k;#限制访问速度,每秒传输1k 字节
        }

3,配置请求日志

ttp {
    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;

    sendfile        on;
...
...
server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;

        access_log  logs/host.access.log  main;

3,缓存

...
proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
    upstream local{
        server 127.0.0.1:8080;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://local;
            proxy_cache my_cache;
            proxy_cache_key $host$uri$is_args$args;
            proxy_cache_valid 200 304 302 1d;
        }
...

五、GoAccess可视化监控access日志

1,安装使用goaccess

①安装ncurses

 yum install ncurses-devel 

②安装geoip

 yum install geoip-devel 

③安装goaccess

$ wget http://tar.goaccess.io/goaccess-1.2.tar.gz
$ tar -xzvf goaccess-1.2.tar.gz
$ cd goaccess-1.2/
$ ./configure --enable-utf8 --enable-geoip=legacy --prefix=/usr/local/goaccess
$ make
# make install

④启动goaccess

 ./goaccess /usr/local/nginx/logs/host.access.log -o /usr/local/nginx/html/monitoring/report.html --real-time-html --time-format='%H:%M:%S' --date-format='%d/%b/%y' --log-format=COMBINED 

  • /usr/local/nginx/logs/host.access.log 日志位置
  • -o /usr/local/nginx/html/monitoring/report.html 输出到report.html文件
  • --real-time-html 实时更新页面
  • --time-format='%H:%M:%S'  时间格式
  • --date-format='%d/%b/%y'  日期格式
  • --log-format=COMBINED  日志格式

⑤修改nginx配置

 vim /usr/local/nginx/conf/nginx.conf 

...
        location /report.html{
           alias /usr/local/nginx/html/monitoring/report.html;
        }
...

六、SSL证书

1,证书类型

 ①域名验证(domain validated,DV)证书

只会认证域名的归属是否正确。只要是你的域名指向的是申请证书的那台服务器。很多都是免费的

 ②组织验证(organization validated,OV)证书

在申请证书的时候会去验证填写的机构、企业名称是否正确。一般OV证书申请需要几天时间,价格高于DV证书

 ③扩展演示(extended validation,EV)证书

EV证书更严格的验证,所以大部分浏览器对EV证书的显示非常友好,会在地址栏中显示机构名称

2,TLS安全密码套件

 

  •  第一部分: 密钥交换。椭圆曲线加密算法的表达。密钥交换是为了解决浏览器与服务器怎样各自独立的生成密钥,而最后生成的密钥是相同的,接下来会用这个密钥去加密数据。
  •  第二部分:身份验证。密钥交换需要验证身份
  •  第三部分:数据加密解密通讯的时候需要用到对称加密算法。AES(表达是那种算法),128(AES中有三种加密强度,使用128位加密强度),GCM(新的分组模式,可以提高多核CPU情况下加密或解密的性能)
  •  第四部分:SHA256是一个摘要算法,把不定长度的字符串,生成一个固定长度的更短的一个摘要

 3,生成免费证书

 yum install python2-certbot-nginx 安装certbot。yum搜索不到,则需要更新yum源。参考:https://blog.csdn.net/coderyqwh/article/details/111410616

 ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/sbin/nginx 创建软链接。sbin里面没有nginx执行certbot会失败

 certbot --nginx --nginx-server-root=/usr/local/openresty/nginx/conf/ -d www.zhang.com --nginx-server-root指定nginx配置目录

七、使用信号管理Nginx的父子进程

 1,Master进程

 kill -TERM 2938 或者kill -INT 2938  立刻停止nginx进程

 kill -QUIT 2938 优化的停止nginx进程

 kill -HUP 2938 重载配置文件

 kill -USR1 2938 重新打开日志文件,做日志文件切割

 kill -USR2 2938 向nginx的老的master进程发送USR2信号,会新启动nginx进程,现在新老nginx进程并存,老的进程不再监听端口

 kill -WINCH 2938 向nginx的老的master进程发送WINCH信号,请优雅的关闭掉所有的worker进程

2,Worker进程(一般不会直接对worker进程进行管理)

  kill -TERM 3937 或者kill -INT 3937 

 kill -QUIT 3937 

 kill -USR1 3937 

 kill -WINCH 3937 

 3,nginx命令行

reload: HUP

reopen:USR1

stop:TERM

quit:QUIT

八、模块 

 nginx官网文档:http://nginx.org/en/docs/

 1,查询编译进来的模块

 vim /usr/local/src/nginx-1.18.0/objs/ngx_modules.c 

*ngx_modules[]数组里面是编辑进nginx的模块

①查询模块源代码(例如ngx_http_gzip_filter_module)

 vim /usr/local/src/nginx-1.18.0/src/http/modules/ngx_http_gzip_filter_module.c 

 

原文地址:https://www.cnblogs.com/zd1994/p/14124151.html