date: 2020-09-27title: nginx安装 #标题
tags: nginx安装 #标签
categories: nginx # 分类

记录下nginx安装过程,适用于大多数nginx版本。

安装

  1. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  2. yum makecache fast
  3. yum -y install pcre pcre-devel openssl openssl-devel zlib-devel zlib git gcc
  4. wget http://mirrors.sohu.com/nginx/nginx-1.18.0.tar.gz
  5. tar zxf nginx-1.18.0.tar.gz && cd nginx-1.18.0
  6. # 修改源码,隐藏版本信息
  7. $ vim src/core/nginx.h
  8. 源码如下:
  9. #define nginx_version 1001018
  10. #define NGINX_VERSION "1.18.0"
  11. #define NGINX_VER "nginx/" NGINX_VERSION
  12. 改为:
  13. #define nginx_version 1001018
  14. #define NGINX_VERSION "unknown"
  15. #define NGINX_VER "unknown/" NGINX_VERSION
  16. $ vim src/http/ngx_http_header_filter_module.c
  17. 源码如下:
  18. static char ngx_http_server_string[] = "Server: nginx" CRLF;
  19. 改为:
  20. static char ngx_http_server_string[] = "Server: unknown" CRLF;
  21. $ vim src/http/ngx_http_special_response.c
  22. 源码如下(有很多相似的字段,千万别改错了,注意!):
  23. static u_char ngx_http_error_tail[] =
  24. "<hr><center>nginx</center>" CRLF
  25. "</body>" CRLF
  26. "</html>" CRLF
  27. ;
  28. 改为:
  29. static u_char ngx_http_error_tail[] =
  30. "<hr><center>unknown</center>" CRLF
  31. "</body>" CRLF
  32. "</html>" CRLF
  33. ;
  34. mkdir /apps/usr -p
  35. # 为了支持sticky调度算法基于cookie会话保持,增加第三方模块sticky
  36. git clone https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng.git
  37. sticky_path=$(readlink -f nginx-sticky-module-ng/)
  38. ./configure --prefix=/apps/usr/nginx-1.18.0 --with-http_ssl_module \
  39. --with-http_addition_module --with-pcre \
  40. --with-http_stub_status_module --with-debug \
  41. --add-module=${sticky_path} --with-stream --with-http_realip_module
  42. make
  43. make install

安装后的优化

  1. cd /apps/usr/nginx-1.18.0/conf/
  2. $ vim nginx.conf
  3. # 全局字段配置如下:
  4. worker_processes auto;
  5. worker_cpu_affinity auto;
  6. worker_rlimit_nofile 65535;
  7. events {
  8. worker_connections 65535;
  9. }
  10. # http{ }字段配置如下:
  11. http {
  12. server_tokens off;
  13. sendfile on;
  14. keepalive_timeout 65;
  15. proxy_buffer_size 128k;
  16. proxy_buffers 32 32k;
  17. proxy_busy_buffers_size 256k;
  18. server_names_hash_bucket_size 256;
  19. client_header_buffer_size 256k;
  20. large_client_header_buffers 4 256k;
  21. reset_timedout_connection on;
  22. client_max_body_size 50m;
  23. client_header_timeout 10;
  24. client_body_timeout 10;
  25. send_timeout 10;
  26. open_file_cache max=1000 inactive=20s;
  27. open_file_cache_errors on;
  28. open_file_cache_min_uses 2;
  29. keepalive_requests 100;
  30. tcp_nodelay on;
  31. tcp_nopush on;
  32. gzip on;
  33. gzip_min_length 10k;
  34. gzip_comp_level 5;
  35. gzip_buffers 4 16k;
  36. gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css text/js application/json;
  37. gzip_http_version 1.1;
  38. gzip_proxied any;
  39. gzip_vary on;
  40. gzip_disable "MSIE [1-6].";
  41. .........
  42. }
  43. # 若使用反向代理,location字段添加如下,使后端应用获取客户端真实IP
  44. location / {
  45. .........
  46. proxy_set_header Host $host;
  47. proxy_set_header X-Real-IP $remote_addr;
  48. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  49. }
  50. # 修改系统最多可打开文件数限制
  51. $ ulimit -n # 查看当前限制
  52. 1024
  53. cat >> /etc/security/limits.conf << EOF
  54. * soft nofile 65535
  55. * hard nofile 65535
  56. EOF
  57. # 重新登录后生效

检测配置文件并启动nginx

  1. $ ../sbin/nginx -t
  2. nginx: the configuration file /apps/usr/nginx-1.18.0/conf/nginx.conf syntax is ok
  3. nginx: configuration file /apps/usr/nginx-1.18.0/conf/nginx.conf test is successful
  4. $ ../sbin/nginx
  5. $ curl -I 127.0.0.1 # 访问测试
  6. HTTP/1.1 200 OK
  7. Server: unknown
  8. Date: Tue, 07 Jul 2020 15:13:53 GMT
  9. Content-Type: text/html
  10. Content-Length: 151
  11. Last-Modified: Tue, 07 Jul 2020 14:43:35 GMT
  12. Connection: keep-alive
  13. Accept-Ranges: bytes