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 环境
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
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
# 第二种方式安装编译依赖
gcc -v
yum -y install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
下载nginx-1.20.1.tar.gz
cd /opt
wget http://nginx.org/download/nginx-1.20.1.tar.gz
编译、安装Nginx
tar -zxvf nginx-1.20.1.tar.gz
cd /opt/nginx-1.20.1
./configure & make & make install
启动测试Nginx
/usr/local/nginx/sbin/nginx
ps -ef | grep nginx
Nginx常用命令
help命令
# 查看nginx版本号
./sbin/nginx -v
# 查看命令帮助
[root@basic sbin]# ./nginx -h
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-e filename : set error log file (default: logs/error.log)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
默认方式启动:
/usr/local/nginx/sbin
/usr/local/nginx/sbin -s reload
-c:指定配置文件启动
/usr/local/nginx/sbin -c /tmp/nginx.conf
-p :指定nginx程序目录启动
/usr/local/nginx/sbin -p /usr/local/nginx/
Mac安装
# Mac 使用brew安装nginx 各种命令
安装:brew install nginx
启动:brew services start nginx
重启:brew services restart nginx
停止:brew services stop nginx
查看:cat usr/local/etc/nginx/nginx.conf
编辑:vi usr/local/etc/nginx/nginx.conf
Nginx启动
# 查看开发端口号
firewall-cmd --list-all
firewall-cmd --list-ports
# 开启端口
firewall-cmd --add-service=http --permanent
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
# 重启防火墙
firewall-cmd -reload
# 关闭Centos自带的firewall防火墙
systemctl stop firewalld.service
# 禁止Firewall下次启动
systemctl disable firewalld.service #禁止firewall开机启动
# 批量开放所有端口1/65535
firewall-cmd --permanent --zone=public --add-port=1-65535/tcp
firewall-cmd --permanent --zone=public --add-port=1-65535/udp
firewall-cmd --reload
#命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
open() “/usr/local/nginx/html/favicon.ico” failed (2: No such file or directory)
只需要关闭 favicon.ico 的 log:
vim conf/nginx.conf
# 添加
location = /favicon.ico {
log_not_found off;
access_log off;
}
=:用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
~:用于表示 uri 包含正则表达式,并且区分大小写
~*:用于表示 uri 包含正则表达式,并且不区分大小写
^~:用于不含正则表达式的 uri 前,要求Nginx服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配
# 基于正则动静分离
location ~* \.(gif|jpg|png|css|js)$ {
root /usr/www/static;
}
# 防盗链配置演示,加入至指定location 即可实现
valid_referers none blocked *.luban.com;
if ($invalid_referer) {
return 403;
}
# 下载限速
location /download {
limit_rate 1m; //限制每S下载速度
limit_rate_after 30m; // 超过30 之 后在下载
}
# 创建IP黑名单
#封禁指定IP
deny 192.168.0.1;
allow 192.168.0.1;
#开放指定IP 段
allow 192.168.0.0/24;
#封禁所有
deny all;
#开放所有
allow all;
# 创建黑名单文件
echo 'deny 192.168.0.132;' >> balck.ip
#http 配置块中引入 黑名单文件
include black.ip;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
#基于域名打印日志
access_log logs/$host.access.log main;
模块更新
# 添加状态查查看模块
./configure --with-http_stub_status_module
# 重新创建主文件
make
# 将新生成的nginx 文件覆盖 旧文件。
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自动的模块
./configure --prefix=/usr/local/nginx --add-module=/opt/echo-nginx-module-0.62 --with-http_stub_status_module && make -j2 && make install
查看nginx版本以及安装的插件
/user/local/nginx/sbin/nginx -V
[root@basic nginx]# sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --add-module=/opt/echo-nginx-module-0.62 --with-http_stub_status_module
configure arguments: --prefix=/usr/local/openresty/nginx
--with-cc-opt=-O2
--add-module=../ngx_devel_kit-0.3.0
--add-module=../echo-nginx-module-0.61
--add-module=../xss-nginx-module-0.06
--add-module=../ngx_coolkit-0.2rc3
--add-module=../set-misc-nginx-module-0.32
--add-module=../form-input-nginx-module-0.12
--add-module=../encrypted-session-nginx-module-0.08
--add-module=../srcache-nginx-module-0.31
--add-module=../ngx_lua-0.10.13
--add-module=../ngx_lua_upstream-0.07
--add-module=../headers-more-nginx-module-0.33
--add-module=../array-var-nginx-module-0.05
--add-module=../memc-nginx-module-0.19
--add-module=../redis2-nginx-module-0.15
--add-module=../redis-nginx-module-0.3.7
--add-module=../rds-json-nginx-module-0.15
--add-module=../rds-csv-nginx-module-0.09
--add-module=../ngx_stream_lua-0.0.5
--with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib
--with-http_stub_status_module
--with-http_ssl_module
--with-http_realip_module
--with-stream --with-stream_ssl_module
安装启动过程中遇到的错误问题
worker_connections exceed open file resource limit: 1024
解决办法
1、查看用户打开的最大文件数
ulimit -n
查看参数:ulimt -a
设置最大数:ulimt -n 65535
2、查看系统每个进程打开的最大文件数
more /proc/sys/fs/file-max
3、修改/etc/security/limits.conf文件,在文件中添加如下行:
* soft noproc 6553560
* hard noproc 6553560
* soft nofile 6553560
* hard nofile 6553560
限制了任意用户的最大线程数和文件数为65535
4、修改/etc/rc.local脚本,在脚本中添加如下行:
echo '6553560' > /proc/sys/fs/file-max
5、重启电脑
reboot -h now