前言
这个文章当我的备忘了,本身打算直接一股脑弄完的,结果想了想,万一下次再弄一次忘记了又得重头弄起,有点麻烦,转念一想,好像没什么用宝塔的人喜欢手动编译NGINX做调优?
然后就抱着这样的心态写下了这篇文章,作为一个记录,方便我以后调试,可能不会像之前那样写的那么直白,会直接写过程,不会每个地方做出详细的解释。如果希望了解原理,请前往本处提供的链接出处学习。
所用的东西都是现成的,只是由于宝塔的缘故要多一道坎,不用宝塔的直接用github项目里自带的脚本就完事了
开工!
环境
宝塔7.5.1
Centos8
git
curl
使用到的东西
https://github.com/stylersnico/nginx-secure-config 已经完成安全优化的NGINX配置文件
https://github.com/stylersnico/nginx-openssl-chacha-naxsi 配套项目(NGINX+WAF(我自己用了宝塔的,就不装了)+模块+Openssl(自带的是1.1.1故也不需要))
安装NGINX
先将headers-more-nginx-module装了,对于此模块的介绍:https://segmentfault.com/a/1190000018418253?utm_source=coffeephp.com
二话不说先进入宝塔,卸载掉已经安装过的nginx(注意有没有什么重要的conf,记得先备份下conf)
在软件列表找到nginx,安装,选择1.19(我的目前版本,可以选更新的),选择编译安装,选择自定义模块
首先是headers-more-nginx-module模块,这是一个可以自定义头部的nginx模块
模块名称
headers_more_nginx_module
描述随意
模块参数
--add-module=/usr/src/headers-more-nginx-module-0.33
前置脚本
cd /usr/src curl -fLRO "https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz" tar -xaf v0.33.tar.gz
点击提交,然后是brotli加速,比gzip要好的压缩算法
模块名称
ngx_brotli
模块参数
--add-module=/usr/src/ngx_brotli
前置脚本
cd /usr/src git clone https://github.com/google/ngx_brotli cd ngx_brotli git submodule update --init
其他的都可以不用管了,宝塔内建的都会带上,宝塔不自带线程池,不过那个东西好像也没什么用…
想要用aio threads的话,得去改宝塔的nginx安装脚本,这个可以去参考下这篇文章:https://www.daniao.org/7265.html这篇的思路是差不多的
确认无误,勾选上这两个模块
(新版宝塔真方便,以前还得手动单独改宝塔nginx的安装脚本才行)
配置NGINX
打开配置文件https://github.com/stylersnico/nginx-secure-config/blob/master/nginx.conf
这里面的参数我是直接拿来用了,对比下宝塔默认的配置与其不同地方进行删改添加即可
我已经调整完毕,可以正常运行的配置:
user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; pid /www/server/nginx/logs/nginx.pid; worker_rlimit_nofile 16884; events { use epoll; worker_connections 16384; multi_accept on; } http { include mime.types; #include luawaf.conf; include proxy.conf; default_type application/octet-stream; server_names_hash_bucket_size 64; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; fastcgi_connect_timeout 4s; fastcgi_send_timeout 120s; fastcgi_read_timeout 120s; fastcgi_buffer_size 256k; fastcgi_buffers 256 32k; fastcgi_busy_buffers_size 512k; fastcgi_temp_file_write_size 512k; reset_timedout_connection on; fastcgi_intercept_errors on; gzip on; gzip_min_length 1k; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml; gzip_vary on; gzip_proxied any; gzip_disable "msie6"; server_tokens off; access_log off; ##TLS ssl_protocols TLSv1.2 TLSv1.3; ssl_ecdh_curve X25519:sect571r1:secp521r1:secp384r1; ssl_session_timeout 1d; ssl_session_tickets off; ssl_ciphers 'TLS13+AESGCM+AES128:TLS13+AESGCM+AES256:TLS13+CHACHA20:EECDH+AESGCM:EECDH+CHACHA20'; ##头部常规安全配置 add_header Feature-Policy "geolocation none;midi none;notifications none;push none;sync-xhr none;microphone none;camera none;magnetometer none;gyroscope none;speaker self;vibrate none;fullscreen self;payment none;"; more_set_headers "Strict-Transport-Security : max-age=15768000; includeSubDomains; preload"; more_set_headers "X-Frame-Options : SAMEORIGIN"; more_set_headers "Content-Security-Policy : default-src https: data: 'unsafe-inline' 'unsafe-eval' always"; more_set_headers "X-Xss-Protection : 1; mode=block"; more_set_headers "X-Content-Type-Options : nosniff"; more_set_headers "Referrer-Policy : strict-origin-when-cross-origin"; more_set_headers "Server : Follow the white rabbit."; ##为TLS1.3启用对0-RTT的支持 ssl_early_data on; proxy_set_header Early-Data $ssl_early_data; ##Brotli brotli on; brotli_comp_level 6; brotli_static on; brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml; ##IP访问限制 limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; limit_req_zone $binary_remote_addr zone=allips:10m rate=400r/s; limit_req zone = allips burst=400 nodelay; ##缓存 open_file_cache max=2000 inactive=20s; open_file_cache_valid 60s; open_file_cache_min_uses 5; open_file_cache_errors off; ##其他优化 client_body_timeout 15; client_header_timeout 15; send_timeout 15; server { listen 888; server_name phpmyadmin; index index.html index.htm index.php; root /www/server/phpmyadmin; #error_page 404 /404.html; include enable-php.conf; location / { limit_conn perip 40; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; } #纠正原先的一个错误,limit参数应当是在server中进行修改
为了避免错误,client_body_buffer_size需要在NGINX配置里的“性能调整”进行修改,本处修改为1024
如果出现”perip”的报错,注意是否在前面已经有定义过perip了。
大功告成
参考
https://github.com/stylersnico/nginx-secure-config/blob/master/nginx.conf