Nginx是一个高性能的HTTP和反向代理Web服务器(俄罗斯人研发)https://lnmp.org/nginx.html,除它之外Apache、Tomcat、Jetty、IIS,它们都是Web服务器,或者叫做WWW(World Wide Web)服务器,相应地也都具备Web服务器的基本功能。Nginx

Tomcat、Jetty 面向java语言,先天就是重量级的WEB服务器,其性能与Nginx没有可比性。 IIS只能在Windows操作系统上运行 官方文档 http://nginx.org/en/docs/beginners_guide.html 官方安装教程 https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/

Centos安装

yum 一键安装nginx 环境

  1. yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
  2. yum install gcc gcc-c++ make unzip pcre pcre-devel zlib zlib-devel libxml2 libxml2-devel readline readline-devel ncurses ncurses-devel perl-devel perl-ExtUtils-Embed openssl-devel -y
  3. # 第二种方式安装编译依赖
  4. gcc -v
  5. yum -y install gcc
  6. yum install -y pcre pcre-devel
  7. yum install -y zlib zlib-devel
  8. yum install -y openssl openssl-devel

下载nginx-1.20.1.tar.gz

  1. cd /opt
  2. wget http://nginx.org/download/nginx-1.20.1.tar.gz

编译、安装Nginx

  1. tar -zxvf nginx-1.20.1.tar.gz
  2. cd /opt/nginx-1.20.1
  3. ./configure & make & make install

启动测试Nginx

  1. /usr/local/nginx/sbin/nginx
  2. ps -ef | grep nginx

Nginx常用命令

help命令

  1. # 查看nginx版本号
  2. ./sbin/nginx -v
  3. # 查看命令帮助
  4. [root@basic sbin]# ./nginx -h
  5. nginx version: nginx/1.20.1
  6. Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
  7. [-e filename] [-c filename] [-g directives]
  8. Options:
  9. -?,-h : this help
  10. -v : show version and exit
  11. -V : show version and configure options then exit
  12. -t : test configuration and exit
  13. -T : test configuration, dump it and exit
  14. -q : suppress non-error messages during configuration testing
  15. -s signal : send signal to a master process: stop, quit, reopen, reload
  16. -p prefix : set prefix path (default: /usr/local/nginx/)
  17. -e filename : set error log file (default: logs/error.log)
  18. -c filename : set configuration file (default: conf/nginx.conf)
  19. -g directives : set global directives out of configuration file

默认方式启动:

  1. /usr/local/nginx/sbin
  2. /usr/local/nginx/sbin -s reload

-c:指定配置文件启动

  1. /usr/local/nginx/sbin -c /tmp/nginx.conf

-p :指定nginx程序目录启动

  1. /usr/local/nginx/sbin -p /usr/local/nginx/

Mac安装

  1. # Mac 使用brew安装nginx 各种命令
  2. 安装:brew install nginx
  3. 启动:brew services start nginx
  4. 重启:brew services restart nginx
  5. 停止:brew services stop nginx
  6. 查看:cat usr/local/etc/nginx/nginx.conf
  7. 编辑:vi usr/local/etc/nginx/nginx.conf

Nginx启动

  1. # 查看开发端口号
  2. firewall-cmd --list-all
  3. firewall-cmd --list-ports
  4. # 开启端口
  5. firewall-cmd --add-service=http --permanent
  6. sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
  7. # 重启防火墙
  8. firewall-cmd -reload
  9. # 关闭Centos自带的firewall防火墙
  10. systemctl stop firewalld.service
  11. # 禁止Firewall下次启动
  12. systemctl disable firewalld.service #禁止firewall开机启动
  13. # 批量开放所有端口1/65535
  14. firewall-cmd --permanent --zone=public --add-port=1-65535/tcp
  15. firewall-cmd --permanent --zone=public --add-port=1-65535/udp
  16. firewall-cmd --reload
  17. #命令含义:
  18. zone #作用域
  19. add-port=80/tcp #添加端口,格式为:端口/通讯协议
  20. permanent #永久生效,没有此参数重启后失效

open() “/usr/local/nginx/html/favicon.ico” failed (2: No such file or directory)

只需要关闭 favicon.ico 的 log:

  1. vim conf/nginx.conf
  2. # 添加
  3. location = /favicon.ico {
  4. log_not_found off;
  5. access_log off;
  6. }
  7. =:用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
  8. ~:用于表示 uri 包含正则表达式,并且区分大小写
  9. ~*:用于表示 uri 包含正则表达式,并且不区分大小写
  10. ^~:用于不含正则表达式的 uri 前,要求Nginx服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配
  11. # 基于正则动静分离
  12. location ~* \.(gif|jpg|png|css|js)$ {
  13. root /usr/www/static;
  14. }
  15. # 防盗链配置演示,加入至指定location 即可实现
  16. valid_referers none blocked *.luban.com;
  17. if ($invalid_referer) {
  18. return 403;
  19. }
  20. # 下载限速
  21. location /download {
  22. limit_rate 1m; //限制每S下载速度
  23. limit_rate_after 30m; // 超过30 之 后在下载
  24. }
  25. # 创建IP黑名单
  26. #封禁指定IP
  27. deny 192.168.0.1;
  28. allow 192.168.0.1;
  29. #开放指定IP 段
  30. allow 192.168.0.0/24;
  31. #封禁所有
  32. deny all;
  33. #开放所有
  34. allow all;
  35. # 创建黑名单文件
  36. echo 'deny 192.168.0.132;' >> balck.ip
  37. #http 配置块中引入 黑名单文件
  38. include black.ip;
  39. # 日志格式
  40. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  41. '$status $body_bytes_sent "$http_referer" '
  42. '"$http_user_agent" "$http_x_forwarded_for"';
  43. access_log logs/access.log main;
  44. #基于域名打印日志
  45. access_log logs/$host.access.log main;

模块更新

  1. # 添加状态查查看模块
  2. ./configure --with-http_stub_status_module
  3. # 重新创建主文件
  4. make
  5. # 将新生成的nginx 文件覆盖 旧文件。
  6. cp objs/nginx /usr/local/nginx/sbin/

Nginx模块安装教程

nginx官网模块地址:https://www.nginx.com/nginx-wiki/build/dirhtml/modules/

安装echo-nginx-module插件模块

1、首先下载echo-nginx-module插件包,下载到/opt目录
2、接下来就执行下面的命令安装即刻
—add-module:第三方模块安装命令
—with-xxxxxx:nginx自动的模块

  1. ./configure --prefix=/usr/local/nginx --add-module=/opt/echo-nginx-module-0.62 --with-http_stub_status_module && make -j2 && make install

查看nginx版本以及安装的插件

  1. /user/local/nginx/sbin/nginx -V
  2. [root@basic nginx]# sbin/nginx -V
  3. nginx version: nginx/1.20.1
  4. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  5. configure arguments: --prefix=/usr/local/nginx --add-module=/opt/echo-nginx-module-0.62 --with-http_stub_status_module
  6. configure arguments: --prefix=/usr/local/openresty/nginx
  7. --with-cc-opt=-O2
  8. --add-module=../ngx_devel_kit-0.3.0
  9. --add-module=../echo-nginx-module-0.61
  10. --add-module=../xss-nginx-module-0.06
  11. --add-module=../ngx_coolkit-0.2rc3
  12. --add-module=../set-misc-nginx-module-0.32
  13. --add-module=../form-input-nginx-module-0.12
  14. --add-module=../encrypted-session-nginx-module-0.08
  15. --add-module=../srcache-nginx-module-0.31
  16. --add-module=../ngx_lua-0.10.13
  17. --add-module=../ngx_lua_upstream-0.07
  18. --add-module=../headers-more-nginx-module-0.33
  19. --add-module=../array-var-nginx-module-0.05
  20. --add-module=../memc-nginx-module-0.19
  21. --add-module=../redis2-nginx-module-0.15
  22. --add-module=../redis-nginx-module-0.3.7
  23. --add-module=../rds-json-nginx-module-0.15
  24. --add-module=../rds-csv-nginx-module-0.09
  25. --add-module=../ngx_stream_lua-0.0.5
  26. --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib
  27. --with-http_stub_status_module
  28. --with-http_ssl_module
  29. --with-http_realip_module
  30. --with-stream --with-stream_ssl_module

安装启动过程中遇到的错误问题

worker_connections exceed open file resource limit: 1024

解决办法

  1. 1、查看用户打开的最大文件数
  2. ulimit -n
  3. 查看参数:ulimt -a
  4. 设置最大数:ulimt -n 65535
  5. 2、查看系统每个进程打开的最大文件数
  6. more /proc/sys/fs/file-max
  7. 3、修改/etc/security/limits.conf文件,在文件中添加如下行:
  8. * soft noproc 6553560
  9. * hard noproc 6553560
  10. * soft nofile 6553560
  11. * hard nofile 6553560
  12. 限制了任意用户的最大线程数和文件数为65535
  13. 4、修改/etc/rc.local脚本,在脚本中添加如下行:
  14. echo '6553560' > /proc/sys/fs/file-max
  15. 5、重启电脑
  16. reboot -h now