OPRATION09 - 生产Nginx调优

一、Nginx安装

1.1 安装依赖包

  • openssl-devel、zlib-devel 是为使nginx 支持 https 时使用
  • pcre-devel 安装 pcre 库是为了使 nginx 支持 HTTP Rewrite 模块
yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel

1.2 编译,安装Nginx

  • 编译时添加 --with-http_ssl_module 模块让Nginx支持SSL,编译时会去读取已经安装好的openssl 依赖包
  • 编译时添加 --with-pcre模块让Nginx支持Rewrite,编译时会去读取已经安装好的pcre依赖包
./configure --prefix=/opt/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module

二、Nginx配置优化详解

2.1 主配置文件nginx.conf

2.1.1 优化nginx worker进程数

  • master 进程用于管理 worker 进程,worker 进程用于 Nginx 服务;worker 进程数应该设置为服务器 CPU 的核数
~]$ grep -c processor /proc/cpuinfo         # 查看CPU核数
2
~]$ vim /opt/nginx/conf/nginx.conf          # 设置worker进程数
worker_processes  2;

2.1.2  优化 Nginx 单个进程允许的最大连接数

  • 控制 Nginx 单个进程允许的最大连接数的参数为 worker_connections ,这个参数要根据服务器性能和内存使用量来调整
  • Nginx 总并发连接数 = worker_processes * worker_connections

 ] # vim /opt/nginx/conf/nginx.conf

events {
    worker_connections  65535;   #配置单个线程打开的连接数为65535
}

 2.1.3 定义日志格式

  • 自定义访问日志的日志采集字段,方便通过查看访问日志定位问题
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$request_body" "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    '$connection "$upstream_addr" "$request_body"'
                    '"$upstream_response_time" "$request_time"';

 2.1.4 隐藏版本号

  • 避免暴露nginx版本号,防止黑客利用版本漏洞攻击nginx
server_tokens off;

2.1.5 优化域名hash存储

  • 避免多个域名不能正常hash存储了,调整大小为1024
 server_names_hash_bucket_size 1024

 2.1.6 优化文件传输

  • 通过使用sendfile系统调用来加快文件传输效率
sendfile   on;

2.1.7 优化TCP超时时间

  • 指定每个 TCP 连接最多可以保持多长时间,Nginx 默认值是 75 秒,有些浏览器只能保持 60 秒,所以可以设定为 60 秒
keepalive_timeout 60s;

2.1.8 GZIP 性能优化

  • gzip详细参数优化见注释
 gzip on;
 gzip_http_version 1.0;
 gzip_vary on;
 #varyheader支持,让前端的缓存服务器识别压缩后的文件,代理
 gzip_proxied any;
 #允许或者禁止压缩基于请求和响应的响应流,若设置为any,将会压缩所有请求
 gzip_types text/plain text/css application/javascript application/x-javascript text/xml 
 application/xml application/xml+rss text/javascript application/json image/jpeg image/gif image/png;
 #压缩类型
 gzip_min_length 1100;
 #对数据启用压缩的最少字节数,如:请求小于1K文件,不要压缩,压缩小数据会降低处理此请求的所有进程速度
 gzip_buffers 16 8k;
 #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
 gzip_comp_level 9;
 #gzip压缩等级在0-9内,数值越大压缩率越高,CPU消耗也就越大

2.1.9 错误码重定向优化 

  • 当出现错误码400,500这两类错误的时候,统一重定向到一个指定页面
error_page   500 502 503 504 404 403 400  /index.html;
location = /index.html {
     rewrite ^(.*)$ https://$host/no-block/html/no-block.html redirect;
}

2.1.10 开启ssi模块

  • 将内容发送到浏览器之前,可以使用“服务器端包含 (SSI)”指令将文本、图形或应用程序信息包含到网页中
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;

三、生产Nginx配置文件

3.1 主配置文件nginx.conf

  • 具体参数调优参考2.1节,3.1节的nginx.conf是调优之后的结果,可复制后稍做修改用于生产

] # vim /opt/nginx/conf/nginx.conf

user  mogu;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        pid/nginx.pid;


events {
    worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$request_body" "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    '$connection "$upstream_addr" "$request_body"'
                    '"$upstream_response_time" "$request_time"';
    server_tokens off;
    #access_log  logs/access.log  main;
    server_names_hash_bucket_size 1024;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml 
    application/xml application/xml+rss text/javascript image/jpeg image/gif image/png;
    gzip_min_length  512;
    gzip_buffers 16 8k;

     server {
        listen       443 ssl;
        server_name  $host;

        include common/ssl.conf;

        access_log  logs/access-443.log  main;

        error_page   500 502 503 504 404 403 400  /index.html;
        location = /index.html {
            rewrite ^(.*)$ https://$host/no-block/html/no-block.html redirect;
        }

        include common/common-insurance.conf;
    }
    include upstream.conf;
    include vhost/*.conf;

}

3.2 虚拟主机配置

3.2.1 虚拟主机配置文件

  • 配置监听端口和访问域名,小程序打开域名下的接口需要在指定域名下配置微信公众平台派发的txt文件
  • 配置前端页面访问路径并包含后端服务服务配置文件

] # vim /opt/nginx/conf/vhost/tklife.moguyun.com.conf

server {
        listen       443 ssl;
        server_name  tklife.moguyun.com;

        include common/ssl.conf;
        client_max_body_size 10m;
        access_log  logs/tklife.moguyun.com.log  main;

        location /lJp2ihln7V.txt {
            return 200 'd2f1a999d9ede4bee0434d6d73ad1cf8';
        }
        location ^~/data-manager{
            alias /opt/www/tkrs/data-manager;
            index index.html;
            try_files $uri $uri/ /data-manager/index.html;
        }
        include common/common-insurance.conf;
    }

3.2.2 证书配置文件

  • 引入了moguyun.com这个域名下的证书和私钥,证书和密钥都正确才可以正常使用moguyun.com域名的https协议

] # vim /opt/nginx/conf/common/ssl.conf

ssl_certificate     /opt/cert/20180203/server.pem;
ssl_certificate_key /opt/cert/20180203/server.key;

3.3 公共后端配置文件

  • 写入一些公用的前端配置,如location /no-block 用于前面的错误码重定向
  • 匹配后端服务名称,如financial-shop-server是后端SpringMVC的项目名称,前端同学通过这个名称调用后端接口
  • 通过proxy_pass 代理到financial,然后通过这个别名找到对应的真正提供后端服务的IP和端口

] # vim /opt/nginx/conf/common/common-insurance.conf

        ssi on;
        ssi_silent_errors on;
        ssi_types text/shtml;  #开启ssi模块支持

        charset utf-8;

        location /no-block {
            alias /opt/www/no-block;
        }

        location /resources {
             alias /opt/www/resources;
             index index.html;
        }
        location /financial-shop-server {
             proxy_connect_timeout   3;
             proxy_send_timeout      30;
             proxy_read_timeout      30;
             proxy_pass http://financial;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $host;
             proxy_redirect off;
             proxy_headers_hash_max_size 51200;
             proxy_headers_hash_bucket_size 6400;
        }

        location /financial-shop-server/intf/web/admin/user/getCheckStatus {
             if ($request_method !~* POST) {
                 return 403;
             }

             proxy_connect_timeout   30;
             proxy_send_timeout      300;
             proxy_read_timeout      300;
             proxy_pass http://financial/financial-shop-server/intf/web/admin/user/getSessionInfo;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $host;
             proxy_redirect off;
             proxy_headers_hash_max_size 51200;
             proxy_headers_hash_bucket_size 6400;
        }

3.4 后端服务IP端口配置文件

  • 配置的是提供后端服务的IP地址和端口号,upstream.conf已经被nginx.conf包含了,所以可以通过financial这个别名找到真正提供后端服务的IP和端口

] # vim /opt/nginx/conf/upstream.conf

upstream financial {
        server 10.80.165.79:6211;
}
  • 参考链接:

nginx使用ssl模块配置支持HTTPS访问​​​​​​​

发布了161 篇原创文章 · 获赞 40 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_36441027/article/details/103921150