
离线安装 Nginx参考来源一、下载二、编译参数三、配置环境变量四、创建nginx服务五、启动及验证六、编译参数其他、1、下载功能2、当图片服务器参考来源1、离线安装 Nginx2、Nginx服务配合负载均衡做代理网关3、nginx好用的模块一、下载Nginx官网Nginx官网cd/workspacetar-zxvfnginx-1.22.1.tar.gzmvnginx-1.22.1 nginxcdnginx ./configure --with-http_mp4_module --with-http_flv_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_gunzip_module --with-file-aiosudomakemakeinstall二、编译参数三、配置环境变量vi/etc/profile内容如下exportNGINX_HOME/usr/local/nginxexportPATH$PATH:$NGINX_HOME/sbin生效source/etc/profile四、创建nginx服务sudovi/lib/systemd/system/nginx.service内容如下[Unit]DescriptionnginxserviceAfternetwork.target[Service]TypeforkingExecStart/usr/local/nginx/sbin/nginxExecReload/usr/local/nginx/sbin/nginx-sreloadExecStop/usr/local/nginx/sbin/nginx-squitPrivateTmptrue[Third_party]WantedBymulti-user.target五、启动及验证# 启动nginxsystemctl start nginx# 查看nginx状态systemctl status nginx# 设置开机自启动systemctlenablenginx# 验证curlhttp://localhost源码启动nginx-c/usr/local/nginx/conf/nginx.conf重加载nginx-sreload-c/usr/local/nginx/conf/nginx.conf#docker方式重加载dockerexec-itXXX nginx-sreload六、编译参数参考来源1、https://www.cnblogs.com/linyouyi/p/11502282.html2、https://comate.baidu.com/zh/page/2kstn86weoh3、https://vip.kingdee.com/knowledge/464722953813409280?productLineId29isKnowledge2langzh-CN./configure 脚本后面可以跟许多参数来定制 Nginx 的配置详情看官网https://nginx.org/en/docs/configure.html部分常用参数配置–prefixPATH指定 Nginx 安装目录默认为 /usr/local/nginx –sbin-pathPATH指定 Nginx 可执行文件的路径默认为 PREFIX/sbin/nginx –conf-pathPATH指定 Nginx 配置文件的路径默认为 PREFIX/conf/nginx.conf –error-log-pathPATH指定错误日志文件的路径默认为 PREFIX/logs/error.log –http-log-pathPATH指定访问日志文件的路径默认为 PREFIX/logs/access.log –pid-pathPATH指定 PID 文件的路径默认为 PREFIX/logs/nginx.pid –lock-pathPATH指定锁文件的路径默认为 PREFIX/logs/nginx.lock –userUSER指定 Nginx 工作进程运行的用户默认为 nobody –groupGROUP 指定 Nginx 工作进程运行的用户组默认为 nobody –with-http_ssl_module 启用 SSL 支持 –with-http_v2_module 启用 HTTP/2 支持需要 OpenSSL1.0.2 或更高版本 –with-http_gzip_static_module 启用gzip静态文件压缩支持 –with-http_realip_module 启用获取客户端真实 IP 地址的支持 –with-http_stub_status_module 启用状态页面模块用于监控 Nginx 状态 –with-http_sub_module 启用响应内容替换模块 –with-http_dav_module 启用 WebDAV 支持 –with-http_flv_module 启用 FLV 视频流支持 –with-http_mp4_module 启用 MP4 视频流支持 –with-http_gunzip_module 启用 gunzip 模块用于解压gzip压缩的内容 –with-http_auth_request_module 启用基于子请求的认证模块 –with-http_random_index_module 启用随机索引模块 –with-http_secure_link_module 启用安全链接模块 –with-http_degradation_module 启用降级模块 –with-http_perl_module 启用 Perl 模块需要 Perl5.6.1 或更高版本 –with-mail 启用邮件代理模块 –with-mail_ssl_module 启用邮件代理模块的 SSL 支持 –with-stream 启用流媒体模块 –with-stream_ssl_module 启用流媒体模块的 SSL 支持 –with-stream_realip_module 启用流媒体模块的获取客户端真实 IP 地址支持 –with-stream_geoip_module 启用流媒体模块的 GeoIP 支持需要 GeoIP 库 –with-http_geoip_module 启用 HTTP 模块的 GeoIP 支持需要 GeoIP 库 –with-file-aio 启用文件异步 I/O 支持 –with-threads 启用线程池支持 –with-compat 启用兼容模式允许使用第三方模块 –without-http 不编译 HTTP 模块 –without-http-cache 不编译 HTTP 缓存模块 –without-mail_pop3_module 不编译 POP3 邮件协议模块 –without-mail_imap_module 不编译 IMAP 邮件协议模块 –without-mail_smtp_module 不编译 SMTP 邮件协议模块1、Nginx被动监控检查proxy_next_upstream#已内置完整示例upstream cluster{server172.16.0.23:80max_fails1fail_timeout10s;server172.16.0.24:80max_fails1fail_timeout10s;# max_fails1和fail_timeout10s 表示在单位周期为10s钟内中达到1次连接失败那么接将把节点标记为不可用并等待下一个周期同样时常为fail_timeout再一次去请求判断是否连接是否成功。# fail_timeout为10s,max_fails为1次。}server{listen80;server_name xxxxxxx.com;location /{proxy_pass http://cluster;}}2、Nginx主动健康检查主动地健康检查nignx定时主动地去ping后端的服务列表当发现某服务出现异常时把该服务从健康列表中移除当发现某服务恢复时又能够将该服务加回健康列表中。--nginx_upstream_check_module完整配置示例http{upstream backend{server192.168.0.1:80;server192.168.0.2:80;checkinterval3000rise2fall3timeout1000typehttp;check_http_sendHEAD /health HTTP/1.0\r\n\r\n;check_http_expect_alive http_2xx http_3xx;}server{location /{proxy_pass http://backend;}location /status{check_status;access_log off;}}}下载模块oldgitclone https://github.com/yaoweibin/nginx_upstream_check_module.gitnewgitclone https://github.com/mofantor/nginx_upstream_check_module.gitwgethttp://nginx.org/download/nginx-1.30.0.tar.gztar-xzvfnginx-1.30.0.tar.gzcdnginx-1.30.0/# 编译安装patch-p1../nginx_upstream_check_module/check_1.30.0.patch ./configure --add-module../nginx_upstream_check_modulemakesudomakeinstall状态监控通过check_status指令可查看健康状态支持JSON/HTML格式访问http://your-server/status获取结果。location /status{check_status json;#allow 192.168.0.0/16;#deny all;}3、这个模块能够获取Nginx自上次启动以来的工作状态(状态统计)--with-http_stub_status_module示例location /nginx_status{stub_status on;access_log off;#访问控制#allow 192.168.110.132;;#deny all;#认证#auth_basic close site;#auth_basic_user_file /usr/local/nginx/conf/htpasswd;}认证设置密码htpasswd -c /usr/local/nginx/conf/htpasswd admin访问http://localhost/nginx_status会看到这样的信息Active connections:291server accepts handled requests166309481663094831070465Reading:6Writing:179Waiting:106Active connections当前的活跃连接数accepts服务器已接受的连接数handled服务器已处理的连接数requests服务器已处理的请求Reading – Nginx 读取的请求头次数为 6Writting – Nginx 读取请求体、处理请求并发送响应给客户端的次数为 179Waiting – 当前活动的长连接数106。其他、1、下载功能在html目录下创建一个目录download可以放各种文件还要存在一个index.htmllocation中添加autoindex on;server{listen18010;server_name localhost;#access_log logs/host.access.log main;location /images/{alias/usr/local/nginx/html/images/;#root /usr/local/nginx/html;# 和alias类似index index.html index.htm;autoindex on;#auth_basic close site;auth_basic_user_file /usr/local/nginx/conf/htpasswd;auth_basic always;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page500502503504/50x.html;location/50x.html{root html;}}2、当图片服务器在html目录中创建一个目录images可以放图片访问http://localhost:80/imgaes/xxx.jpgserver{listen8010;server_name localhost;#access_log logs/host.access.log main;location /{alias/usr/local/nginx/html/images/;index index.html index.htm;autoindex on;#加这个可以有下载的感觉}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page500502503504/50x.html;location/50x.html{root html;}}1、Linux系统版本查询2、Debian/Ubuntu系统apt-get指定软件源配置指南