lanxi256 发表于 2018-11-11 09:12:01

Nginx安装配置详解-修订版

  版本更新历史:
  v1.2更新内容:
  添加了proxy_connect正向代理模块的说明
  添加了stream配置tcp代理
  更新了gzip的配置
  修复了大部分安装配置错误
  改善了lua安装过程
  v1.1更新内容:
  集成ngx_lua和nginx_upstream_check两个模块,这两个模块是目前最常用的第三方模块
1.1软件信息
  软件:nginx
  版本:1.9.0+
  用户:nginx
1.2安装目录
  /home/nginx/nginx                           安装主目录
  /home/nginx/nginx/conf/nginx.conf         主配置文件
  /home/nginx/nginx/conf/conf.d/vhost         虚拟主机配置文件目录
  /home/nginx/nginx/conf/conf.d/upstream      upstream配置文件目录
  /home/nginx/nginx/conf/conf.d/ssl         ssl证书文件目录
  /home/nginx/nginx/conf/conf.d/gzip          gzip配置文件目录
  /home/nginx/nginx/logs                  日志文件目录,pid文件也存放在这里
  /home/nginx/nginx/temp                  临时文件路径,如静态缓存、代理缓存文件
  /home/nginx/nginx/sbin                  nginx命令目录
1.3安装过程

[*]  1.安装必备软件
  yum install gcc gcc-c++ make glibc-devel glibcopenssl openssl-devel -y

[*]  2.编译pcre,版本不建议低于8.3.5
  unzip pcre-8.3.5.zip -d /home/nginx/soft/
  cd /home/nginx/soft/pcre-8.3.5
  ./configure --enable-utf8 && make

[*]  3.ngx_upstream_check_module-master安装:用来做后端服务器健康检查的第三方模块,安装nginx_upstream_check_module-master 模块需要的准备工作为:
  wget    https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
  unzip master -d/home/nginx/soft
  cd/home/nginx/soft/nginx-1.9.0
  patch -p1 < /home/nginx/soft/nginx_upstream_check_module-master/check.patch

[*]  4.ngx_lua:是用来通过lua扩展nginx功能的,安装nginx_lua需要的准备工作为:
  wget   http://luajit.org/download/LuaJIT-2.0.4.tar.gz
  tar -zxvfLuaJIT-2.0.4.tar.gz -C /home/nginx/soft/
  mkdir~/luajit
  cd /home/nginx/soft/LuaJIT/
  make && make install PREFIX=/home/nginx/luajit
  exportLUAJIT_LIB=/home/nginx/luajit/lib >> ~/.bash_profile
  exportLUAJIT_INC=/home/nginx/luajit/include/luajit-2.0>> ~/.bash_profile
  exportLD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH>> ~/.bash_profile
  exportPATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/luajit/bin:$HOME/nginx/sbin
  source~/.bash_profile
  #下载ngx_devel_kit和ngx_lua_module
  gitclone https://github.com/openresty/lua-nginx-module.git
  gitclone https://github.com/simpl/ngx_devel_kit.git

[*]  5.ngx_http_proxy_connect_module:是gitlab开源的第三方模块,用于配置nginx正向代理,支持https的connect属性。安装ngx_http_proxy_connect_module需要的准备工作为:
  cd/home/nginx/soft/nginx-1.9.0
  patch -p1 < /home/nginx/soft/ngx_http_proxy_connect_module/proxy_connect.patch
  #注意,打补丁操作是在nginx安装源文件根目录执行的

[*]  6.编译安装nginx
  su - nginx
  tar   -zxvf   nginx-1.9.0.tar.gz   –C /home/nginx/soft/
  cd/home/nginx/soft/nginx-1.9.0
  mkdir–p/home/nginx/nginx/{conf,temp/{client,proxy_temp,proxy_cache},logs,sbin}
  sed   -i   's/Server:\ nginx/Server:\ Company_name/'   src/http/ngx_http_header_filter_module.c
  ./configure      \\开始编译安装,以下为配置说明
  --prefix=/home/nginx/nginx    \\指定软件安装目录
  --conf-path=/home/nginx/nginx/conf/nginx.conf    \\指定主配置文件路径
  --sbin-path=/home/nginx/nginx/sbin                \\指定nginx命令路径
  --error-log-path=/home/nginx/nginx/logs/error.log \\指定错误日志路径
  --http-log-path=/home/nginx/nginx/logs/access.log      \\指定访问日志路径
  --with-http_ssl_module               \\添加ssl认证模块
  --with-http_gunzip_module      \\添加gunzip模块
  --add-module=/home/nginx/soft/nginx_upstream_check_module-master \\添加第三方模块,用以check后端应用状态
  --with-pcre=/home/nginx/soft/pcre-8.3.5         \\指定pcre目录,pcre用于正则匹配
  --add-module=/home/nginx/soft/lua-nginx-module         \\添加lua模块
  --add-module=/home/nginx/soft/ngx_devel_kit      \\lua模块需要使用devel_kit模块
  --with-stream    \\添加四层代理模块
  --without-http_fastcgi_module\\删除fastcgi模块,有使用此模块勿选
  --without-http_uwscgi_module         \\删除uwscgi模块,有使用此模块勿选
  --without-http_scgi_module   \\删除scgi模块,有使用此模块勿选
  --add-module=/home/nginx/soft/ngx_http_proxy_connect_module   \\添加第三方正向代理模块
  make -j2 && make install
2      配置
2.1主配置文件
  usernginx;   #使用nginx用户启动进程
  work_processes2;   #这里一般为cpu核数
  pid logs/nginx.pid;   #pid文件存储路径
  events{
  worker_connections   102400;#最大连接数,根据服务器实际情况配置
  useepoll;   #linux多路复用IO
  }
  http{
  includemime.types;   #默认配置,这里表示引用context-type的配置文件。如果需要添加新的类型,手动在此文件中添加即可。
  default_typeapplication/octet-stream;   #默认context-type类型定义。
  charsetutf-8;      #默认编码
  keepalive_timeout 65;       #会话超时时间
  sendfile on;   #开启调用linux的sendfile功能,提高文件传输效率
  server_tokens off; #关闭nginx的版本显示
  #以下标记蓝色配置信息,请根据应用详情配置,过大会影响服务器性能,过小会导致应用异常。
  client_header_buffer_size   2k;#读取客户端请求头部信息的缓冲区大小,
  large_client_header_buffers 416k;    #读取客户端请求头部信息的缓冲区的最大数量和大小
  client_max_body_size 8m;    #设置客户端请求体最大值
  proxy_cache cache_one;#在内存中开启缓冲区,名称为cache_one,只有配置这个proxy_cache_path才可使用
  proxy_read_timeout 300; #后端服务器读取超时
  proxy_send_timeout 300; #后端服务器发送超时
  proxy_buffer_size 16k;#后端response返回缓冲区大小
  proxy_buffers 4 16k;      #后端response返回缓冲区的最大数量和大小
  proxy_busy_buffers_size 32k; #一般为proxy_buffers的两倍,限制最大缓存区大小,如果大于此值,将会依靠proxy_temp_path的路径对response信息进行存储
  proxy_temp_file_write_size 32k;#大于32k的response信息,将会被存储到临时文件当中
  proxy_temp_path temp/proxy_temp; #proxy的临时文件路径
  proxy_ignore_client_abort on ;#这个参数默认是关闭状态的,此时如果客户端主动关闭请求或者客户端网络断掉,那么nginx会记录499,同时request_time是[后端已经处理时间]。而upstream_response_time为’_’。使用该参数后,那么客户端主动断掉连接之后,Nginx 会等待后端处理完(或者超时),然后 记录 「后端的返回信息」 到日志。所以,如果后端 返回 200, 就记录 200 ;如果后端放回 5XX ,那么就记录 5XX 。如果超时(默认60s,可以用 proxy_read_timeout 设置),Nginx 会主动断开连接,记录504。
  proxy_next_upstream http_500http_502 http_504 error timeout invalid header;   #定义后端在抛出500、502、504错误时,跳转到另一个可用服务器
  proxy_cache_path temp/proxy_cache levels=1:2 keys_zone=cache_one:100m inactive=2d max_size=1g; #定义后端缓存文件的存放路路径以及缓存策略,请根据实际需要进行配置。
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status$body_bytes_sent $http_referer '
  '"$http_user_agent" "$http_x_forward_for" '
  '$upstream_status $upstream_addr "$upstream_cache_status" '
  '$request_time$upstream_response_time'; #定义日志存储内容
  include conf.d/vhost/*.conf;    #指定引用匹配条件的虚拟主机文件
  include conf.d/upstream/*.conf;    #指定引用匹配条件的upstream配置文件
  #以上两条用于管理大批量应用时,对虚拟主机和upstream进行批量管理,如果应用不多,只使用Nginx.conf就可以
  }
2.2upstream配置
  由于不同的服务可能会对upstream重复使用,因此把upstream配置单独拉出来进行配置,以防止混淆。配置示例:
  upstream project_appname {#upstream的配置名称需要以项目名称开头,后跟应用名
  ip_hash; #代理访问策略,这里为ip_hash绑定的方式,根据应用情况自行考虑配置
  server ip_address:app_port; #此处配置为应用地址以及端口配置
  check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;      #注意,由于使用了upstream check模块,因此check行必须进行配置,用以检测后端应用的健康状态。可以根据应用的实际需求调整参数,比如type,可以为tcp、http等方式
  }
2.3Server域虚拟主机配置
  虚拟主机配置一般存放在nginx目录下的conf/conf.d/vhost目录下面,如果不存在,自行创建。以下是vhost的配置示例:
  #http协议配置示例:
  server{
  listen port;#侦听端口,为nginx对外提供服务的端口
  server_name dns/ip; #以实际情况为准,可以为nginx服务器的ip,也可以为定义好的域名
  #以下红色标记内容设置了一些时间的变量,第一个限制请求类型,第二个日志进行轮转。注意,if判断只能在location和server中定义
  if ($request_method !~ ^(GET|POST|HEAD)) {
  return 444;
  }
  if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
  set $year $1;
  set $month $2;
  set $day $3;
  }
  #gzip配置
  include conf.d/gzip/project-gzip.conf;    #gzip的配置文件路径,此处可根据情况配置,如果有压缩需求,就在server层配置
  location / {
  root html;         #静态文件目录
  index index.html;#索引文件,这两项配置根据项目进行配置,如果不需要,可以去除
  proxy_pass http://project_appname;    #此处为代理后端地址配置,引用upstream的配置名称
  proxy_set_header X-Forwarded-For $remote_addr; #透传客户端的真实IP,加入到请求的header当中,后端如果需要获取,须引用request的header信息中的X-Forwarded-For参数得到真实IP
  access_log /home/nginx/nginx/logs/project-appname-access-$year-$month-$day.log main;   #日志根据之前的time_iso8601参数分隔出来的三个变量,进行定义,用以实现日志轮转功能。
  }
  #注意,error_page如果后端应用有配置,此处可以不必配置,如果没有,必须配置。
  error_page    404 502 503 504 500 /50x.html;
  location = /50x.html {
  root html;
  access_log /home/nginx/logs/error-$year-$month-$day.log main;
  }    #以上为错误请求返回码返回页面配置,如果代理后端定义有,此得可以考虑是否需要配置
  }
  #https协议配置示例:
  server{
  listen port ssl;   #nginx服务侦听端口,ssl协议需要在此处标记。如果在编译时加上了--with-http_spdy_module参数,可以在ssl后加上spdy参数,如:ssl spdy
  server_name dns/ip;         #由于ssl绑定了域名,此处建议配置为域名
  ssl_certificate conf.d/ssl/project_appname.crt;   #证书存放路径
  ssl_certificate conf.d/ssl/project_appname.key;   #密钥存放路径
  ssl_session_cache shared:SSL:10m;   #设置ssl缓存的类型和大小,此处表示nginx共享ssl会话缓存,10m表示存储ssl缓存的空间大小
  ssl_session_timeout 5m; #ssl会话超时时间
  ssl_ciphers HIGH:!aNULL:!MD5;    #内容一致性校验算法,根据安全性级别自行定义,建议使用sha2以上的算法,此处禁用了md5算法。关于服务器支持的算法类型,可以使用openssl指令来查询
  ssl_prefer_server_ciphers on;    #此处表示,协商加密算法时,优先使用服务器端定义的加密算法,而非客户端浏览器的加密算法
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#由于默认配置包含ssl3,如果要求安全级别高,可以在此处强定义ssl协议支持的版本。
  location / {
  proxy_pass http://project_appname;
  proxy_set_header X-Forwarded-For $remote_addr;
  access_log /home/nginx/nginx/logs/app_name-access-$year-$month-$year-$day.log main;
  }    #此处配置类同于http协议的配置,不再说明
  }
2.4 gzip配置
  gzip用来对静态内容进行压缩,主要用来提高用户访问速度。此文件进行单独配置,视项目情况进行配置。配置示例:
  gzipon;#启用gzip压缩
  gzip_min_length    2k;#gzip压缩的最小文件,小于此大小的不进行压缩
  gzip_buffers4    16k; #设置缓冲区大小和数量
  gzip_comp_level    2;   #压缩级别,1-9,值越小压缩程度越高,但压缩越小越耗cpu
  gzip_types   text/js image/jepg image/png text/plain text/css text/javascript; #根据实际情况,如果有新的需要压缩的类型,在此处添加即可
  gzip_varyon; #根据客户端的http头来判断是否需要压缩,不支持的不压缩
  gzip_proxiedexpired no-cache no-store private auth;
  #expired - 启用压缩,如果header头中包含 "Expires" 头信息
  #no-cache - 启用压缩,如果header头中包含"Cache-Control:no-cache" 头信息
  #no-store - 启用压缩,如果header头中包含"Cache-Control:no-store" 头信息
  #private - 启用压缩,如果header头中包含"Cache-Control:private" 头信息
  #no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
  #no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
  #auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
  #any - 无条件启用压缩
2.5 stream配置
  #注意,stream作用域和http作用域是在同一个级别,不能配置在http作用域内
  stream{
  upstream appname { #定义upstream,用于代理多台tcp服务
  hash$remote_addrconsistent;
  server ip1:port    weight=1 max_fails=2    fail_timeout=30s;
  server ip2:port    weight=1 max_fails=2    fail_timeout=30s;
  }
  server {
  listen port_number;
  proxy_connect_timeout 10s;
  proxy_pass    appname;
  }
  }
2.6      ngx_proxy_connect模块配置
  #ngx_proxy_connect_module只涉及server域的配置,因此http域的不再重复说明,此模块用来做nginx正向代理,用以部分内网向互联网的访问请求。注意,使用代理后连接https协议,使用的是connect方法,如果目标服务器不支持,会抛出403错误。
  server   {
  listenprot_number;   #正向代理端口
  resolver 114.114.114.114;   #正向代理DNS解析服务器
  access_log    logs/proxy.log;      #日志路径
  proxy_connect;   #开启connect功能
  proxy_connect_allow 443 563; #许可https和nntps协议通过
  proxy_connect_connect_timeout    20s; #连接超时时间
  proxy_connect_read_timeout   20s;   #代理读超时时间
  proxy_connect_send_timeout   20s;   #代理发送超时时间
  location/ {
  if ($cookie_pro ~ "") {
  proxy_passhttp://$host:$cookie_passport$request_uri;   #这里使用了cookie来标识来自非80/443/563端口的请求,需要传入passport=port_number的cookie内容后,才能将请求转至目标服务器。
  }
  if ($cookie_pro ~ "https") {
  proxy_pass$cookie_pro://$host:$cookie_passport$request_uri;
  }    #当接收到来自非443端口的https请求时,需要在cookie中配置https协议才可以转到connect属性去连接目标服务器
  proxy_set_headerHost$host;
  }
  }
  非443端口使用connect方法测试样例: curl --proxy proxy_server:port -b passport=dest_port -b pro=https https://domain.com:port

页: [1]
查看完整版本: Nginx安装配置详解-修订版