编译安装nginx并配置

编译安装nginx并配置

[TOC]

reference1: https://www.runoob.com/linux/nginx-install-setup.html --> nginx

reference2: https://www.cnblogs.com/felixzh/p/6283791.html --> nginx编译参数详解

reference3: https://www.cnblogs.com/knowledgesea/p/5175711.html --> nginx配置详解

1. 安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre-devel # 或
yum -y groupinstall "Development Tools" "Server Platform Development" \ 
    --setopt=group_package_types=mandatory,default,optional # 此步骤较慢

2. 安装pcre

# pcre 作用是让 Nginx 支持 Rewrite 功能
cd /usr/local/src/
wget https://jaist.dl.sourceforge.net/project/pcre/pcre2/10.33/pcre2-10.33.tar.gz
cd pcre2-10.33 # 还是下载8.x版本, nginx暂时不支持10版本的pcre
./configure --prefix=/usr/local/pcre
make && make install
pcre-config --version # 查看pcre版本

3. 安装 Nginx

1. 创建 Nginx 运行使用的用户 nginx

/usr/sbin/groupadd nginx
/usr/sbin/useradd -r -s /sbin/nologin -g nginx nginx

2. 编译安装nginx

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
/configure --prefix=/usr/local/nginx \ 
           --user=nginx --group=nginx \
           --with-http_realip_module \
           --with-http_flv_module \ # 流媒体
           --with-http_image_filter_module \ # 需要安装GD库
           --with-http_gzip_static_module \
           --with-http_gunzip_module \ # 支持压缩
           --with-http_stub_status_module \ # 健康探测页面 
           --with-http_ssl_module \ 
           --with-threads \
           --with-file-aio \
           --with-pcre=/usr/local/src/pcre # 目前nginx还暂时不支持10版本的pcre
           -error-log-path= \ # 错误日志路径, 默认均在--prefix指定目录下
           -http-log-path= \ # http日志路径
           # 可在./configure时多次重复设置, 编译时的参数最好保存
make && make install
/usr/local/webserver/nginx/sbin/nginx -v # 查看nginx版本, 到此安装完成       

以下是编译参数详解:

--prefix= 指向安装目录
--sbin-path= 指定执行程序文件存放位置
--modules-path= 指定第三方模块的存放路径
--conf-path= 指定配置文件存放位置
--error-log-path= 指定错误日志存放位置
--pid-path= 指定pid文件存放位置
--lock-path= 指定lock文件存放位置
--user= 指定程序运行时的非特权用户
--group= 指定程序运行时的非特权用户组
--builddir= 指向编译目录
--with-rtsig_module 启用rtsig模块支持
--with-select_module 启用select模块支持, 一种轮询处理方式, 不推荐在高并发环境中使用, 禁用:--without-select_module
--with-poll_module 启用poll模块支持,功能与select相同, 不推荐在高并发环境中使用
--with-threads 启用thread pool支持
--with-file-aio 启用file aio支持
--with-http_ssl_module 启用https支持
--with-http_v2_module 启用ngx_http_v2_module支持
--with-ipv6 启用ipv6支持
--with-http_realip_module 允许从请求报文头中更改客户端的ip地址, 默认为关
--with-http_addition_module 启用ngix_http_additon_mdoule支持(作为一个输出过滤器, 分部分响应请求)
--with -http_xslt_module 启用ngx_http_xslt_module支持, 过滤转换XML请求 
--with-http_image_filter_mdoule 启用ngx_http_image_filter_module支持, 传输JPEG\GIF\PNG图片的一个过滤器, 默认不启用, 需要安装gd库
--with-http_geoip_module 启用ngx_http_geoip_module支持, 用于创建基于MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量
--with-http_sub_module 启用ngx_http_sub_module支持, 允许用一些其他文本替换nginx响应中的一些文本
--with-http_dav_module 启用ngx_http_dav_module支持, 增加PUT、DELETE、MKCOL创建集合, COPY和MOVE方法, 默认为关闭, 需要编译开启
--with-http_flv_module 启用ngx_http_flv_module支持, 提供寻求内存使用基于时间的偏移量文件
--with-http_mp4_module 启用ngx_http_mp4_module支持, 启用对mp4类视频文件的支持
--with-http_gzip_static_module 启用ngx_http_gzip_static_module支持, 支持在线实时压缩输出数据流
--with-http_random_index_module 启用ngx_http_random_index_module支持, 从目录中随机挑选一个目录索引
--with-http_secure_link_module 启用ngx_http_secure_link_module支持, 计算和检查要求所需的安全链接网址
--with-http_degradation_module 启用ngx_http_degradation_module 支持允许在内存不足的情况下返回204或444代码
--with-http_stub_status_module 启用ngx_http_stub_status_module 支持查看nginx的状态页
--without-http_charset_module 禁用ngx_http_charset_module这一模块, 可以进行字符集间的转换, 从其它字符转换成UTF-8或者从UTF8转换成其它字符. 它只能从服务器到客户端方向, 只有一个字节的字符可以转换
--without-http_gzip_module 禁用ngx_http_gzip_module支持, 同--with-http_gzip_static_module功能一样
--without-http_ssi_module 禁用ngx_http_ssi_module支持, 提供了一个在输入端处理服务器包含文件(SSI) 的过滤器
--without-http_userid_module 禁用ngx_http_userid_module支持, 该模块用来确定客户端后续请求的cookies
--without-http_access_module 禁用ngx_http_access_module支持, 提供了基于主机ip地址的访问控制功能
--without-http_auth_basic_module 禁用ngx_http_auth_basic_module支持, 可以使用用户名和密码认证的方式来对站点或部分内容进行认证
--without-http_autoindex_module 禁用ngx_http_authindex_module, 该模块用于在ngx_http_index_module模块没有找到索引文件时发出请求, 用于自动生成目录列表
--without-http_geo_module 禁用ngx_http_geo_module支持, 这个模块用于创建依赖于客户端ip的变量
--without-http_map_module 禁用ngx_http_map_module支持, 使用任意的键、值 对设置配置变量
--without-http_split_clients_module 禁用ngx_http_split_clients_module支持, 该模块用于基于用户ip地址、报头、cookies划分用户
--without-http_referer_module 禁用ngx_http_referer_modlue支持, 该模块用来过滤请求, 报头中Referer值不正确的请求
--without-http_rewrite_module 禁用ngx_http_rewrite_module支持. 该模块允许使用正则表达式改变URI, 并且根据变量来转向以及选择配置. 如果在server级别设置该选项, 那么将在location之前生效, 但如果location中还有更进一步的重写规则, location部分的规则依然会被执行. 如果这个URI重写是因为location部分的规则造成的, 那么location部分会再次被执行作为新的URI, 这个循环会被执行10次, 最后返回一个500错误. 
--without-http_proxy_module 禁用ngx_http_proxy_module支持, http代理功能
--without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持, 该模块允许nginx与fastcgi进程交互, 并通过传递参数来控制fastcgi进程工作
--without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持, 该模块用来使用uwsgi协议, uwsgi服务器相关
--without-http_scgi_module 禁用ngx_http_scgi_module支持, 类似于fastcgi, 也是应用程序与http服务的接口标准
--without-http_memcached_module 禁用ngx_http_memcached支持, 用来提供简单的缓存, 提高系统效率
--without-http_limit_conn_module 禁用ngx_http_limit_conn_module支持, 该模块可以根据条件进行会话的并发连接数进行限制
--without-http_limit_req_module 禁用ngx_limit_req_module支持, 该模块可以实现对于一个地址进行请求数量的限制
--without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持, 该模块在内存中常驻了一个1*1的透明gif图像, 可以被非常快速的调用
--without-http_browser_module 禁用ngx_http_browser_mdoule支持, 创建依赖于请求报头的值, 如果浏览器为modern, 则$modern_browser等于modern_browser_value的值;如果浏览器为old, 则$ancient_browser等于$ancient_browser_value指令分配的值;如果浏览器为MSIE, 则$msie等于1
--without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持, 该模块用于简单的负载均衡
--with-http_perl_module 启用ngx_http_perl_module支持, 它使nginx可以直接使用perl或通过ssi调用perl
--with-perl_modules_path= 设定perl模块路径
--with-perl= 设定perl库文件路径
--http-log-path= 设定access log路径
--http-client-body-temp-path= 设定http客户端请求临时文件路径
--http-proxy-temp-path= 设定http代理临时文件路径
--http-fastcgi-temp-path= 设定http fastcgi临时文件路径
--http-uwsgi-temp-path= 设定http scgi临时文件路径
--http-scgi-temp-path= 设定http scgi临时文件路径
--without-http 禁用http server功能
--without-http-cache 禁用http cache功能
--with-mail 启用POP3、IMAP4、SMTP代理模块
--with-mail_ssl_module 启用ngx_mail_ssl_module支持
--without-mail_pop3_module 禁用pop3协议
--without-mail_iamp_module 禁用iamp协议
--without-mail_smtp_module 禁用smtp协议
--with-google_perftools_module 启用ngx_google_perftools_mdoule支持, 调试用, 可以用来分析程序性能瓶颈
--with-cpp_test_module 启用ngx_cpp_test_module支持
--add-module= 指定外部模块路径, 启用对外部模块的支持
--with-cc= 指向C编译器路径
--with-cpp= 指向C预处理路径
--with-cc-opt= 设置C编译器参数, 指定--with-cc-opt="-I /usr/lcal/include", 如果使用select()函数, 还需要同时指定文件描述符数量--with-cc-opt="-D FD_SETSIZE=2048" (PCRE库) 
--with-ld-opt= 设置连接文件参数, 需要指定--with-ld-opt="-L /usr/local/lib" (PCRE库) 
--with-cpu-opt= 指定编译的CPU类型, 如pentium,pentiumpro,...amd64,ppc64...
--without-pcre 禁用pcre库
--with-pcre 启用pcre库
--with-pcre= 指向pcre库文件目录
--with-pcre-opt= 在编译时为pcre库设置附加参数
--with-md5= 指向md5库文件目录
--with-md5-opt= 编译时为md5库设置附加参数
--with-md5-asm 使用md5汇编源
--with-sha1= 指向sha1库文件目录
--with-sha1-opt= 编译时为sha1库设置附加参数
--with-sha1-asm 使用sha1汇编源
--with-zlib= 指向zlib库文件目录
--with-zlib-opt= 在编译时为zlib设置附加参数
--with-zlib-asm= 为指定的CPU使用汇编源进行优化
--with-libatomic 为原子内存的更新操作的实现提供一个架构
--with-libatomic= 指向libatomic_ops的安装目录
--with-openssl= 指向openssl安装目录
--with-openssl-opt= 在编译时为openssl设置附加参数
--with-debug 启用debug日志

4. Nginx 配置

1. 编辑nginx配置文件

vi /usr/local/nginx/conf/nginx.conf # 将/usr/local/nginx/conf/nginx.conf替换为以下内容
user nginx nginx;
worker_processes 2; # 设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; # 日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
# Specifies the value for maximum file descriptors that can be opened by this process
worker_rlimit_nofile 65535;
events
{
  use epoll;
  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 "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  
# charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
 # limit_zone crawler $binary_remote_addr 10m;
 # 下面是server虚拟主机的配置
 server
  {
    listen 80; # 监听端口
    server_name localhost; # 域名
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html; # 站点目录
    
    location /status
    {
      stub_status on;
      allow 172.16.0.0/16;
      deny all;
    }
    
    location ~ .*\.(php|php5)?$
    {
      # fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    
    access_log off;
  }

}

2. 检查配置文件nginx.conf的正确性

/usr/local/nginx/sbin/nginx -t

5. 启动Nginx

/usr/local/nginx/sbin/nginx
ps -ef | grep nginx # 查看nginx的master和worker

/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop   # 停止 Nginx

6. 自带的健康检测

利用nginx自带模块ngx_http_proxy_module和ngx_http_upstream_module对后端节点做健康检查:


语法: proxy_connect_timeout time;
默认值: proxy_connect_timeout 60s;
上下文: http, server, location
设置与后端服务器建立连接的超时时间. 应该注意这个超时一般不可能大于75秒.

语法: proxy_read_timeout time;
默认值: proxy_read_timeout 60s;
上下文: http, server, location
定义从后端服务器读取响应的超时. 此超时是指相邻两次读操作之间的最长时间间隔, 而不是整个响应传输完成的最长时间. 如果后端服务器在超时时间段内没有传输任何数据, 连接将被关闭.


语法: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 |http_404 | off ...;
默认值: proxy_next_upstream error timeout;
上下文: http, server, location
指定在何种情况下一个失败的请求应该被发送到下一台后端服务器:

error 和后端服务器建立连接时, 或者向后端服务器发送请求时, 或者从后端服务器接收响应头时, 出现错误
timeout 和后端服务器建立连接时, 或者向后端服务器发送请求时, 或者从后端服务器接收响应头时, 出现超时
invalid_header 后端服务器返回空响应或者非法响应头
http_500 后端服务器返回的响应状态码为500
http_502 后端服务器返回的响应状态码为502
http_503 后端服务器返回的响应状态码为503
http_504 后端服务器返回的响应状态码为504
http_404 后端服务器返回的响应状态码为404
off 停止将请求发送给下一台后端服务器

需要理解一点的是, 只有在没有向客户端发送任何数据以前, 将请求转给下一台后端服务器才是可行的. 也就是说, 如果在传输响应到客户端时出现错误或者超时, 这类错误是不可能恢复的.

upstream LB-WWW {
      ip_hash; 
      #这是负载均衡的ip_hash负载策略,好处是实现session共享。根据需求决定是否加这个配置。
      server 192.168.1.101:80 max_fails=3 fail_timeout=30s;     
      #max_fails = 3 为允许失败的次数,默认值为1。 这是对后端节点做健康检查。
      server 192.168.1.102:80 max_fails=3 fail_timeout=30s;     
      #fail_timeout = 30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间
      server 192.168.1.118:80 max_fails=3 fail_timeout=30s;
    }
    
server {
     listen       80;
     server_name  www.wangshibo.com;
    
     access_log  /usr/local/nginx/logs/www-access.log main;
     error_log  /usr/local/nginx/logs/www-error.log;
    # 因为upstream配置里每个几秒就会去健康后端机器, 当连接失败时, 错误信息就输出到error.log日志里
      
     location / {
         proxy_pass http://LB-WWW;
         proxy_connect_timeout 300;
         proxy_send_timeout 300;
         proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
     }
}

你可能感兴趣的:(编译安装nginx并配置)