安装nginx
【目录】
简介
Nginx (engine x http://nginx.org)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫开发的,第一个公开版本0.1.0发布于2004年10月4日其特点是占用内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
应用场景
静态服务器(图片 视频服务器) 竞争软件 lighttpd
动态服务 nignx +fastcgi
反向代理 负载均衡 竞争软件 haproxy
cache(web缓存) 竞争软件vanish
快速开始
-
安装依赖(centos)<span id=dep>
centos
#GCC (gun compiler collection)c语言编译器 #gcc-c++ c++语言编译器 yum install gcc gcc-c++ -y # rewrite模块需要 pcre (perl compatible regular expression per 兼容正则表达式) yum install pcre pcre-devel -y # zlib 配置中gizp on 使用 yum install zlib zlib-devel -y # openssl 提供https 和md5 sha1等 yum install openssl openssl-devel -y
ubuntu
# 更新软件包列表 sudo apt-get update # 安装GCC和G++编译器 sudo apt-get install gcc g++ -y # 安装PCRE库 sudo apt-get install libpcre3 libpcre3-dev -y # 安装zlib库 sudo apt-get install zlib1g zlib1g-dev -y # 安装OpenSSL库 sudo apt-get install openssl libssl-dev -y
-
优化内核参数 <span id=kernel>
IP Sysctl — The Linux Kernel documentation
net.ipv4.tcp_fin_timeout真实意义 - 今夕何兮 - 博客园 (cnblogs.com)
由优化FIN_WAIT_2状态超时引入的关于tcp_fin_timeout参数研究 - zzouqb - twt企业IT交流平台 (talkwithtrend.com)
#单个worker 可以打开的最大文件句柄 fs.file-max = 999999 # 当服务器主动断开时,socket保持fin-wait-2的最大时长 net.ipv4.tcp_fin_timeout = 30 # 允许处于time_wait 状态的socket 能够处理新的tcp 连接 net.ipv4.tcp_tw_reuse = 1 # 开启TCP连接中TIME-WAIT的快速回收,NAT环境可能导致DROP掉SYN包(回复RST) net.ipv4.tcp_tw_recycle = 1 # 保持tcp 连接。默认值为7200(2h),设置为600(10m) ,可以更快的断开无效的连接节省资源开销 net.ipv4.tcp_keepalive_time = 600 # 开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击 net.ipv4.tcp_syncookies = 1 # 保持TIME_WAIT套接字的最大个数,超过这个数字TIME_WAIT套接字将立刻被清除并打印警告信息 net.ipv4.tcp_max_tw_buckets = 5000 #指定tcp 和 udp 在本地端口的使用范围 net.ipv4.ip_local_port_range = 1024 61000 # 接受tcp syn 请求的最大长度。调大改值可以避免当web请求过大时不至于丢失客户发起的连接 net.ipv4.tcp_max_syn_backlog = 8096 # tcp 接收缓存的 最小值 默认值 最大值 net.ipv4.tcp_rmem = 4096 327686 262142 # tcp 发送缓存 最小值 默认值 最大值 net.ipv4.tcp_wmem = 4096 327686 262142 # 当网卡接收速度大于内核处理速度是会有一个队列保存数据, net.core.netdev_max_backlog = 8192 # 内核套接字的接收缓冲区 net.core.rmem_default = 262144 net.core.rmem_max = 2097152 # 内核套接字的发送缓冲区 net.core.wmem_default = 262144 net.core.wmem_max = 2097152
-
编译
修改源码隐藏软件类型,网站502时不会暴露自己为nginx
wget http://nginx.org/download/nginx-1.22.1.tar.gz tar xf nginx-1.22.1.tar.gz && cd nginx-1.22.1
-
隐藏nginx标识
src/http/ngx_http_header_filter_module.c
源码
static u_char ngx_http_server_string[] = "Server: nginx" CRLF; static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
修改后
static u_char ngx_http_server_string[] = "Server: IIS" CRLF; static u_char ngx_http_server_full_string[] = "Server: IIS" CRLF; static u_char ngx_http_server_build_string[] = "Server: IIS" CRLF;
-
关闭错误提示中展示nginx信息。
src/http/ngx_http_special_response.c
源码
static u_char ngx_http_error_full_tail[] = "<hr><center>" NGINX_VER "</center>" CRLF "</body>" CRLF "</html>" CRLF ; static u_char ngx_http_error_build_tail[] = "<hr><center>" NGINX_VER_BUILD "</center>" CRLF "</body>" CRLF "</html>" CRLF ; static u_char ngx_http_error_tail[] = "<hr><center>nginx</center>" CRLF "</body>" CRLF "</html>" CRLF ;
修改后
static u_char ngx_http_error_full_tail[] = "<hr><center> IIS </center>" CRLF "</body>" CRLF "</html>" CRLF ; static u_char ngx_http_error_build_tail[] = "<hr><center> IIS </center>" CRLF "</body>" CRLF "</html>" CRLF ; static u_char ngx_http_error_tail[] = "<hr><center>IIS</center>" CRLF "</body>" CRLF "</html>" CRLF ;
源码编译
useradd nginx -M -s /bin/nologin mkdir -p /var/tmp/nginx/client/ chown -R nginx.nginx /var/tmp/nginx/client/
./configure \ --prefix=/opt/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-stream_ssl_module \ --with-stream \ --with-http_realip_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre \ --with-debug
make && make insatll
/opt/nginx/sbin/nginx -v /opt/nginx/sbin/nginx -V # 备份并更新日志文件 /opt/nginx/sbin/nginx -s reopen
启动并测试
user nginx; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
[root@lavm-ioreaqndwv nginx]# curl -I http://117.72.41.172/ HTTP/1.1 200 OK Server: IIS Date: Fri, 07 Jun 2024 10:21:41 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Fri, 07 Jun 2024 09:42:18 GMT Connection: keep-alive ETag: "6662d5fa-267" Accept-Ranges: bytes
-